From 75f384d4ec8231d2802eacebd7944513d9779c8f Mon Sep 17 00:00:00 2001 From: "Dustin J. Mitchell" Date: Sun, 24 Nov 2024 22:29:01 -0500 Subject: [PATCH] Log Errors causing ISE's at the error level (#65) This will cause these to be displayed even without $RUST_LOG set. If RUST_LOG=debug, the message is printed twice (once at the debug level by the built-in `Logger` middleware), but this is better than not at all. --- server/src/bin/taskchampion-sync-server.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/server/src/bin/taskchampion-sync-server.rs b/server/src/bin/taskchampion-sync-server.rs index ecc1046..15fd9c3 100644 --- a/server/src/bin/taskchampion-sync-server.rs +++ b/server/src/bin/taskchampion-sync-server.rs @@ -1,6 +1,11 @@ #![deny(clippy::all)] -use actix_web::{middleware::Logger, App, HttpServer}; +use actix_web::{ + dev::ServiceResponse, + http::StatusCode, + middleware::{ErrorHandlerResponse, ErrorHandlers, Logger}, + App, HttpServer, +}; use clap::{arg, builder::ValueParser, value_parser, ArgAction, Command}; use std::{collections::HashSet, ffi::OsString}; use taskchampion_sync_server::WebServer; @@ -44,6 +49,13 @@ fn command() -> Command { ) } +fn print_error(res: ServiceResponse) -> actix_web::Result> { + if let Some(err) = res.response().error() { + log::error!("Internal Server Error caused by:\n{:?}", err); + } + Ok(ErrorHandlerResponse::Response(res.map_into_left_body())) +} + #[actix_web::main] async fn main() -> anyhow::Result<()> { env_logger::init(); @@ -66,6 +78,7 @@ async fn main() -> anyhow::Result<()> { log::info!("Serving on port {}", port); HttpServer::new(move || { App::new() + .wrap(ErrorHandlers::new().handler(StatusCode::INTERNAL_SERVER_ERROR, print_error)) .wrap(Logger::default()) .configure(|cfg| server.config(cfg)) })