Created
September 24, 2014 18:08
-
-
Save timmaybrown/b7f33420b782497c848f to your computer and use it in GitHub Desktop.
Revisions
-
timmaybrown created this gist
Sep 24, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,73 @@ component { property name="customJSON" inject="model"; public void function preHandler(event, rc, prc){ prc["ret"] = {}; } public any function onError(event, faultAction, exception, eventArguments){ //duplicate the PRC so it can be useful information during development var prevPRC = duplicate(prc); //make the ERROR ret based on the exception data var stArgs = { "event" = event ,"ret" = { "msg" = arguments.exception.message ,"detail" = arguments.exception.detail ,"error" = { "tagContext" = arguments.exception.tagContext } ,"prcBeforeError" = prevPRC } ,"statusCode" = 500 ,"statusText" = "Application Error Occurred" }; //send to the render results function to do the actual rendering out renderResults(argumentCollection=stArgs); } public any function postHandler(event, rc, prc){ if (!event.valueExists("format") || (event.valueExists("format") && rc.format NEQ 'html')) { //prep the renderResults argument collection var stArgs = { "event" = event ,"ret" = prc.ret }; //if status code or status text are provided lets append those to the renderData() call if (structKeyExists(prc, "statusCode")) stArgs.statusCode = prc.statusCode; if (structKeyExists(prc, "statusText")) stArgs.statusText = prc.statusText; //send to the render results function to do the actual rendering out renderResults(argumentCollection=stArgs); } } private any function renderResults(event, required any ret, numeric statusCode, string statusText){ // renderData() argument collection //writeDump(GetComponentMetaData("model.apiKey").properties);abort; var stArgs = { "type" = "plain" ,"contentType" = "application/json" ,"data" = customJSON.serialize(input=ret) }; //add statusCode and statusText to argCollection if one was specified explicitly if (structKeyExists(ARGUMENTS, "statusCode")) stArgs.statusCode = ARGUMENTS.statusCode; if (structKeyExists(ARGUMENTS, "statusText")) stArgs.statusText = ARGUMENTS.statusText; //finally render it out event.renderData(argumentCollection=stArgs); } private struct function badRequest(required string msg, string detail=""){ prc["statusCode"] = 400; prc["statusText"] = "Unprocessable Entity"; prc["ret"] = { "msg" = msg ,"detail" = detail }; return prc; } }