fatalException Method
Creates a log item with severity FATAL containing a message and an exception
Definition
fatalException(logObject: any, e: any): Logger
Parameters
logObject | String or object to be logged, or a function that returns the string or object to be logged (details). |
---|---|
e | Exception that will be logged along with the logObject. On Chrome, Firefox and IE10 and higher, a stack trace will be logged as well. However, see the Remarks below. |
Return Value
The Logger itself.
Remarks
The fatalException function will log the stack trace of the JavaScript exception, showing where in your code the exception occurred, under these circumstances:
-
The exception was thrown by the browser, such as when trying to read an undefined variable:
try { // Browser throws exception i.dont.exist = 666; } catch(e) { // Logs stack trace JL().fatalException("Exception was thrown!", e); }
-
You threw an
Error Object
or an Exception Object
: try { // Throwing Error object throw new Error("Whoops!"); } catch(e) { // Logs stack trace JL().fatalException("Exception was thrown!", e); }
The fatalException function will not log the stack trace if you throw something that is not an Error Object or Exception Object:
try { // Throwing something that is not an Error Object or // Exception Object (in this case a string) throw "Whoops!"; } catch(e) { // Does not log stack trace JL().fatalException("Exception was thrown!", e); }
The Exception Object is built into JSNLog.
It opens the door to much better exception handling.
The details are
here
Examples
This code catches any JavaScript exceptions and logs them.
try { ... } catch (e) { JL().fatalException("Exception was thrown!", e); }
This code catches any JavaScript exceptions and logs them along with the values of some variables to make debugging easier.
function f1(i, j) { try { ... } catch (e) { JL('f1').fatalException({ "i": i, "j": j}, e); } }