Skip to content

Instantly share code, notes, and snippets.

@homestar9
Created March 19, 2025 21:17
Show Gist options
  • Save homestar9/15de3fb4dc8f5fa076813f534b1c3ea9 to your computer and use it in GitHub Desktop.
Save homestar9/15de3fb4dc8f5fa076813f534b1c3ea9 to your computer and use it in GitHub Desktop.
Sample `aroundHandler()` I like to use as part of a ColdBox BaseHandler
function aroundHandler( event, targetAction, eventArguments, rc, prc ) {
try {
// prepare arguments for action call
var args = {
event = arguments.event,
rc = arguments.rc,
prc = arguments.prc
};
structAppend( args, eventArguments );
// execute the action now
return arguments.targetAction( argumentCollection=args );
// Handle Bad Requests
} catch ( BadRequest e ) {
param prc.badRequestSetMessage = true;
if ( prc.badRequestSetMessage ) {
cbMessagebox().setMessage(
type="error",
message = e.message
);
}
return runEvent(
event = "errors.onBadRequest",
eventArguments = {
"exception": e
}
);
// Handle missing entities (404s)
} catch ( EntityNotFound e ) {
return runEvent(
event = "errors.onMissingPage",
eventArguments = {
"exception": e
}
);
// Handle missing file download requests
} catch ( InvalidFileException e ) {
cbMessagebox().setMessage(
type="error",
message = "I was unable to find the file you requested."
);
return runEvent(
event = "errors.onMissingPage",
eventArguments = {
"exception": e
}
);
// Handle validation failures
} catch ( ValidationException e ) {
param prc.validationSetMessage = true;
param prc.validationExcludes = "";
handleValidationException(
event = event,
rc = rc,
prc = prc,
exception = e,
setMessage = prc.validationSetMessage,
excludes = prc.validationExcludes
);
return runEvent(
event = "errors.onBadRequest",
eventArguments = {
"exception": e
}
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment