Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ rust-argon2 = "0.8.1"
uuid = { version = "0.8.1", features = ["v4"] }

# async futures
futures = "0.3.4"
futures = "0.3.4"

# for errors
thiserror = "1.0.18"
6 changes: 6 additions & 0 deletions http/errors.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
GET http://127.0.0.1:8000/error/default/Cargo.tomls
Accept: application/json

###
GET http://127.0.0.1:8000/error/custom/Cargo.toml
Accept: application/json
29 changes: 29 additions & 0 deletions src/errors/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use actix_web::{
web::{get, scope, Path},
Error, HttpResponse, Scope,
};
use serde::Deserialize;
use crate::utils::errors;

#[derive(Deserialize)]
struct Info {
#[serde(rename(deserialize = "fileName"))]
file_name: String,
}

async fn do_something(info: Path<Info>) -> Result<HttpResponse, Error> {
let contents = std::fs::read_to_string(&info.file_name)?;
Ok(HttpResponse::Ok().body(contents))
}

async fn with_custom_message(info: Path<Info>) -> Result<HttpResponse, Error> {
let lol = std::fs::read_to_string(&info.file_name)
.map_err(|_e| errors::CustomError::NoFileFound(format!("no file found with name {}", &info.file_name)))?;
Ok(HttpResponse::Ok().body(lol))
}

pub fn register_error_handlers() -> Scope {
scope("/error")
.route("/default/{fileName}", get().to(do_something))
.route("/custom/{fileName}", get().to(with_custom_message))
}
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ mod private;
// middleware
mod middleware;


// errors example
mod errors;
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
// loading .env file
Expand All @@ -41,6 +44,7 @@ async fn main() -> std::io::Result<()> {
.data(redis_client.clone())
.service(auth::auth_routes())
.service(Json::json_routes())
.service(errors::register_error_handlers())
.service(private::register_private()
.wrap(middleware::private::CheckToken))
}).bind("127.0.0.1:8000")?.run().await
Expand Down
30 changes: 30 additions & 0 deletions src/utils/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use thiserror::Error;
use actix_web::{HttpResponse, ResponseError};
use std::io;

#[derive(Error, Debug)]
pub enum CustomError {
#[error("unable to find file `{0}`")]
Disconnect(#[from] io::Error),
#[error("`{0}`")]
NoFileFound(String),
}


/// Actix web uses `ResponseError` for conversion of errors to a response
impl ResponseError for CustomError {
fn error_response(&self) -> HttpResponse {
match self {
CustomError::Disconnect(error) => {
HttpResponse::BadRequest().body(error.to_string())
}
CustomError::NoFileFound(error) => {
HttpResponse::BadRequest().body(error)
}
_ => {
println!("do some stuff related to CustomFour error");
HttpResponse::InternalServerError().into()
}
}
}
}
3 changes: 2 additions & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod redis_utils;
pub mod uuid_utils;
pub mod password_hash;
pub mod response;
pub mod response;
pub mod errors;