Last active
January 4, 2021 21:57
-
-
Save begin-again/4a82a8aaf0ab836805c5446aa149b33d to your computer and use it in GitHub Desktop.
error instance
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 characters
<cfscript> | |
// run with Adobe 2018 engine cuz i'm lazy with semi-colons | |
public function doSomething( | |
any error = {} | |
){ | |
var result = {SUCCESS: false, ERRORS: []}; | |
if(isErrorObject(error)){ | |
arrayAppend(result.errors, { | |
message: error.message | |
, type: error.type | |
, detail: error.detail | |
, code: error.errorcode | |
}) | |
} | |
else { | |
var _error = {} | |
var propsOfInterest = ["message","detail","errorCode", "type"]; | |
if(isSimpleValue(error)){ | |
_error.message = error; | |
} | |
else if(isStruct(error)){ | |
for(var prop in error){ | |
if(arrayContainsNoCase(propsOfInterest, prop)){ | |
_error[prop] = error.keyExists(prop) ? error[prop] : "not specified"; | |
} | |
} | |
} | |
else if(isArray(error)){ | |
throw(message="error cannot be an array"); | |
} | |
else if(isDate(error)){ | |
_error.message = error.dateFormat("yyy-mm-dd"); | |
} | |
arrayAppend(result.errors, _error) | |
} | |
writeOutput( | |
"Checking: #serializeJSON(result)#<br>" | |
); | |
} | |
/** | |
* Determines if @error is in fact an Error created by throwing | |
* | |
* @error | |
*/ | |
public boolean function isErrorObject(required any error){ | |
if( | |
isStruct(error) || isArray(error) || isSimpleValue(error) || isDate(error) | |
){ | |
return false; | |
} | |
else { | |
return true; | |
} | |
} | |
try{ | |
throw(message="me error", type="orange"); | |
} | |
catch(any e) { | |
doSomething(e) | |
} | |
doSomething({message: "me error", type:"expression"}) | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was hoping there would be some cleaner method to determine if an object is an error, but even still I would have to then do a bunch of checks of other types of objects. Ideally I want to require a type of Error (apparently it doesn't exist) for the error parameter to the doSomething function.