pub struct JsonError {
pub status: StatusCode,
pub message: String,
}Expand description
A structured JSON error response.
Serializes to {"error": {"status": 400, "message": "..."}}.
§Example
use typeway_server::error::JsonError;
use typeway_server::Json;
#[derive(serde::Serialize)]
struct User { id: u32 }
async fn get_user() -> Result<Json<User>, JsonError> {
// Return a structured JSON error on failure:
Err(JsonError::not_found("user not found"))
}Fields§
§status: StatusCode§message: StringImplementations§
Source§impl JsonError
impl JsonError
Sourcepub fn new(status: StatusCode, message: impl Into<String>) -> Self
pub fn new(status: StatusCode, message: impl Into<String>) -> Self
Create a new error with the given status code and message.
Sourcepub fn bad_request(message: impl Into<String>) -> Self
pub fn bad_request(message: impl Into<String>) -> Self
400 Bad Request.
401 Unauthorized.
Sourcepub fn unprocessable(message: impl Into<String>) -> Self
pub fn unprocessable(message: impl Into<String>) -> Self
422 Unprocessable Entity.
Trait Implementations§
Source§impl Error for JsonError
impl Error for JsonError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0:
use the Display impl or to_string()
Source§impl From<(StatusCode, String)> for JsonError
Implement From<(StatusCode, String)> so existing extractor errors
can be converted to JsonError automatically.
impl From<(StatusCode, String)> for JsonError
Implement From<(StatusCode, String)> so existing extractor errors
can be converted to JsonError automatically.
Source§fn from((status, message): (StatusCode, String)) -> Self
fn from((status, message): (StatusCode, String)) -> Self
Converts to this type from the input type.
Source§impl IntoResponse for JsonError
impl IntoResponse for JsonError
Source§fn into_response(self) -> Response<BoxBody>
fn into_response(self) -> Response<BoxBody>
Convert this value into an HTTP response.
Auto Trait Implementations§
impl Freeze for JsonError
impl RefUnwindSafe for JsonError
impl Send for JsonError
impl Sync for JsonError
impl Unpin for JsonError
impl UnsafeUnpin for JsonError
impl UnwindSafe for JsonError
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
Mutably borrows from an owned value. Read more