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.
This commit is contained in:
Dustin J. Mitchell 2024-11-24 22:29:01 -05:00 committed by GitHub
parent 895bb433e0
commit 75f384d4ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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<B>(res: ServiceResponse<B>) -> actix_web::Result<ErrorHandlerResponse<B>> {
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))
})