Working on some code that's currently on Lucee 5 a...
# lucee
d
Working on some code that's currently on Lucee 5 and really wanted access to Lucee 6's throw feature where you could specify the "cause" of the exception, so I came up with the following which appears to work:
Copy code
/**
	* @hint  A wrapper for throwing exception which supports passing in a "cause" to emulate Lucee 6.
	*
	*        NOTE - When migrating to Lucee 6, we could remove this and just use <cfthrow /> with the
	*               "clause" attribute, which would do the same thing.
	*
	* @type          A custom type or the predefined type Application. Do not enter any other predefined types because they are not generated by CFML applications.
	* @message       A message that describes the exceptional event.
	* @detail        A detailed description of the event. The CFML server appends the position of the error to this description; the server uses this parameter if an error is not caught by your code.
	* @errorCode     A custom error code that you supply.
	* @extendedInfo  Extended information about the exception.
	* @cause         The cause of the exception created with this tag. This can be a cfcatch block or a native Java exception.
	*/
	private void function throwException(required string type, string message, string detail, string errorCode, string extendedInfo, any cause){
		var hasCause = arguments.keyExists("cause");

		// for Lucee exceptions, we need to get the raw exception
		if( hasCause && (find("lucee.runtime.exp.", arguments.cause.getClass().getName()) == 1)) arguments.cause = arguments.cause.getPageException().getException();

		// when no message is defined, try to inherit from the cause
		if( hasCause && !arguments.keyExists("message") ) arguments.message = arguments.cause.getMessage();

		var CustomTypeException = createObject("java", "lucee.runtime.exp.CustomTypeException").init(
			  arguments.message?:""
			, arguments.detail?:""
			, arguments.errorCode?:""
			, arguments.type?:""
			, arguments.extendedinfo?:""
		);

		// attach the cause to our exception
		if( hasCause ){
			CustomTypeException.initCause(arguments.cause);
		}

		throw CustomTypeException;
	}
Just thought I'd share it for anyone else who might want it.
👍 2
👍🏼 1