Emit our own errors

asonix/better-errors
Aode (lion) 2021-11-27 13:36:51 -06:00
parent ceba674b46
commit 476ac51b87
2 changed files with 33 additions and 2 deletions

View File

@ -50,7 +50,7 @@ serde = { version = "1.0.130", features = ["derive"] }
actix = "0.12.0"
actix-web = { version = "4.0.0-beta.9", default-features = false, features = ["rustls"] }
tracing = "0.1.29"
tracing-actix-web = "0.5.0-beta.3"
tracing-actix-web = { version = "0.5.0-beta.3", default-features = false }
tracing-error = "0.2.0"
tracing-log = "0.1.2"
tracing-subscriber = { version = "0.3.2", features = ["env-filter"] }

View File

@ -1,4 +1,4 @@
use actix_web::{http::StatusCode, ResponseError};
use actix_web::{dev::ServiceResponse, http::StatusCode, ResponseError};
use tracing::Span;
use tracing_actix_web::RootSpanBuilder;
@ -34,6 +34,8 @@ impl RootSpanBuilder for QuieterRootSpanBuilder {
span: tracing::Span,
outcome: &Result<actix_web::dev::ServiceResponse<B>, actix_web::Error>,
) {
emit_event_on_error::<B>(outcome);
match &outcome {
Ok(response) => {
if let Some(error) = response.response().error() {
@ -69,3 +71,32 @@ fn handle_error(span: Span, status_code: StatusCode, response_error: &dyn Respon
span.record("otel.status_code", &"ERROR");
}
}
fn emit_event_on_error<B>(outcome: &Result<ServiceResponse<B>, actix_web::Error>) {
match outcome {
Ok(response) => {
if let Some(err) = response.response().error() {
// use the status code already constructed for the outgoing HTTP response
emit_error_event(err.as_response_error(), response.status())
}
}
Err(error) => {
let response_error = error.as_response_error();
emit_error_event(response_error, response_error.status_code())
}
}
}
fn emit_error_event(response_error: &dyn ResponseError, status_code: StatusCode) {
let span = tracing::info_span!(
parent: None,
"Error encountered while processing the incoming HTTP request"
);
let entered = span.enter();
if status_code.is_client_error() {
tracing::warn!("{}\n{:?}", response_error, response_error);
} else {
tracing::error!("{}\n{:?}", response_error, response_error);
}
drop(entered);
}