depolymerization

wire gateway for Bitcoin/Ethereum
Log | Files | Refs | Submodules | README | LICENSE

commit 41ad5070a43da25419d8dd0042d35c82ddc053fc
parent f9fe9248934080e57b5359dab4b53d0b1852f9b5
Author: Antoine A <>
Date:   Thu,  9 Dec 2021 18:20:38 +0100

Improve error logging

Diffstat:
MCargo.lock | 24++++++++++++------------
Mwire-gateway/src/error.rs | 61+++++++++++++++++++++++++++++++++++++------------------------
Mwire-gateway/src/main.rs | 16++++++++--------
3 files changed, 57 insertions(+), 44 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -19,9 +19,9 @@ dependencies = [ [[package]] name = "argh" -version = "0.1.6" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f023c76cd7975f9969f8e29f0e461decbdc7f51048ce43427107a3d192f1c9bf" +checksum = "dbb41d85d92dfab96cb95ab023c265c5e4261bb956c0fb49ca06d90c570f1958" dependencies = [ "argh_derive", "argh_shared", @@ -29,9 +29,9 @@ dependencies = [ [[package]] name = "argh_derive" -version = "0.1.6" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48ad219abc0c06ca788aface2e3a1970587e3413ab70acd20e54b6ec524c1f8f" +checksum = "be69f70ef5497dd6ab331a50bd95c6ac6b8f7f17a7967838332743fbd58dc3b5" dependencies = [ "argh_shared", "heck", @@ -42,15 +42,15 @@ dependencies = [ [[package]] name = "argh_shared" -version = "0.1.6" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38de00daab4eac7d753e97697066238d67ce9d7e2d823ab4f72fe14af29f3f33" +checksum = "e6f8c380fa28aa1b36107cd97f0196474bb7241bb95a453c5c01a15ac74b2eac" [[package]] name = "async-trait" -version = "0.1.51" +version = "0.1.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44318e776df68115a881de9a8fd1b9e53368d7a4a5ce4cc48517da3393233a5e" +checksum = "061a7acccaa286c011ddc30970520b98fa40e00c9d644633fb26b5fc63a265e3" dependencies = [ "proc-macro2", "quote", @@ -1214,9 +1214,9 @@ checksum = "568a8e6258aa33c13358f81fd834adb854c6f7c9468520910a9b1e8fac068012" [[package]] name = "serde" -version = "1.0.130" +version = "1.0.131" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913" +checksum = "b4ad69dfbd3e45369132cc64e6748c2d65cdfb001a2b1c232d128b4ad60561c1" dependencies = [ "serde_derive", ] @@ -1233,9 +1233,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.130" +version = "1.0.131" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b" +checksum = "b710a83c4e0dff6a3d511946b95274ad9ca9e5d3ae497b63fda866ac955358d2" dependencies = [ "proc-macro2", "quote", diff --git a/wire-gateway/src/error.rs b/wire-gateway/src/error.rs @@ -1,35 +1,46 @@ use hyper::{Body, Response, StatusCode}; -use taler_log::log::error; use wire_gateway::{api_common::ErrorDetail, error_codes::ErrorCode}; /// Generic http error #[derive(Debug)] pub struct ServerError { - status: StatusCode, - body: Body, + pub status: StatusCode, + pub body: Body, + pub msg: String, +} + +pub enum ServerContent { + None, + Detailed(ErrorDetail), } impl ServerError { - fn new(status: StatusCode, body: Body) -> Self { - Self { status, body } + fn new(status: StatusCode, body: Body, msg: String) -> Self { + Self { status, body, msg } } - pub fn response(self) -> Response<Body> { - Response::builder() - .status(self.status) - .body(self.body) - .unwrap() + pub fn response(self) -> (Response<Body>, String) { + ( + Response::builder() + .status(self.status) + .body(self.body) + .unwrap(), + self.msg, + ) } pub fn unexpected<E: std::error::Error>(e: E) -> Self { - error!("unexpected: {}", e); - Self::new(StatusCode::INTERNAL_SERVER_ERROR, Body::empty()) + Self::new( + StatusCode::INTERNAL_SERVER_ERROR, + Body::empty(), + format!("unexpected: {}", e), + ) } - fn detail(status: StatusCode, code: ErrorCode) -> Self { - let detail = &ErrorDetail { + fn detail(status: StatusCode, code: ErrorCode, msg: String) -> Self { + let detail = ErrorDetail { code: code as i64, - hint: None, + hint: Some(msg.clone()), detail: None, parameter: None, path: None, @@ -41,23 +52,25 @@ impl ServerError { type_actual: None, }; match serde_json::to_vec(&detail) { - Ok(json) => Self::new(status, Body::from(json)), + Ok(json) => Self::new(status, Body::from(json), msg), Err(e) => Self::unexpected(e), } } pub fn code(status: StatusCode, code: ErrorCode) -> Self { - error!( - "standard {}: {}", - code as i64, - status.canonical_reason().unwrap_or("") - ); - Self::detail(status, code) + Self::detail( + status, + code, + format!( + "standard {}: {}", + code as i64, + status.canonical_reason().unwrap_or("") + ), + ) } pub fn catch_code<E: std::error::Error>(e: E, status: StatusCode, code: ErrorCode) -> Self { - error!("standard {}: {}", code as i64, e); - Self::detail(status, code) + Self::detail(status, code, format!("standard {}: {}", code as i64, e)) } } diff --git a/wire-gateway/src/main.rs b/wire-gateway/src/main.rs @@ -5,7 +5,7 @@ use hyper::{ Body, Error, Method, Response, Server, StatusCode, }; use json::parse_body; -use std::{process::exit, str::FromStr, time::Instant}; +use std::{process::exit, str::FromStr, time::Instant, convert::Infallible}; use taler_log::log::{error, info, log, Level}; use tokio_postgres::{Client, NoTls}; use url::Url; @@ -48,14 +48,13 @@ async fn main() { let state: &'static ServerState = Box::leak(Box::new(state)); let addr = ([0, 0, 0, 0], 8080).into(); let make_service = make_service_fn(move |_| async move { - Ok::<_, Error>(service_fn(move |req| async move { + Ok::<_, Infallible>(service_fn(move |req| async move { let start = Instant::now(); let (parts, body) = req.into_parts(); - let response = match router(&parts, body, state).await { - Ok(resp) => resp, + let (response, msg) = match router(&parts, body, state).await { + Ok(resp) => (resp, String::new()), Err(err) => err.response(), }; - // TODO log error message inlined into response log OR use a request id to link error message and response let status = response.status().as_u16(); let level = if status >= 500 { Level::Error @@ -66,13 +65,14 @@ async fn main() { }; log!( level, - "{} {} {:>2}ms {}", + "{} {} {:>2}ms {} - {}", parts.method, status, start.elapsed().as_millis(), - parts.uri.path() + parts.uri.path(), + msg ); - Ok::<Response<Body>, Error>(response) + Ok::<Response<Body>, Infallible>(response) })) });