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

57 lines
1.2 KiB
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
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
}