surrealpatch/src/net/fail.rs

109 lines
4 KiB
Rust
Raw Normal View History

2022-01-14 17:13:44 +00:00
use crate::err::Error;
use serde::Serialize;
use warp::http::StatusCode;
#[derive(Serialize)]
struct Message {
code: u16,
#[serde(skip_serializing_if = "Option::is_none")]
details: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
information: Option<String>,
}
pub async fn recover(err: warp::Rejection) -> Result<impl warp::Reply, warp::Rejection> {
if err.is_not_found() {
Ok(warp::reply::with_status(
warp::reply::json(&Message {
code: 404,
details: Some("Requested resource not found".to_string()),
description: Some("The requested resource does not exist. Check that you have entered the url correctly.".to_string()),
2022-01-14 17:13:44 +00:00
information: None,
}),
StatusCode::NOT_FOUND,
))
} else if let Some(err) = err.find::<Error>() {
Ok(warp::reply::with_status(
warp::reply::json(&Message {
code: 400,
details: Some("Request problems detected".to_string()),
description: Some("There is a problem with your request. Refer to the documentation for further information.".to_string()),
2022-01-14 17:13:44 +00:00
information: Some(err.to_string()),
}),
StatusCode::BAD_REQUEST,
))
} else if err.find::<warp::reject::MethodNotAllowed>().is_some() {
2022-01-14 17:13:44 +00:00
Ok(warp::reply::with_status(
warp::reply::json(&Message {
code: 405,
details: Some("Request content length too large".to_string()),
description: Some("The requested http method is not allowed for this resource. Refer to the documentation for allowed methods.".to_string()),
2022-01-14 17:13:44 +00:00
information: None,
}),
StatusCode::METHOD_NOT_ALLOWED,
))
} else if err.find::<warp::reject::PayloadTooLarge>().is_some() {
2022-01-14 17:13:44 +00:00
Ok(warp::reply::with_status(
warp::reply::json(&Message {
code: 413,
details: Some("Request problems detected".to_string()),
description: Some("The request has exceeded the maximum payload size. Refer to the documentation for the request limitations.".to_string()),
2022-01-14 17:13:44 +00:00
information: None,
}),
StatusCode::PAYLOAD_TOO_LARGE,
))
} else if err.find::<warp::reject::UnsupportedMediaType>().is_some() {
2022-01-14 17:13:44 +00:00
Ok(warp::reply::with_status(
warp::reply::json(&Message {
code: 415,
details: Some("Unsupported content type requested".to_string()),
description: Some("The request needs to adhere to certain constraints. Refer to the documentation for supported content types.".to_string()),
2022-01-14 17:13:44 +00:00
information: None,
}),
StatusCode::UNSUPPORTED_MEDIA_TYPE,
))
} else if err.find::<warp::reject::MissingHeader>().is_some() {
2022-01-14 17:13:44 +00:00
Ok(warp::reply::with_status(
warp::reply::json(&Message {
code: 412,
details: Some("Request problems detected".to_string()),
description: Some("The request appears to be missing a required header. Refer to the documentation for request requirements.".to_string()),
2022-01-14 17:13:44 +00:00
information: None,
}),
StatusCode::PRECONDITION_FAILED,
))
} else if err.find::<warp::reject::InvalidQuery>().is_some() {
2022-01-14 17:13:44 +00:00
Ok(warp::reply::with_status(
warp::reply::json(&Message {
code: 501,
details: Some("Not Implemented".to_string()),
description: Some("The server either does not recognize the query, or it lacks the ability to fulfill the request.".to_string()),
2022-01-14 17:13:44 +00:00
information: None,
}),
StatusCode::NOT_IMPLEMENTED,
))
} else if err.find::<warp::reject::InvalidHeader>().is_some() {
2022-01-14 17:13:44 +00:00
Ok(warp::reply::with_status(
warp::reply::json(&Message {
code: 501,
details: Some("Not Implemented".to_string()),
description: Some("The server either does not recognize a request header, or it lacks the ability to fulfill the request.".to_string()),
2022-01-14 17:13:44 +00:00
information: None,
}),
StatusCode::NOT_IMPLEMENTED,
))
} else {
Ok(warp::reply::with_status(
warp::reply::json(&Message {
code: 500,
details: Some("Internal server error".to_string()),
description: Some("There was a problem with our servers, and we have been notified. Refer to the documentation for further information".to_string()),
2022-01-14 17:13:44 +00:00
information: None,
}),
StatusCode::INTERNAL_SERVER_ERROR,
))
}
}