[coldbox-5.6.2] Throw an error in a handler for onError to catch

Is this possible? I want my handler action to throw a custom error using throw() and have THAT handlers onError() catch it. So far, my tests indicate that custom nature of my manually throwing an error is not considered “runtime” which the framework is watching for to trip the onError() action in that handler.

The easiest way to accomplish this is to use the “aroundHandler” method and let the catch block in there handle the exception response.

See coldbox.system.RestHandler for an example.

Isn’t that supposed to work though? The docs say re: onError():

This is a localized error handler for your event handler. If any type of runtime error occurs in an event handler and this method exists, then the framework will call your method so you can process the error first. If the method does not exist, then normal error procedures ensue.

We use this quite a bit… and now I need to check, but thought it was working as advertised?

Yes, however your local error handler passes through, after its done processing to the global error Coldbox “onException” interceptor ( “so you can process the error first” ). If that’s what you want, then you’re good. If you want to change the response entirely then I find it easier to trap and respond to custom errors using “aroundHandler”

Jon

Jon - right. Conceptually I always thought of a handler’s onError method as just that: an error/exception-specific aroundHandler for that handler’s methods. So, yep - I’m good… I just think Mike is not!

Mike - I just checked and can confirm that if I throw() a custom error in a handler, that handler’s onError method will indeed pick it up. So it appears something is not working correctly on your setup. I tested in Coldbox 5-6 and 6.0-RC.

Well that’s strange. I thought originally it was because I had specified my global error handler in coldbox.cfc so I disabled that and I have no error interceptor code anywhere…perhaps a sample of that handler or where you talking about using a test with an aroundHandler ? Not sure what I’d be doing wrong here. I do understand Jon’s recommendation on the aroundHandler and can test that as well.

Mike

Sure… just grab a fresh advanced-script template. Add a new Test.cfc handler and just put these 2 methods in there:

`

function onError( event, rc, prc, faultAction, exception, eventArguments ){
if( exception.type==“Test.onError” ){
writelog(type=“error”, file=“testdebug”, text=“fired from: Test.onError [type: #exception.type# | message: #exception.message#]”);
relocate();
}
}

function throwAnError( event, rc, prc ) {
throw( type=“Test.onError”, message=“I am a custom error.” );
event.setView( “test/index” );
}

`

hit the site URL with /test/throwAnError and check your logfile. I just did this and mine says:

`

“Severity”,“ThreadID”,“Date”,“Time”,“Application”,“Message”
“ERROR”,“ajp-nio-8009-exec-8”,“05/21/2020”,“17:14:22”,"",“fired from: Test.onError [type: Test.onError | message: I am a custom error.]”

`

…proving that the handler’s onError method is catching the custom error thrown in the same handler.