From e59b35612848aa55112d693750c3b7a3f2d26af6 Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Thu, 9 Jun 2022 22:17:03 +0200 Subject: [PATCH] add errors --- Cargo.lock | 1 + Cargo.toml | 1 + src/api/errors.rs | 27 +++++++++++++++++++++++++++ src/api/mod.rs | 1 + src/api/routes.rs | 17 ++++++++++++----- 5 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 src/api/errors.rs diff --git a/Cargo.lock b/Cargo.lock index 3602dc46..22bab976 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -669,6 +669,7 @@ dependencies = [ "chrono 0.4.19 (git+https://github.com/sbrocket/chrono?branch=parse-error-kind-public)", "clap", "crossbeam-channel", + "derive_more", "faccess", "ffprobe", "file-rotate", diff --git a/Cargo.toml b/Cargo.toml index 71336f53..b15b56d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ argon2 = "0.4" chrono = { git = "https://github.com/sbrocket/chrono", branch = "parse-error-kind-public" } clap = { version = "3.1", features = ["derive"] } crossbeam-channel = "0.5" +derive_more = "0.99" faccess = "0.2" ffprobe = "0.3" file-rotate = { git = "https://github.com/Ploppz/file-rotate.git", branch = "timestamp-parse-fix" } diff --git a/src/api/errors.rs b/src/api/errors.rs new file mode 100644 index 00000000..a515c47a --- /dev/null +++ b/src/api/errors.rs @@ -0,0 +1,27 @@ +use actix_web::{error::ResponseError, HttpResponse}; +use derive_more::Display; + +#[derive(Debug, Display)] +pub enum ServiceError { + #[display(fmt = "Internal Server Error")] + InternalServerError, + + #[display(fmt = "BadRequest: {}", _0)] + BadRequest(String), + + #[display(fmt = "Unauthorized")] + Unauthorized, +} + +// impl ResponseError trait allows to convert our errors into http responses with appropriate data +impl ResponseError for ServiceError { + fn error_response(&self) -> HttpResponse { + match self { + ServiceError::InternalServerError => { + HttpResponse::InternalServerError().json("Internal Server Error. Please try later.") + } + ServiceError::BadRequest(ref message) => HttpResponse::BadRequest().json(message), + ServiceError::Unauthorized => HttpResponse::Unauthorized().json("No Permission!"), + } + } +} diff --git a/src/api/mod.rs b/src/api/mod.rs index d3beadd4..6ecc6917 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,5 +1,6 @@ pub mod args_parse; pub mod auth; +pub mod errors; pub mod handles; pub mod models; pub mod routes; diff --git a/src/api/routes.rs b/src/api/routes.rs index 1ce99fca..441ad993 100644 --- a/src/api/routes.rs +++ b/src/api/routes.rs @@ -6,6 +6,7 @@ use simplelog::*; use crate::api::{ auth::{create_jwt, Claims}, + errors::ServiceError, handles::{get_login, get_role}, models::{LoginUser, User}, }; @@ -17,21 +18,27 @@ struct ResponseObj { data: Option, } +/// curl -X GET http://127.0.0.1:8080/api/settings -H "Authorization: Bearer " #[get("/settings")] #[has_permissions("admin")] -async fn settings(user: web::ReqData) -> impl Responder { +async fn settings(user: web::ReqData) -> Result { println!("{:?}", user); - "Hello from settings!" + Ok("Hello from settings!") } #[put("/user/{user_id}")] #[has_permissions("admin")] -async fn update_user(user_id: web::Path, user: web::ReqData) -> impl Responder { +async fn update_user( + user_id: web::Path, + user: web::ReqData, + data: web::Json, +) -> Result { if user_id.into_inner() == user.id { - return "Update allow!"; + println!("{data:?}"); + return Ok("Update allow!"); } - "Wrong user!" + Err(ServiceError::Unauthorized) } /// curl -X POST -H "Content-Type: application/json" -d '{"username": "USER", "password": "abc123" }' \