Add delete handler and json-payload error response

This commit is contained in:
Tobias Reisinger 2021-11-08 13:11:20 +01:00
parent e6278176e4
commit 7254eddc6c
8 changed files with 162 additions and 51 deletions
src/handlers

View file

@ -1,16 +1,21 @@
use actix_web::http::StatusCode;
use actix_web::HttpResponse;
use serde::ser::SerializeStruct;
use serde::{Serialize, Serializer};
pub enum HandlerError {
BadUidError,
BadUid,
ProtectedSchedule
}
impl HandlerError {
fn to_code(&self) -> u32 {
fn get_code(&self) -> StatusCode {
match self {
HandlerError::BadUidError => 400
HandlerError::BadUid => StatusCode::BAD_REQUEST,
HandlerError::ProtectedSchedule => StatusCode::FORBIDDEN,
}
}
}
impl Serialize for HandlerError {
@ -19,7 +24,7 @@ impl Serialize for HandlerError {
S: Serializer,
{
let mut s = serializer.serialize_struct("error", 2)?;
s.serialize_field("code", &self.to_code())?;
s.serialize_field("code", &self.get_code().as_u16())?;
s.serialize_field("description", &String::from(self))?;
s.end()
}
@ -28,7 +33,14 @@ impl Serialize for HandlerError {
impl From<&HandlerError> for String {
fn from(err: &HandlerError) -> Self {
match err {
HandlerError::BadUidError => String::from("the uid is in a bad format"),
HandlerError::BadUid => String::from("the uid is in a bad format"),
HandlerError::ProtectedSchedule => String::from("the targeted schedule is protected"),
}
}
}
impl From<HandlerError> for HttpResponse {
fn from(err: HandlerError) -> Self {
HttpResponse::build(err.get_code()).json(err)
}
}