Skip to content

Commit 328d540

Browse files
RUST-1406 Convert raw errors to standard error type (#552)
1 parent b407357 commit 328d540

File tree

10 files changed

+244
-300
lines changed

10 files changed

+244
-300
lines changed

src/de/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ impl From<string::FromUtf8Error> for Error {
7676
}
7777
}
7878

79-
impl From<crate::raw::Error> for Error {
80-
fn from(value: crate::raw::Error) -> Self {
81-
Self::deserialization(value)
79+
impl From<crate::error::Error> for Error {
80+
fn from(error: crate::error::Error) -> Self {
81+
Self::deserialization(error.to_string())
8282
}
8383
}
8484

src/document.rs

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,12 @@ impl Document {
252252
match self.get(key) {
253253
Some(&Bson::Double(v)) => Ok(v),
254254
Some(bson) => Err(Error::value_access_unexpected_type(
255-
key,
256255
bson.element_type(),
257256
ElementType::Double,
258257
)),
259-
None => Err(Error::value_access_not_present(key)),
258+
None => Err(Error::value_access_not_present()),
260259
}
260+
.map_err(|e| e.with_key(key))
261261
}
262262

263263
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -267,12 +267,12 @@ impl Document {
267267
match self.get_mut(key) {
268268
Some(&mut Bson::Double(ref mut v)) => Ok(v),
269269
Some(bson) => Err(Error::value_access_unexpected_type(
270-
key,
271270
bson.element_type(),
272271
ElementType::Double,
273272
)),
274-
None => Err(Error::value_access_not_present(key)),
273+
None => Err(Error::value_access_not_present()),
275274
}
275+
.map_err(|e| e.with_key(key))
276276
}
277277

278278
/// Returns a reference to the value for the given key if one is present and is of type
@@ -282,12 +282,12 @@ impl Document {
282282
match self.get(key) {
283283
Some(Bson::Decimal128(v)) => Ok(v),
284284
Some(bson) => Err(Error::value_access_unexpected_type(
285-
key,
286285
bson.element_type(),
287286
ElementType::Decimal128,
288287
)),
289-
None => Err(Error::value_access_not_present(key)),
288+
None => Err(Error::value_access_not_present()),
290289
}
290+
.map_err(|e| e.with_key(key))
291291
}
292292

293293
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -297,42 +297,42 @@ impl Document {
297297
match self.get_mut(key) {
298298
Some(&mut Bson::Decimal128(ref mut v)) => Ok(v),
299299
Some(bson) => Err(Error::value_access_unexpected_type(
300-
key,
301300
bson.element_type(),
302301
ElementType::Decimal128,
303302
)),
304-
None => Err(Error::value_access_not_present(key)),
303+
None => Err(Error::value_access_not_present()),
305304
}
305+
.map_err(|e| e.with_key(key))
306306
}
307307

308308
/// Returns a reference to the value for the given key if one is present and is of type
309309
/// [`ElementType::String`].
310310
pub fn get_str(&self, key: impl AsRef<str>) -> Result<&str> {
311311
let key = key.as_ref();
312312
match self.get(key) {
313-
Some(Bson::String(v)) => Ok(v),
313+
Some(Bson::String(v)) => Ok(v.as_str()),
314314
Some(bson) => Err(Error::value_access_unexpected_type(
315-
key,
316315
bson.element_type(),
317316
ElementType::String,
318317
)),
319-
None => Err(Error::value_access_not_present(key)),
318+
None => Err(Error::value_access_not_present()),
320319
}
320+
.map_err(|e| e.with_key(key))
321321
}
322322

323323
/// Returns a mutable reference to the value for the given key if one is present and is of type
324324
/// [`ElementType::String`].
325325
pub fn get_str_mut(&mut self, key: impl AsRef<str>) -> Result<&mut str> {
326326
let key = key.as_ref();
327327
match self.get_mut(key) {
328-
Some(&mut Bson::String(ref mut v)) => Ok(v),
328+
Some(&mut Bson::String(ref mut v)) => Ok(v.as_mut_str()),
329329
Some(bson) => Err(Error::value_access_unexpected_type(
330-
key,
331330
bson.element_type(),
332331
ElementType::String,
333332
)),
334-
None => Err(Error::value_access_not_present(key)),
333+
None => Err(Error::value_access_not_present()),
335334
}
335+
.map_err(|e| e.with_key(key))
336336
}
337337

338338
/// Returns a reference to the value for the given key if one is present and is of type
@@ -342,12 +342,12 @@ impl Document {
342342
match self.get(key) {
343343
Some(Bson::Array(v)) => Ok(v),
344344
Some(bson) => Err(Error::value_access_unexpected_type(
345-
key,
346345
bson.element_type(),
347346
ElementType::Array,
348347
)),
349-
None => Err(Error::value_access_not_present(key)),
348+
None => Err(Error::value_access_not_present()),
350349
}
350+
.map_err(|e| e.with_key(key))
351351
}
352352

353353
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -357,12 +357,12 @@ impl Document {
357357
match self.get_mut(key) {
358358
Some(&mut Bson::Array(ref mut v)) => Ok(v),
359359
Some(bson) => Err(Error::value_access_unexpected_type(
360-
key,
361360
bson.element_type(),
362361
ElementType::Array,
363362
)),
364-
None => Err(Error::value_access_not_present(key)),
363+
None => Err(Error::value_access_not_present()),
365364
}
365+
.map_err(|e| e.with_key(key))
366366
}
367367

368368
/// Returns a reference to the value for the given key if one is present and is of type
@@ -372,12 +372,12 @@ impl Document {
372372
match self.get(key) {
373373
Some(Bson::Document(v)) => Ok(v),
374374
Some(bson) => Err(Error::value_access_unexpected_type(
375-
key,
376375
bson.element_type(),
377376
ElementType::EmbeddedDocument,
378377
)),
379-
None => Err(Error::value_access_not_present(key)),
378+
None => Err(Error::value_access_not_present()),
380379
}
380+
.map_err(|e| e.with_key(key))
381381
}
382382

383383
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -387,12 +387,12 @@ impl Document {
387387
match self.get_mut(key) {
388388
Some(&mut Bson::Document(ref mut v)) => Ok(v),
389389
Some(bson) => Err(Error::value_access_unexpected_type(
390-
key,
391390
bson.element_type(),
392391
ElementType::EmbeddedDocument,
393392
)),
394-
None => Err(Error::value_access_not_present(key)),
393+
None => Err(Error::value_access_not_present()),
395394
}
395+
.map_err(|e| e.with_key(key))
396396
}
397397

398398
/// Returns a reference to the value for the given key if one is present and is of type
@@ -402,12 +402,12 @@ impl Document {
402402
match self.get(key) {
403403
Some(&Bson::Boolean(v)) => Ok(v),
404404
Some(bson) => Err(Error::value_access_unexpected_type(
405-
key,
406405
bson.element_type(),
407406
ElementType::Boolean,
408407
)),
409-
None => Err(Error::value_access_not_present(key)),
408+
None => Err(Error::value_access_not_present()),
410409
}
410+
.map_err(|e| e.with_key(key))
411411
}
412412

413413
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -417,12 +417,12 @@ impl Document {
417417
match self.get_mut(key) {
418418
Some(&mut Bson::Boolean(ref mut v)) => Ok(v),
419419
Some(bson) => Err(Error::value_access_unexpected_type(
420-
key,
421420
bson.element_type(),
422421
ElementType::Boolean,
423422
)),
424-
None => Err(Error::value_access_not_present(key)),
423+
None => Err(Error::value_access_not_present()),
425424
}
425+
.map_err(|e| e.with_key(key))
426426
}
427427

428428
/// Returns [`Bson::Null`] if the given key corresponds to a [`Bson::Null`] value.
@@ -431,12 +431,12 @@ impl Document {
431431
match self.get(key) {
432432
Some(&Bson::Null) => Ok(Bson::Null),
433433
Some(bson) => Err(Error::value_access_unexpected_type(
434-
key,
435434
bson.element_type(),
436435
ElementType::Null,
437436
)),
438-
None => Err(Error::value_access_not_present(key)),
437+
None => Err(Error::value_access_not_present()),
439438
}
439+
.map_err(|e| e.with_key(key))
440440
}
441441

442442
/// Returns the value for the given key if one is present and is of type [`ElementType::Int32`].
@@ -445,12 +445,12 @@ impl Document {
445445
match self.get(key) {
446446
Some(&Bson::Int32(v)) => Ok(v),
447447
Some(bson) => Err(Error::value_access_unexpected_type(
448-
key,
449448
bson.element_type(),
450449
ElementType::Int32,
451450
)),
452-
None => Err(Error::value_access_not_present(key)),
451+
None => Err(Error::value_access_not_present()),
453452
}
453+
.map_err(|e| e.with_key(key))
454454
}
455455

456456
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -460,12 +460,12 @@ impl Document {
460460
match self.get_mut(key) {
461461
Some(&mut Bson::Int32(ref mut v)) => Ok(v),
462462
Some(bson) => Err(Error::value_access_unexpected_type(
463-
key,
464463
bson.element_type(),
465464
ElementType::Int32,
466465
)),
467-
None => Err(Error::value_access_not_present(key)),
466+
None => Err(Error::value_access_not_present()),
468467
}
468+
.map_err(|e| e.with_key(key))
469469
}
470470

471471
/// Returns the value for the given key if one is present and is of type [`ElementType::Int64`].
@@ -474,12 +474,12 @@ impl Document {
474474
match self.get(key) {
475475
Some(&Bson::Int64(v)) => Ok(v),
476476
Some(bson) => Err(Error::value_access_unexpected_type(
477-
key,
478477
bson.element_type(),
479478
ElementType::Int64,
480479
)),
481-
None => Err(Error::value_access_not_present(key)),
480+
None => Err(Error::value_access_not_present()),
482481
}
482+
.map_err(|e| e.with_key(key))
483483
}
484484

485485
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -489,12 +489,12 @@ impl Document {
489489
match self.get_mut(key) {
490490
Some(&mut Bson::Int64(ref mut v)) => Ok(v),
491491
Some(bson) => Err(Error::value_access_unexpected_type(
492-
key,
493492
bson.element_type(),
494493
ElementType::Int64,
495494
)),
496-
None => Err(Error::value_access_not_present(key)),
495+
None => Err(Error::value_access_not_present()),
497496
}
497+
.map_err(|e| e.with_key(key))
498498
}
499499

500500
/// Returns the value for the given key if one is present and is of type
@@ -504,12 +504,12 @@ impl Document {
504504
match self.get(key) {
505505
Some(&Bson::Timestamp(timestamp)) => Ok(timestamp),
506506
Some(bson) => Err(Error::value_access_unexpected_type(
507-
key,
508507
bson.element_type(),
509508
ElementType::Timestamp,
510509
)),
511-
None => Err(Error::value_access_not_present(key)),
510+
None => Err(Error::value_access_not_present()),
512511
}
512+
.map_err(|e| e.with_key(key))
513513
}
514514

515515
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -519,12 +519,12 @@ impl Document {
519519
match self.get_mut(key) {
520520
Some(&mut Bson::Timestamp(ref mut timestamp)) => Ok(timestamp),
521521
Some(bson) => Err(Error::value_access_unexpected_type(
522-
key,
523522
bson.element_type(),
524523
ElementType::Timestamp,
525524
)),
526-
None => Err(Error::value_access_not_present(key)),
525+
None => Err(Error::value_access_not_present()),
527526
}
527+
.map_err(|e| e.with_key(key))
528528
}
529529

530530
/// Returns a reference to the value for the given key if one is present and is of type
@@ -537,12 +537,12 @@ impl Document {
537537
ref bytes,
538538
})) => Ok(bytes),
539539
Some(bson) => Err(Error::value_access_unexpected_type(
540-
key,
541540
bson.element_type(),
542541
ElementType::Binary,
543542
)),
544-
None => Err(Error::value_access_not_present(key)),
543+
None => Err(Error::value_access_not_present()),
545544
}
545+
.map_err(|e| e.with_key(key))
546546
}
547547

