-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c873f6
commit 067b280
Showing
9 changed files
with
98 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,42 @@ | ||
use std::sync::Arc; | ||
|
||
use afire::{headers::Vary, route::RouteError, Content, HeaderName, Request, Response, Server}; | ||
use afire::{error::AnyResult, headers::Vary, route::RouteError, Content, Context, HeaderName}; | ||
use serde_json::json; | ||
|
||
use crate::app::App; | ||
|
||
pub fn error_handler(_server: Arc<Server<App>>, req: Arc<Request>, error: RouteError) -> Response { | ||
if req | ||
pub fn error_handler(ctx: &Context<App>, error: RouteError) -> AnyResult<()> { | ||
if ctx | ||
.req | ||
.headers | ||
.get(HeaderName::Accept) | ||
.map(|x| x == "application/json") | ||
.unwrap_or(false) | ||
{ | ||
Response::new() | ||
.text(json!({ | ||
"message": error.message, | ||
"location": error.location.map(|x| x.to_string()), | ||
"error": error.error.map(|x| format!("{x:?}")), | ||
})) | ||
.content(Content::JSON) | ||
ctx.text(json!({ | ||
"message": error.message, | ||
"location": error.location.map(|x| x.to_string()), | ||
"error": error.error.map(|x| format!("{x:?}")), | ||
})) | ||
.content(Content::JSON) | ||
} else { | ||
Response::new() | ||
.text(format!( | ||
"Internal Server Error\n{}{}{}", | ||
error.message, | ||
error | ||
.error | ||
.map(|x| format!("\n{:?}", x)) | ||
.unwrap_or_default(), | ||
error | ||
.location | ||
.map(|x| format!("\n{}", x)) | ||
.unwrap_or_default(), | ||
)) | ||
.content(Content::TXT) | ||
} | ||
.header(Vary::headers([HeaderName::Accept])) | ||
.status(error.status) | ||
.headers(error.headers) | ||
ctx.text(format!( | ||
"Internal Server Error\n{}{}{}", | ||
error.message, | ||
error | ||
.error | ||
.map(|x| format!("\n{:?}", x)) | ||
.unwrap_or_default(), | ||
error | ||
.location | ||
.map(|x| format!("\n{}", x)) | ||
.unwrap_or_default(), | ||
)) | ||
.content(Content::TXT) | ||
}; | ||
|
||
ctx.header(Vary::headers([HeaderName::Accept])) | ||
.status(error.status) | ||
.headers(error.headers) | ||
.send()?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters