Skip to content

Commit 8c80d80

Browse files
committed
editoast: enhance error logging
1 parent 13e1ab5 commit 8c80d80

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

editoast/src/core/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::marker::PhantomData;
1212

1313
use crate::error::Result;
1414
use async_trait::async_trait;
15+
use colored::{ColoredString, Colorize};
1516
use editoast_derive::EditoastError;
1617
pub use http_client::{HttpClient, HttpClientBuilder};
1718
use log::info;
@@ -22,6 +23,19 @@ use thiserror::Error;
2223

2324
const MAX_RETRIES: u8 = 5;
2425

26+
fn colored_method(method: &reqwest::Method) -> ColoredString {
27+
let m = method.as_str();
28+
match *method {
29+
reqwest::Method::GET => m.green(),
30+
reqwest::Method::POST => m.yellow(),
31+
reqwest::Method::PUT => m.blue(),
32+
reqwest::Method::PATCH => m.magenta(),
33+
reqwest::Method::DELETE => m.red(),
34+
_ => m.normal(),
35+
}
36+
.bold()
37+
}
38+
2539
#[derive(Debug, Clone)]
2640
pub enum CoreClient {
2741
Direct(HttpClient),
@@ -51,6 +65,9 @@ impl CoreClient {
5165
path: &str,
5266
body: Option<&B>,
5367
) -> Result<R::Response> {
68+
let method_s = colored_method(&method);
69+
log::info!(target: "editoast::coreclient", "{method_s} {path}");
70+
log::debug!(target: "editoast::coreclient", "Request content: {body}", body = body.and_then(|b| serde_json::to_string_pretty(b).ok()).unwrap_or_default());
5471
match self {
5572
CoreClient::Direct(client) => {
5673
let mut i_try = 0;
@@ -84,8 +101,11 @@ impl CoreClient {
84101
msg: err.to_string(),
85102
})?;
86103
if status.is_success() {
104+
log::info!(target: "editoast::coreclient", "{method_s} {path} {status}", status = status.to_string().bold().green());
87105
return R::from_bytes(bytes.as_ref());
88106
}
107+
108+
log::error!(target: "editoast::coreclient", "{method_s} {path} {status}", status = status.to_string().bold().red());
89109
// We try to deserialize the response as the standard Core error format
90110
// If that fails we try to return a generic error containing the raw error
91111
let core_error = <Json<CoreErrorPayload> as CoreResponse>::from_bytes(

editoast/src/error.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use actix_web::{error::JsonPayloadError, http::StatusCode, HttpResponse, ResponseError};
2+
use colored::Colorize;
23
use diesel::result::Error as DieselError;
34
use redis::RedisError;
45
use serde::{Deserialize, Serialize};
56
use serde_json::{json, Value};
7+
use std::backtrace::Backtrace;
68
use std::collections::HashMap;
79
use std::result::Result as StdResult;
810
use std::{
@@ -77,6 +79,12 @@ impl ResponseError for InternalError {
7779
}
7880

7981
fn error_response(&self) -> HttpResponse {
82+
log::error!(
83+
"[{}] {}: {}",
84+
self.error_type.bold(),
85+
self.message,
86+
Backtrace::capture() // won't log unless RUST_BACKTRACE=1
87+
);
8088
HttpResponse::build(self.status).json(self)
8189
}
8290
}

0 commit comments

Comments
 (0)