
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.
DIFFICULTY LEVEL: INTERMEDIATE
Assumes having taken the class SSNF and has good intermediate level of knowledge and/or familiarity with Scripting in ServiceNow.
The other day I wanted to detect exactly one type of error. The question was how do you figure out and more importantly react to certain error types. First I needed to know the current definition of the Error object. So back to my old friend MDN to find that out.
I scrolled down to the ErrorTypes section. Here is the short list:
EvalError - an error that occurs regarding the global function eval().
InternalError - an error that occurs when an internal error in the JavaScript engine is thrown. E.g. "too much recursion".
RangeError - error that occurs when a numeric variable or parameter is outside of its valid range.
ReferenceError - error that occurs when de-referencing an invalid reference.
SyntaxError - a syntax error that occurs while parsing code in eval().
TypeError - an error that occurs when a variable or parameter is not of a valid type.
URIError - an error that occurs when encodeURI() or decodeURI() are passed invalid parameters.
So, theoretically we should be able to catch these kinds of errors. Actually the only one I have seen consistently is SyntaxError. I would have to play around to see if I could get the others to trigger and it may be that these are ECMAScript 5. If so, they may not even be present yet in ServiceNow JavaScript except in Scoped Applications.
Hmmm, I have also seen this error in the past so I should be able to do a check for it:
com.glideapp.workflow.model.ModelRunTimeException
Armed with all of this I flew out to my handy-dandy personal instance. Quickly I navigated to Scripts-Background and feverishly I typed out the following script (which I tweaked from here SyntaxError)
// Copy and paste these from the above link, and change console.write to gs.info
var message = '--->\n';
// Catch a syntax error
try {
eval('hoo bar');
} catch (err) {
message += '---> CATCH:';
message += gs.getMessage('instanceof:\t{0}\n', [err instanceof SyntaxError]);// true
message += gs.getMessage('missing;:\t{0}\n', [err.message]); // "missing ; before statement"
message += gs.getMessage('name-syntax err:\t{0}\n', [err.name]); // "SyntaxError"
message += gs.getMessage('filename-scratchpad/1:\t{0}\n', [err.fileName]);// "Scratchpad/1"
message += gs.getMessage('linenumber:\t{0}\n', [err.lineNumber]); // 1
message += gs.getMessage('columnnumber:\t{0}\n', [err.columnNumber]); // 4
message += gs.getMessage('scratchpad/1:2:3:\t{0}\n\n', [err.stack]); // "@Scratchpad/1:2:3\n"
}
// Throw a syntax error
try {
throw new SyntaxError('Hello', 'someFile.js', 10);
} catch (err) {
message += '---> THROW:';
message += gs.getMessage('instanceof:\t{0}\n', [err instanceof SyntaxError]);// true
message += gs.getMessage('error message:\t{0}\n', [err.message]); // "Hello"
message += gs.getMessage('name-syntax err:\t{0}\n', [err.name]); // "SyntaxError"
message += gs.getMessage('filename:\t{0}\n', [err.fileName]); // "someFile.js"
message += gs.getMessage('linenumber:\t{0}\n', [err.lineNumber]); // 10
message += gs.getMessage('columnnumber:\t{0}\n', [err.columnNumber]); // 0
message += gs.getMessage('scratchpad/2:11:9:\t{0}\n\n', [err.stack]); // "@Scratchpad/2:11:9\n"
}
gs.error(message);
I click on the Run script button, and got the following results:
---> CATCH:instanceof: true missing;: missing ; before statement name-syntax err: SyntaxError filename-scratchpad/1: null.null.script#5(eval) linenumber: 1 columnnumber: undefined scratchpad/1:2:3: at null.null.script:5 ---> THROW:instanceof: true error message: Hello name-syntax err: SyntaxError filename: someFile.js linenumber: 10 columnnumber: undefined scratchpad/2:11:9: undefined
Imagine my surprise when I found that the instanceof worked! Wow! No kidding. I seriously thought it would break. Hmmm, maybe I had it spelled wrong on my previous article...doh!
Ok, so what failed?
filename (on the catch), column number, and (crying here) stack (on the throw).
With what worked I can now use the "instanceof" on an error in the catch and actually look at types of errors (a really good explanation of how to use instanceof can be found here you will have to scroll down to the bottom of the article). That failing I can at least look at the name and snag that.
BTW, since we are throwing an error the syntax to throw will have to be looked at because it obviously does not work this way in ServiceNow. More on throwing errors here. You will have to play around with that and see how it works (my challenge to you! Here is a good start: errors).
Enjoy!
Steven Bell.
If you find this article helps you, don't forget to log in and mark it as "Helpful"!
Originally published on: 11-03-2015 07:49 AM
I updated the code and brought the article into alignment with my new formatting standard.
- 3,352 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.