framework-one / fw1

FW/1 - Framework One - is a lightweight, convention over configuration, MVC application framework for ColdFusion / CFML.
http://framework-one.github.io
Other
373 stars 141 forks source link

fw1 v4.2 - error handler override #533

Closed doodi-v1 closed 3 years ago

doodi-v1 commented 3 years ago

I see you can override the error handler at the application level, but is there a way to override the handler at the request or controller level? If I have a primarily REST application with a UI/form controller, or visa-versa, a primarily UI application with REST controllers, I would want the output to reflect the request - UI=pretty for user, REST=http error with JSON.

I can think of a few ways to accomplish but I am hoping I am simply overlooking something already in the framework (like setting rc.outputOnlyJSON=true in the controller.before function and checking this value within the error handler).

matthewjones commented 3 years ago

I would likely separate my controllers between rest and non-rest stuff, then in the before of the rest style set something into request scope, like you stated. Then override the onError() in Application.cfc and look for that, if it is set, handle it however you like (and avoid the frameworks onError() method), and if it was not set, just call super.onError(), if that sufficiently works for your non rest stuff.

In my opinion, it wouldn't make sense to add anything extra to the framework for a json version of the error, because the expected error output is going to be very application specific.

I have an onError in all of my Application.cfcs, if for nothing else, to log the error, sometimes i also still call super, sometimes not.

doodi-v1 commented 3 years ago

I was secretly hoping for a request.error="main.error" or request.error="rest.error" override feature, maybe in a variables.fw.setError() form. But I can accomplish the same without too much difficulty. Thanks.