onException vs onError

I am trying to implement a custom error handling for each request made to the handler. I’m using the ColdBox exceptionHandler as follows.

`

function onException(event,rc,prc){

//Grab Exception From request collection, placed by ColdBox

var exceptionBean = event.getValue(“ExceptionBean”);

if (NOT isDebugMode()) {

var Exception = exceptionBean;

var bugreport= “”;

savecontent variable=“bugreport” {

include “/coldbox/system/includes/BugReport.cfm”;

}

//Place exception handler below:

dblogger.error(“MESSAGE:#exceptionBean.getExceptionStruct().message#, DETAIL:#exceptionBean.getExceptionStruct().detail#”,exceptionBean);

emaillogger.error(“MESSAGE:#exceptionBean.getExceptionStruct().message#, DETAIL:#exceptionBean.getExceptionStruct().detail#”,bugreport);

//location(url=’/site-error’, addToken=‘false’);

}

}

`

I would like to do the same for the onError in the handler. However, I can’t seem to use the event.getValue(“ExceptionBean”) thus not being able to use the default BugReport.cfm template. The code for the onError is as follows.

`

/**

  • On Error

*/

function onError(event,faultAction,exception,eventArguments){

// prepare a data packet

var data = {

status = false,

error = ‘An error occurred. Please try again. If the problem persists please notify the system administrator.’

};

if (arguments.faultAction == ‘create’){

data[‘error’] = ‘An error occurred while creating the work request. Please try again. If the problem persists please notify the system administrator.’;

}else if (arguments.faultAction == ‘createNote’){

data[‘error’] = ‘An error occurred while creating the work request note. Please try again. If the problem persists please notify the system administrator.’;

}

var exceptionmessage = “#arguments.exception.message# & #arguments.exception.detail#”;

// log via the log variable already prepared by ColdBox

log.error(“Exception when executing #arguments.faultAction# #exceptionmessage#”, arguments.exception);

// render out a json packet according to specs status codes and messages

event.renderData(data=data,type=“json”,statusCode=400,statusMessage=“Error Occurred”);

}

`

The arguments.exception is not recognized in BugReport. How can I set the object rc.ExceptionBean? Any suggestions for the onError would be much appreciated.

Maybe this will help
https://coldbox.ortusbooks.com/content/recipes/coldbox_exception_handling.html

Luis Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com
P/F: 1-888-557-8057

I used the documentation to build the onError. However, my question is if I wanted to use the ColdBox error template BugReport.cfm in onError then the value arguments.exception can’t be used. Is there an alternative?

When I dumped the
arguments
scope in onError(), arguments.expection is empty/null. It might be a bug.

Alternatively, you can get the exception from
arguments.prc.exception
, e.g. writeDump(arguments.prc.exception.getMessage());

On error is by convention on handlers. Is this where you are coding this?

Luis Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com
P/F: 1-888-557-8057

Although I didn’t get an exact answer this discussion helped me to look into the right direction. I was trying to reuse the BugReport.cfm located at

/coldbox/system/includes/BugReport.cfm. However, that template assumes the exception was set in the object coldbox.system.web.context.ExceptionBean. In order to reuse the template I has to initialize and set the exception data in the object coldbox.system.web.context.ExceptionBean. Below was the code I ended up writing.

`

var exceptionBean = createObject(“component”,“coldbox.system.web.context.ExceptionBean”).init(errorStruct=arguments.exception,extramessage=arguments.extramessage,errorType=arguments.errorType);
`

`
var bugreport= “”;
savecontent variable=“bugreport” {
include “/vims/views/handlerBugReport.cfm”;
}
//Place exception handler below:
dblogger.error(“MESSAGE:#exceptionBean.getExceptionStruct().message#, DETAIL:#exceptionBean.getExceptionStruct().detail#”,exceptionBean);
emaillogger.error(“MESSAGE:#exceptionBean.getExceptionStruct().message#, DETAIL:#exceptionBean.getExceptionStruct().detail#”,bugreport);

`

This discussion definitely helped me to think outside the box. Thank you all for your help.

Jae