1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-27 15:59:21 +00:00
comunicapiv3/src/data/http_error.rs

47 lines
993 B
Rust
Raw Normal View History

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
/// Generate a 401 error
pub fn bad_request(message: &str) -> HttpError {
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
}