pavex/error/error_.rs
1// Most of this module is a direct copy (with, from time to time,
2// minor modifications) of the corresponding `error` module in
3// `axum-core`.
4//
5// Copyright (c) 2019 Axum Contributors
6//
7// Permission is hereby granted, free of charge, to any
8// person obtaining a copy of this software and associated
9// documentation files (the "Software"), to deal in the
10// Software without restriction, including without
11// limitation the rights to use, copy, modify, merge,
12// publish, distribute, sublicense, and/or sell copies of
13// the Software, and to permit persons to whom the Software
14// is furnished to do so, subject to the following
15// conditions:
16//
17// The above copyright notice and this permission notice
18// shall be included in all copies or substantial portions
19// of the Software.
20//
21// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
22// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
23// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
24// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
25// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
26// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
28// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30use crate::{Response, methods};
31use std::fmt;
32
33/// Pavex's error type: an opaque wrapper around the concrete error type
34/// return by your components (e.g. request handlers, constructors, etc.).
35/// It is used as an input parameter by
36/// [error observers](https://pavex.dev/docs/guide/errors/error_observers/) and
37/// [universal error handlers](https://pavex.dev/docs/guide/errors/error_handlers/#universal).
38///
39/// # Guide
40///
41/// Check out [the guide](https://pavex.dev/docs/guide/errors/)
42/// for a thorough introduction to error handling and reporting in Pavex.
43///
44/// # Implementation details
45///
46/// It's a thin shim over `Box<dyn std::error::Error + Send + Sync>`.
47#[derive(Debug)]
48pub struct Error {
49 inner: Box<dyn std::error::Error + Send + Sync>,
50}
51
52#[methods]
53impl Error {
54 /// Create a new [`Error`] from a boxable error.
55 pub fn new<E>(error: E) -> Self
56 where
57 E: Into<Box<dyn std::error::Error + Send + Sync>>,
58 {
59 Self {
60 inner: error.into(),
61 }
62 }
63
64 /// Convert [`Error`] back into the underlying boxed error.
65 pub fn into_inner(self) -> Box<dyn std::error::Error + Send + Sync> {
66 self.inner
67 }
68
69 /// Return a reference to the underlying boxed error.
70 pub fn inner_ref(&self) -> &(dyn std::error::Error + Send + Sync) {
71 &*self.inner
72 }
73
74 /// Return an opaque `500 Internal Server Error` to the caller.
75 ///
76 /// It is used as the default error handler for [`pavex::Error`][`Error`].
77 ///
78 /// # Guide
79 ///
80 /// Check out [the "Fallback error handler" section of the guide](https://pavex.dev/docs/guide/errors/error_handlers/#fallback-error-handler)
81 /// for more details on the special role played by the error handler for [`pavex::Error`][`Error`] in Pavex.
82 #[error_handler(pavex = crate)]
83 pub fn to_response(&self) -> Response {
84 Response::internal_server_error()
85 }
86}
87
88impl fmt::Display for Error {
89 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90 self.inner.fmt(f)
91 }
92}
93
94impl std::error::Error for Error {
95 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
96 Some(&*self.inner)
97 }
98}