548548
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -555,12 +555,12 @@ impl Document {
555555
ref mut bytes,
556556
})) => Ok(bytes),
557557
Some(bson) => Err(Error::value_access_unexpected_type(
558-
key,
559558
bson.element_type(),
560559
ElementType::Binary,
561560
)),
562-
None => Err(Error::value_access_not_present(key)),
561+
None => Err(Error::value_access_not_present()),
563562
}
563+
.map_err(|e| e.with_key(key))
564564
}
565565

566566
/// Returns the value for the given key if one is present and is of type
@@ -570,12 +570,12 @@ impl Document {
570570
match self.get(key) {
571571
Some(&Bson::ObjectId(v)) => Ok(v),
572572
Some(bson) => Err(Error::value_access_unexpected_type(
573-
key,
574573
bson.element_type(),
575574
ElementType::ObjectId,
576575
)),
577-
None => Err(Error::value_access_not_present(key)),
576+
None => Err(Error::value_access_not_present()),
578577
}
578+
.map_err(|e| e.with_key(key))
579579
}
580580

581581
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -585,12 +585,12 @@ impl Document {
585585
match self.get_mut(key) {
586586
Some(&mut Bson::ObjectId(ref mut v)) => Ok(v),
587587
Some(bson) => Err(Error::value_access_unexpected_type(
588-
key,
589588
bson.element_type(),
590589
ElementType::ObjectId,
591590
)),
592-
None => Err(Error::value_access_not_present(key)),
591+
None => Err(Error::value_access_not_present()),
593592
}
593+
.map_err(|e| e.with_key(key))
594594
}
595595

596596
/// Returns a reference to the value for the given key if one is present and is of type
@@ -600,12 +600,12 @@ impl Document {
600600
match self.get(key) {
601601
Some(Bson::DateTime(v)) => Ok(v),
602602
Some(bson) => Err(Error::value_access_unexpected_type(
603-
key,
604603
bson.element_type(),
605604
ElementType::DateTime,
606605
)),
607-
None => Err(Error::value_access_not_present(key)),
606+
None => Err(Error::value_access_not_present()),
608607
}
608+
.map_err(|e| e.with_key(key))
609609
}
610610

611611
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -615,12 +615,12 @@ impl Document {
615615
match self.get_mut(key) {
616616
Some(&mut Bson::DateTime(ref mut v)) => Ok(v),
617617
Some(bson) => Err(Error::value_access_unexpected_type(
618-
key,
619618
bson.element_type(),
620619
ElementType::DateTime,
621620
)),
622-
None => Err(Error::value_access_not_present(key)),
621+
None => Err(Error::value_access_not_present()),
623622
}
623+
.map_err(|e| e.with_key(key))
624624
}
625625

626626
/// Returns whether the map contains a value for the specified key.

0 commit comments

Comments
 (0)