2020-05-21 13:28:07 +00:00
|
|
|
use serde::Serialize;
|
|
|
|
|
|
|
|
/// HTTP request error
|
|
|
|
///
|
|
|
|
/// @author Pierre Hubert
|
|
|
|
#[derive(Serialize)]
|
2020-05-23 12:08:22 +00:00
|
|
|
pub struct InnerHTTPError {
|
2020-05-21 13:28:07 +00:00
|
|
|
pub code: u16,
|
|
|
|
pub message: String,
|
|
|
|
}
|
|
|
|
|
2020-05-23 12:08:22 +00:00
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct HttpError {
|
|
|
|
pub error: InnerHTTPError
|
|
|
|
}
|
2020-05-21 13:28:07 +00:00
|
|
|
|
2020-05-23 12:08:22 +00:00
|
|
|
impl HttpError {
|
2020-05-21 13:28:07 +00:00
|
|
|
/// Generate a 404 error
|
|
|
|
pub fn not_found(message: &str) -> HttpError {
|
|
|
|
HttpError {
|
2020-05-23 12:08:22 +00:00
|
|
|
error: InnerHTTPError {
|
|
|
|
code: 404,
|
|
|
|
message: message.to_string(),
|
|
|
|
}
|
2020-05-21 13:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate a 500 error
|
|
|
|
pub fn internal_error(message: &str) -> HttpError {
|
|
|
|
HttpError {
|
2020-05-23 12:08:22 +00:00
|
|
|
error: InnerHTTPError {
|
|
|
|
code: 500,
|
|
|
|
message: message.to_string(),
|
|
|
|
}
|
2020-05-21 13:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-23 08:14:21 +00:00
|
|
|
|
2020-05-23 17:17:48 +00:00
|
|
|
/// Generate a 400 error
|
2020-05-23 08:14:21 +00:00
|
|
|
pub fn bad_request(message: &str) -> HttpError {
|
2020-05-23 17:17:48 +00:00
|
|
|
HttpError {
|
|
|
|
error: InnerHTTPError {
|
|
|
|
code: 400,
|
|
|
|
message: message.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate a 401 error
|
|
|
|
pub fn forbidden(message: &str) -> HttpError {
|
2020-05-23 08:14:21 +00:00
|
|
|
HttpError {
|
2020-05-23 12:08:22 +00:00
|
|
|
error: InnerHTTPError {
|
|
|
|
code: 401,
|
|
|
|
message: message.to_string(),
|
|
|
|
}
|
2020-05-23 08:14:21 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-21 13:28:07 +00:00
|
|
|
}
|