Skip to content

Commit ffd835d

Browse files
committed
editoast: enhance error logging
1 parent 488b282 commit ffd835d

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

editoast/src/core/mod.rs

+19
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,27 @@ 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 reqwest::Url;
1819
use serde::{de::DeserializeOwned, Serialize};
1920
use serde_derive::Deserialize;
2021
use thiserror::Error;
2122

23+
fn colored_method(method: &reqwest::Method) -> ColoredString {
24+
let m = method.as_str();
25+
match *method {
26+
reqwest::Method::GET => m.green(),
27+
reqwest::Method::POST => m.yellow(),
28+
reqwest::Method::PUT => m.blue(),
29+
reqwest::Method::PATCH => m.magenta(),
30+
reqwest::Method::DELETE => m.red(),
31+
_ => m.normal(),
32+
}
33+
.bold()
34+
}
35+
2236
#[derive(Debug, Clone)]
2337
pub enum CoreClient {
2438
Direct(HttpClient),
@@ -48,6 +62,9 @@ impl CoreClient {
4862
path: &str,
4963
body: Option<&B>,
5064
) -> Result<R::Response> {
65+
let method_s = colored_method(&method);
66+
log::info!(target: "editoast::coreclient", "{method_s} {path}");
67+
log::debug!(target: "editoast::coreclient", "Request content: {body}", body = body.and_then(|b| serde_json::to_string_pretty(b).ok()).unwrap_or_default());
5168
match self {
5269
CoreClient::Direct(client) => {
5370
let mut request = client.request(method, path);
@@ -65,8 +82,10 @@ impl CoreClient {
6582
msg: err.to_string(),
6683
})?;
6784
if status.is_success() {
85+
log::info!(target: "editoast::coreclient", "{method_s} {path} {status}", status = status.to_string().bold().green());
6886
R::from_bytes(bytes.as_ref())
6987
} else {
88+
log::error!(target: "editoast::coreclient", "{method_s} {path} {status}", status = status.to_string().bold().red());
7089
// We try to deserialize the response as the standard Core error format
7190
// If that fails we try to return a generic error containing the raw error
7291
let core_error =

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)