add errors

This commit is contained in:
jb-alvarado 2022-06-09 22:17:03 +02:00
parent 51e75cb113
commit e59b356128
5 changed files with 42 additions and 5 deletions

1
Cargo.lock generated
View File

@ -669,6 +669,7 @@ dependencies = [
"chrono 0.4.19 (git+https://github.com/sbrocket/chrono?branch=parse-error-kind-public)", "chrono 0.4.19 (git+https://github.com/sbrocket/chrono?branch=parse-error-kind-public)",
"clap", "clap",
"crossbeam-channel", "crossbeam-channel",
"derive_more",
"faccess", "faccess",
"ffprobe", "ffprobe",
"file-rotate", "file-rotate",

View File

@ -16,6 +16,7 @@ argon2 = "0.4"
chrono = { git = "https://github.com/sbrocket/chrono", branch = "parse-error-kind-public" } chrono = { git = "https://github.com/sbrocket/chrono", branch = "parse-error-kind-public" }
clap = { version = "3.1", features = ["derive"] } clap = { version = "3.1", features = ["derive"] }
crossbeam-channel = "0.5" crossbeam-channel = "0.5"
derive_more = "0.99"
faccess = "0.2" faccess = "0.2"
ffprobe = "0.3" ffprobe = "0.3"
file-rotate = { git = "https://github.com/Ploppz/file-rotate.git", branch = "timestamp-parse-fix" } file-rotate = { git = "https://github.com/Ploppz/file-rotate.git", branch = "timestamp-parse-fix" }

27
src/api/errors.rs Normal file
View File

@ -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!"),
}
}
}

View File

@ -1,5 +1,6 @@
pub mod args_parse; pub mod args_parse;
pub mod auth; pub mod auth;
pub mod errors;
pub mod handles; pub mod handles;
pub mod models; pub mod models;
pub mod routes; pub mod routes;

View File

@ -6,6 +6,7 @@ use simplelog::*;
use crate::api::{ use crate::api::{
auth::{create_jwt, Claims}, auth::{create_jwt, Claims},
errors::ServiceError,
handles::{get_login, get_role}, handles::{get_login, get_role},
models::{LoginUser, User}, models::{LoginUser, User},
}; };
@ -17,21 +18,27 @@ struct ResponseObj<T> {
data: Option<T>, data: Option<T>,
} }
/// curl -X GET http://127.0.0.1:8080/api/settings -H "Authorization: Bearer <TOKEN>"
#[get("/settings")] #[get("/settings")]
#[has_permissions("admin")] #[has_permissions("admin")]
async fn settings(user: web::ReqData<LoginUser>) -> impl Responder { async fn settings(user: web::ReqData<LoginUser>) -> Result<impl Responder, ServiceError> {
println!("{:?}", user); println!("{:?}", user);
"Hello from settings!" Ok("Hello from settings!")
} }
#[put("/user/{user_id}")] #[put("/user/{user_id}")]
#[has_permissions("admin")] #[has_permissions("admin")]
async fn update_user(user_id: web::Path<i64>, user: web::ReqData<LoginUser>) -> impl Responder { async fn update_user(
user_id: web::Path<i64>,
user: web::ReqData<LoginUser>,
data: web::Json<User>,
) -> Result<impl Responder, ServiceError> {
if user_id.into_inner() == user.id { 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" }' \ /// curl -X POST -H "Content-Type: application/json" -d '{"username": "USER", "password": "abc123" }' \