async fn handle_internal_errors(
    future: impl Future<Output = Response>,
) -> Result<Response, Infallible> {
    fn internal_server_error() -> Response {
        Response::builder()
            .status(StatusCode::INTERNAL_SERVER_ERROR)
            .body("Internal server error".into())
            .unwrap()
    }

    // TODO: We want to log lots of information about the exact HTTP request in
    // the error case.

    // The `AssertUnwindSafe` is unfortunately necessary. The whole story of
    // unwind safety is strange. What we are basically saying here is: "if the
    // future panicks, the global/remaining application state is not 'broken'.
    // It is safe to continue with the program in case of a panic."
    //
    // Hyper catches panics for us anyway, so this changes nothing except that
    // our response is better.
    match AssertUnwindSafe(future).catch_unwind().await {
        Ok(response) => Ok(response),
        Err(panic) => {
            // The `panic` information is just an `Any` object representing the
            // value the panic was invoked with. For most panics (which use
            // `panic!` like `println!`), this is either `&str` or `String`.
            let msg = panic.downcast_ref::<String>()
                .map(|s| s.as_str())
                .or(panic.downcast_ref::<&str>().map(|s| *s));

            // TODO: It would be great to also log everything the panic hook
            // would print, namely: location information and a backtrace. Do we
            // install our own panic hook? Or is stdout piped into the log file
            // anyway?
            match msg {
                Some(msg) => error!("INTERNAL SERVER ERROR: HTTP handler panicked: '{}'", msg),
                None => error!("INTERNAL SERVER ERROR: HTTP handler panicked"),
            }

            Ok(internal_server_error())
        }
    }
}
/// This just wraps another future and catches all panics that might occur when
/// resolving/polling that given future. This ensures that we always answer with
/// `500` instead of just crashing the thread and closing the connection.
This just wraps another future and catches all panics that might occur when
resolving/polling that given future. This ensures that we always answer with
`500` instead of just crashing the thread and closing the connection.
This just wraps another future and catches all panics that might occur when resolving / polling that given future . This ensures that we always answer with ` 500 ` instead of just crashing the thread and closing the connection .
async fn handle_internal_errors(
    future: impl Future<Output = Response>,
) -> Result<Response, Infallible> {
    fn internal_server_error() -> Response {
        Response::builder()
            .status(StatusCode::INTERNAL_SERVER_ERROR)
            .body("Internal server error".into())
            .unwrap()
    }
    // TODO: We want to log lots of information about the exact HTTP request in
    // the error case.
    // The `AssertUnwindSafe` is unfortunately necessary. The whole story of
    // unwind safety is strange. What we are basically saying here is: "if the
    // future panicks, the global/remaining application state is not 'broken'.
    // It is safe to continue with the program in case of a panic."
    //
    // Hyper catches panics for us anyway, so this changes nothing except that
    // our response is better.
    match AssertUnwindSafe(future).catch_unwind().await {
        Ok(response) => Ok(response),
        Err(panic) => {
            // The `panic` information is just an `Any` object representing the
            // value the panic was invoked with. For most panics (which use
            // `panic!` like `println!`), this is either `&str` or `String`.
            let msg = panic.downcast_ref::<String>()
                .map(|s| s.as_str())
                .or(panic.downcast_ref::<&str>().map(|s| *s));
            // TODO: It would be great to also log everything the panic hook
            // would print, namely: location information and a backtrace. Do we
            // install our own panic hook? Or is stdout piped into the log file
            // anyway?
            match msg {
                Some(msg) => error!("INTERNAL SERVER ERROR: HTTP handler panicked: '{}'", msg),
                None => error!("INTERNAL SERVER ERROR: HTTP handler panicked"),
            }
            Ok(internal_server_error())
        }
    }
}
async fn handle_internal_errors ( future : impl Future < Output = Response > , ) -> Result < Response , Infallible > { fn internal_server_error ( ) -> Response { Response :: builder ( ) . status ( StatusCode :: INTERNAL_SERVER_ERROR ) . body ( " " . into ( ) ) . unwrap ( ) } // TODO: We want to log lots of information about the exact HTTP request in // the error case. // The `AssertUnwindSafe` is unfortunately necessary. The whole story of // unwind safety is strange. What we are basically saying here is: "if the // future panicks, the global/remaining application state is not 'broken'. // It is safe to continue with the program in case of a panic." // // Hyper catches panics for us anyway, so this changes nothing except that // our response is better. match AssertUnwindSafe ( future ) . catch_unwind ( ) . await { Ok ( response ) => Ok ( response ) , Err ( panic ) => { // The `panic` information is just an `Any` object representing the // value the panic was invoked with. For most panics (which use // `panic!` like `println!`), this is either `&str` or `String`. let msg = panic . downcast_ref :: < String > ( ) . map ( | s | s . as_str ( ) ) . or ( panic . downcast_ref :: < & str > ( ) . map ( | s | * s ) ) ; // TODO: It would be great to also log everything the panic hook // would print, namely: location information and a backtrace. Do we // install our own panic hook? Or is stdout piped into the log file // anyway? match msg { Some ( msg ) => error ! ( " " msg ) , None => error ! ( " " ) , } Ok ( internal_server_error ( ) ) } } }
This just wraps another future and catches all panics that might occur when
resolving/polling that given future.
This just wraps another future and catches all panics that might occur when resolving / polling that given future .
future
impl Future<Output = Response>