Skip to content

Commit 6d4d48e

Browse files
authored
add ResultExt::map_kind (#778)
1 parent 2b31e13 commit 6d4d48e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

sdk/core/src/error/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,18 @@ impl Display for Error {
234234
///
235235
/// This trait cannot be implemented on custom types and is meant for usage with `Result`
236236
pub trait ResultExt<T>: private::Sealed {
237+
/// Creates a new error with the specified kind
238+
fn map_kind(self, kind: ErrorKind) -> Result<T>
239+
where
240+
Self: Sized;
241+
242+
/// Creates a new error with the specified kind and message
237243
fn context<C>(self, kind: ErrorKind, message: C) -> Result<T>
238244
where
239245
Self: Sized,
240246
C: Into<Cow<'static, str>>;
241247

248+
/// Creates a new error with the specified kind and formatted message
242249
fn with_context<F, C>(self, kind: ErrorKind, f: F) -> Result<T>
243250
where
244251
Self: Sized,
@@ -256,6 +263,13 @@ impl<T, E> ResultExt<T> for std::result::Result<T, E>
256263
where
257264
E: std::error::Error + Send + Sync + 'static,
258265
{
266+
fn map_kind(self, kind: ErrorKind) -> Result<T>
267+
where
268+
Self: Sized,
269+
{
270+
self.map_err(|e| Error::new(kind, e))
271+
}
272+
259273
fn context<C>(self, kind: ErrorKind, message: C) -> Result<T>
260274
where
261275
Self: Sized,
@@ -400,4 +414,11 @@ mod tests {
400414
if error_code.as_deref() == Some("teepot")
401415
));
402416
}
417+
418+
#[test]
419+
fn set_result_kind() {
420+
let result = std::result::Result::<(), _>::Err(create_error());
421+
let result = result.map_kind(ErrorKind::Io);
422+
assert_eq!(&ErrorKind::Io, result.unwrap_err().kind());
423+
}
403424
}

0 commit comments

Comments
 (0)