Function opaque

Source
pub fn opaque<E>(err: E) -> impl Error
where E: Into<Box<dyn Error + Send + Sync>>,
Expand description

Wrap a value as a new Error, while hiding its source chain.

The value is used for formatting, but not exposed as the source.

§Usage

This allows continuing to provide all relevant debug information of an error source chain, without exposing the exact types contained within.

Imagine wrapping an error that resulted from some timeout. A user may consider retrying an operation if the error chain contains said timeout. But perhaps the timed out operation has already been retried a few times, and you wish to return an error that no longer matches some “is_timeout” check. You can make the error “opaque”, so that it still includes the timeout information in logs, but no longer is programmatically a “timeout”.

§Example

use std::error::Error;

let orig = errors::wrap("request failed", "timeout");

let err = errors::opaque(orig);

// Still prints all the information...
assert_eq!(format!("{:+}", err), "request failed: timeout");
// But is no longer programatically available.
assert!(err.source().is_none());