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

47 lines
1017 B
Rust

use serde::Serialize;
/// HTTP request error
///
/// @author Pierre Hubert
#[derive(Serialize)]
pub struct InnerHTTPError {
pub code: u16,
pub message: String,
}
#[derive(Serialize)]
pub struct HttpError {
pub error: InnerHTTPError
}
impl HttpError {
/// Construct a new custom error
pub fn new(code: u16, message: &str) -> HttpError {
HttpError {
error: InnerHTTPError {
code,
message: message.to_string(),
}
}
}
/// Generate a 404 error
pub fn not_found(message: &str) -> HttpError {
HttpError::new(404, message)
}
/// Generate a 500 error
pub fn internal_error(message: &str) -> HttpError {
HttpError::new(500, message)
}
/// Generate a 400 error
pub fn bad_request(message: &str) -> HttpError {
HttpError::new(400, message)
}
/// Generate a 401 error
pub fn forbidden(message: &str) -> HttpError {
HttpError::new(401, message)
}
}