From 8562251821968b36dc88431bde92359f8aecd127 Mon Sep 17 00:00:00 2001 From: Adrian Date: Tue, 15 Aug 2023 15:01:45 +0300 Subject: [PATCH] Handle error for Post request (#18) --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/app_error.rs | 16 ++++++++++++++++ src/lib.rs | 1 + src/routes/full_url.rs | 17 +++++++---------- 5 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 src/app_error.rs diff --git a/Cargo.lock b/Cargo.lock index 87e2830..5370416 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -61,6 +61,12 @@ dependencies = [ "libc", ] +[[package]] +name = "anyhow" +version = "1.0.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f768393e7fabd388fe8409b13faa4d93ab0fef35db1508438dfdb066918bcf38" + [[package]] name = "async-trait" version = "0.1.68" @@ -203,6 +209,7 @@ dependencies = [ name = "briefly" version = "0.1.0" dependencies = [ + "anyhow", "axum", "base62", "chrono", diff --git a/Cargo.toml b/Cargo.toml index bab3a97..f432a79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ tracing-subscriber = "0.3.17" tower-http = { version = "0.4.3", features = ["trace"] } md5 = "0.7.0" base62 = "2.0.2" +anyhow = "1.0.73" [dependencies.sqlx] version = "0.7.1" diff --git a/src/app_error.rs b/src/app_error.rs new file mode 100644 index 0000000..7f09f04 --- /dev/null +++ b/src/app_error.rs @@ -0,0 +1,16 @@ +use axum::{ + http::StatusCode, + response::{IntoResponse, Response}, +}; + +pub struct AppError(pub anyhow::Error); + +impl IntoResponse for AppError { + fn into_response(self) -> Response { + ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Something went wrong: {}", self.0), + ) + .into_response() + } +} diff --git a/src/lib.rs b/src/lib.rs index 24d561a..a98bedd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod app_error; pub mod configuration; pub mod key_generator; pub mod model; diff --git a/src/routes/full_url.rs b/src/routes/full_url.rs index 10a088b..32522ee 100644 --- a/src/routes/full_url.rs +++ b/src/routes/full_url.rs @@ -1,6 +1,8 @@ use crate::{ - key_generator::generate, model::UrlRequestModel, schema::CreateShortUrlSchema, AppState, + app_error::AppError, key_generator::generate, model::UrlRequestModel, + schema::CreateShortUrlSchema, AppState, }; +use anyhow::anyhow; use axum::{extract::State, Json}; use chrono::Utc; use sqlx::Error; @@ -10,7 +12,7 @@ use uuid::Uuid; pub async fn full_url( State(data): State>, Json(payload): Json, -) -> String { +) -> Result { let mut retry_count = 3; while retry_count > 0 { @@ -18,22 +20,17 @@ pub async fn full_url( match query_result { Ok(request) => { - println!("okay received testing: {:?}", request); - return request.extension; + return Ok(request.extension); } Err(e) => { - println!("failed with error: {:?}", e); - //TODO : - For now assume is failing because of error code 23505, that stands for - //duplicate key if retry_count == 1 { - return "error".to_string(); + return Err(AppError(e.into())); } retry_count -= 1; } } } - - "internal server error".to_string() + Err(AppError(anyhow!("Something went wrong"))) } async fn insert_in_db(