Skip to content

Use blanket impls to add convenience methods to all errors#20

Open
sruggier wants to merge 1 commit intooxidecomputer:mainfrom
sruggier:pr/add-blanket-convenience-methods
Open

Use blanket impls to add convenience methods to all errors#20
sruggier wants to merge 1 commit intooxidecomputer:mainfrom
sruggier:pr/add-blanket-convenience-methods

Conversation

@sruggier
Copy link

This defines two traits, each defining a convenience method, as_inline_error_chain and as_array_error_chain respectively, as a more convenient way to construct InlineErrorChain and ArrayErrorChain adapters.

Finally, it also defines corresponding blanket implementations for each one, so they automatically apply to any value whose type implements std::error::Error. This makes it possible to use err.as_inline_error_chain() instead of InlineErrorChain::new(&err), and err.as_array_error_chain() instead of
ArrayErrorChain::new(&err).

This defines two traits, each defining a convenience method,
`as_inline_error_chain` and `as_array_error_chain` respectively, as a
more convenient way to construct `InlineErrorChain` and
`ArrayErrorChain` adapters.

Finally, it also defines corresponding blanket implementations for each
one, so they automatically apply to any value whose type implements
`std::error::Error`. This makes it possible to use
`err.as_inline_error_chain()` instead of `InlineErrorChain::new(&err)`,
and `err.as_array_error_chain()` instead of
`ArrayErrorChain::new(&err)`.
@sruggier
Copy link
Author

As discussed in oxidecomputer/omicron#9923. After playing with the idea a bit in a minimal test crate, I decided to go ahead and implement it. I made it return the adapter rather than producing a string directly, so that, true to the crate name, it would also be applicable in calls to slog macros.

@sruggier
Copy link
Author

To be clear, I don't see this as mutually exclusive with adding an extra string formatting method on the derive macros, I just happen to have tried this first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant