pub enum Error {
MissingExtension,
OverlappingExtractors,
Database {
error: Error,
},
}Expand description
Possible errors when extracting Tx from a request.
Errors can occur at two points during the request lifecycle:
-
The
Txextractor might fail to obtain a connection from the pool andBEGINa transaction. This could be due to:- Forgetting to add the middleware:
Error::MissingExtension. - Calling the extractor multiple times in the same request:
Error::OverlappingExtractors. - A problem communicating with the database:
Error::Database.
- Forgetting to add the middleware:
-
The middleware
Layermight fail to commit the transaction. This could be due to a problem communicating with the database, or else a logic error (e.g. unsatisfied deferred constraint):Error::Database.
axum requires that errors can be turned into responses. The Error type converts into a
HTTP 500 response with the error message as the response body. This may be suitable for
development or internal services but it’s generally not advisable to return internal error
details to clients.
You can override the error types for both the Tx extractor and Layer:
-
Override the
Tx<DB, E>error type using theEgeneric type parameter.Emust be convertible fromError(e.g.Error: Into<E>). -
Override the
Layererror type usingConfig::layer_error. The layer error type must be convertible fromsqlx::Error(e.g.sqlx::Error: Into<LayerError>).
In both cases, the error type must implement axum::response::IntoResponse.
use axum::{response::IntoResponse, routing::post};
enum MyError{
Extractor(axum_sqlx_tx::Error),
Layer(sqlx::Error),
}
impl From<axum_sqlx_tx::Error> for MyError {
fn from(error: axum_sqlx_tx::Error) -> Self {
Self::Extractor(error)
}
}
impl From<sqlx::Error> for MyError {
fn from(error: sqlx::Error) -> Self {
Self::Layer(error)
}
}
impl IntoResponse for MyError {
fn into_response(self) -> axum::response::Response {
// note that you would probably want to log the error as well
(http::StatusCode::INTERNAL_SERVER_ERROR, "internal server error").into_response()
}
}
// Override the `Tx` error type using the second generic type parameter
type Tx = axum_sqlx_tx::Tx<sqlx::Sqlite, MyError>;
let pool = sqlx::SqlitePool::connect("...").await.unwrap();
let (state, layer) = Tx::config(pool)
// Override the `Layer` error type using the `Config` API
.layer_error::<MyError>()
.setup();Variants§
MissingExtension
Indicates that the Layer middleware was not installed.
OverlappingExtractors
Indicates that Tx was extracted multiple times in a single
handler/middleware.
Database
A database error occurred when starting or committing the transaction.
Trait Implementations§
Source§impl Error for Error
impl Error for Error
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§impl IntoResponse for Error
impl IntoResponse for Error
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Auto Trait Implementations§
impl Freeze for Error
impl !RefUnwindSafe for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl !UnwindSafe for Error
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more