SaturnFramework / Saturn

Opinionated, web development framework for F# which implements the server-side, functional MVC pattern
https://saturnframework.org
MIT License
703 stars 108 forks source link

Default Controller ErrorHandler does not preserve original stack trace #280

Closed retendo closed 3 years ago

retendo commented 3 years ago

Hi,

I just recognized that the default Controller exception handling is not super useful.

By default, the ErrorHandler in Controllers just does:

fun _ ex -> raise ex

Here: https://github.com/SaturnFramework/Saturn/blob/8e2761440841a51d5cf845cf8ba359714867398f/src/Saturn/Controller.fs#L110


This completely erases the original/useful stack trace. It's a problem when you want to do top level exception handling.


There might be better ways as a default:

https://stackoverflow.com/questions/41193629/how-to-keep-the-stacktrace-when-rethrowing-an-exception-out-of-catch-context


Workaround for now:

For each controller, define your own error handler and use one of the methods in the above linked StackOverflow answers

retendo commented 3 years ago

I got a related question. Is there any way to get the current HttpContext at the top level application CE error_handler? I would like to log information from the context. So far I didn't see any possibility to do so. The reason why I want to do top level exception handling is that I have a mix of controllers and sub routers and while I could do exception handling in controllers, there is no such way in routers.

Could we have the application CE pass the HttpContext to the provided error_handler instead of the logger? It's very easy to get the logger from from ctx and I could also use ctx.GetLogger and through that have MyType easily visible in the logs via NLog.