add errors
This commit is contained in:
parent
51e75cb113
commit
e59b356128
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -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",
|
||||
|
@ -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" }
|
||||
|
27
src/api/errors.rs
Normal file
27
src/api/errors.rs
Normal 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!"),
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
pub mod args_parse;
|
||||
pub mod auth;
|
||||
pub mod errors;
|
||||
pub mod handles;
|
||||
pub mod models;
|
||||
pub mod routes;
|
||||
|
@ -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<T> {
|
||||
data: Option<T>,
|
||||
}
|
||||
|
||||
/// curl -X GET http://127.0.0.1:8080/api/settings -H "Authorization: Bearer <TOKEN>"
|
||||
#[get("/settings")]
|
||||
#[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);
|
||||
"Hello from settings!"
|
||||
Ok("Hello from settings!")
|
||||
}
|
||||
|
||||
#[put("/user/{user_id}")]
|
||||
#[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 {
|
||||
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" }' \
|
||||
|
Loading…
Reference in New Issue
Block a user