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

It is not enough information.


I'm deploying an application at my company that is still in early stages and I really need to know exactly where in my code the errors are being thrown.



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

Hi @The SN Nerd 
There is another callable function in GlideLog:

gs.log(new GlideLog.getStackTrace(new Packages.java.lang.Throwable()));

See also https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0683765


If this answer was helpful, I would appreciate if you marked it as such - thanks!

Best
Daniel

Kalaiarasan Pus
Giga Sage

This does not throw me any error. Do I need to enable any property for this to throw error?


Example code is meant to be this sorry



  1. try {  
  2.         var gr = new GlideRecord('incident');  
  3.         madeUpVariable.madeUpFunction();  
  4. } catch (e) {  
  5.         var stackTrace = e.????;  
  6.         gs.print('Stack trace: ' + stackTrace);  
  7. }  

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

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