The CreatorCon Call for Content is officially open! Get started here.

How to Print Stack Trace via Script

The SN Nerd
Giga Sage
Giga Sage

Does anyone know if it is possible to print a stack trace out from a try/catch? I want to find out the line of code to store in an error record for my application.

Example code:

try {

        var gr = new GlideRecord('incident');

        var test = gr.madeUpFunction();

        test.adssad();

} catch (e) {

        var stackTrace = e.????;

        gs.print('Stack trace: ' + stackTrace);

}

Expected output:

Evaluator: org.mozilla.javascript.EcmaError: "est" is not defined.

  Caused by error in script at line 4

  1:

  2: var gr = new GlideRecord('incident');

  3: var test = gr.madeUpFunction();

==> 4: est.adssad();

Evaluator: org.mozilla.javascript.EcmaError: "est" is not defined.

  Caused by error in script at line -1


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
1 ACCEPTED SOLUTION

Hi Paul, the following is what I believe you have available for an exception:



try {


        var gr = new GlideRecord('incident');


        madeUpVariable.madeUpFunction();


} catch (e) {


        gs.print('LineNumber :' + e.lineNumber);


        gs.print('SourceName :' + e.sourceName);


        gs.print('Name: ' + e.name);


        gs.print('Message : ' + e.message);      


}



outcome:



*** Script: LineNumber :3


*** Script: SourceName :<refname>


*** Script: Name: ReferenceError


*** Script: Message : "madeUpVariable" is not defined.



Thanks,


Berny


View solution in original post

14 REPLIES 14

The SN Nerd
Giga Sage
Giga Sage

P.S.


e.stack returns undefined



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

sergiu_panaite
ServiceNow Employee
ServiceNow Employee

Hi Paul,



GlideLog has a method for dumping the stack trace in the log files:



GlideLog.dumpStack();



Regards,


Sergiu


Ironically, an exception is thrown when I try and use that method.



Attempted script access to inaccessible member denied - com.glide.util.Log:dumpStack:()V


Evaluator: java.lang.SecurityException: Illegal access to method dumpStack() in class com.glide.util.Log


  Caused by error in script at line 9



  6: gss.fakeMethod();


  7:


  8: } catch (e) {


==> 9: GlideLog.dumpStack();


  10: gs.print('caught' + e );


  11:


  12:



Evaluator: java.lang.SecurityException: Illegal access to method dumpStack() in class com.glide.util.Log


  Caused by error in script at line -1


Background message, type:error, message: Illegal access to method dumpStack() in class com.glide.util.Log



Anyway, I'm looking for a way to capture the output of the stack trace of the last caught exception so I can put it in my own logs.


I need to store it as a string temporarily.


The log files are not reliable enough.



Does anyone have any ideas?



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Paul,



What about this type of exercise:



try {


      foo.somethingThatThrows();


} catch (err) {


      gs.error("There was an error doing something", err);


}



gives me on my own instance:



[0:00:00.004] Script completed in scope global: script


*** Script: There was an error doing something: ReferenceError: "foo" is not defined.: no thrown error



Is this good enough?



Regards,


Sergiu