Welcome to the IBM Collaboration Solutions Community IQJam.
UsernamePassword
Reset Password | Register
   
Home | Leader Board | Tags | Help
How Do I Debug SSJS? 
I have searched high and low but I have not found any documentation anywhere on how you debug SSJS.  Just looking for the standard things such as setting breakpoints, stepping through the code and inspecting variables.
Domino Development / XPages
Tagged
Peter Presnell - over 2 years ago |  |  | Viewed 600 times

There are 8 answers

3votes
Marked as correct on9/19/09 12:47 AM

I don't know if I have a great answer to this yet.  I will say that:

print() is your friend!

Since print() writes to the Notes Log it's almost like debugging a server/web agent.

TaskJam has something in it to hook into OpenLog.  I've not tried that yet but that might be something to check out.

I've not heard of anything that will allow stepping through the code....



David Leedy - over 2 years ago | 
Voting
Vote on the answer to show whether you think the answer is correct or useful to the rest of the community.

Answers with more votes are more visible to the rest of the community


0votes
Here's a link to TaskJam that David mentions.

Bruce Elgort - over 2 years ago | 
Voting
Vote on the answer to show whether you think the answer is correct or useful to the rest of the community.

Answers with more votes are more visible to the rest of the community


2votes
I didn't know about print(). I've used OpenLog for XPages, using log.logEvent() to write out. It's the same principle as print() though.

Paul Withers - over 2 years ago | 
Voting
Vote on the answer to show whether you think the answer is correct or useful to the rest of the community.

Answers with more votes are more visible to the rest of the community


1votes
There is also article I found on how to use Visual Studio 8 to debug on the server side in a hackish kind of way.

Not sure if would work in your case, but you never know.


Christopher Byrne - over 2 years ago | 
Voting
Vote on the answer to show whether you think the answer is correct or useful to the rest of the community.

Answers with more votes are more visible to the rest of the community


2votes
What I've been doing when a bit of SSJS gives me fits is copy and paste the code into a computed text field, and then use the line use:

return <insert meaningful text here>

so if I want to know what the return result of a variable is, I'll return it and it displays it, or if I want to check to see how far a script gets, ill do something like

return 'got this far!'

etc...


Jeremy Hodge - over 2 years ago | 
Voting
Vote on the answer to show whether you think the answer is correct or useful to the rest of the community.

Answers with more votes are more visible to the rest of the community


1votes
For those who may not know SSJS stands for "Server Side Javascript".

Bruce Elgort - over 2 years ago | 
Voting
Vote on the answer to show whether you think the answer is correct or useful to the rest of the community.

Answers with more votes are more visible to the rest of the community


0votes
@Christopher.  I was actually based upon my experience with .Net (Visual Studio) that I was asking the question.  When I developed server-sided code with C# I could step through my code, set breakpoints inspect variables as with LotusScript debugger.  I could even change variables and change the code about to be executed allowing me to quickly debug my code.  It sounds like with XPages i am restricted to the old fashioned (e.g 1960s) style of debugging with manual debug statements that direct my output to the server log, agentlog, or back to a debug field on my xpage. 

I Just wanted to check with all the experts that I wasnt missing something obvious - heavan knows debugging wouldn't be the only major item missing from the Xpage help documentation.


Peter Presnell - over 2 years ago | 
Voting
Vote on the answer to show whether you think the answer is correct or useful to the rest of the community.

Answers with more votes are more visible to the rest of the community


0votes
I'm too behind in my XPages project to think about how I can save time :-/ but when I get some breathing room I'm going to look at Medusa.

Until then, I took a technique I saw from Tommy Valand (emailing a stack trace) and changed/extended it to instead create and populate requestScope.debugOutput.  I create a computed field that is bound to requestScope.debugOutput (using JavaScript so that it's one way instead of binding to requestScope through EL), and then I use the following script library:
Debug = {
    _log : function (msgType, msgSource, msg) {
        var tmp = msgType;
        if (msgSource) {
            tmp += ' from ' + msgSource;
        }
        var newEntry = '<dt>' + @Now() + ' ' + tmp + ':</dt><dd>' + msg + '</dd>';
        if ( requestScope.debugOutput ) {
            requestScope.debugOutput += newEntry;
        } else {
            requestScope.debugOutput = newEntry;
        }
    },
   
    exception : function (exception, source) {
        var stringWriter = new java.io.StringWriter();
        exception.printStackTrace( new java.io.PrintWriter( stringWriter ) );
        this._log('Error', source, stringWriter.toString());
    },
   
    message : function (msg, source) {
        this._log('Print ', source, msg);
    }
};
From within a custom control's combobox's onblur SSJS, I might do something like:
var sHere = 'ccCustomControlName.comboBoxId.onblur';
try {
     Debug.message('starting', sHere);
    // do stuff
     Debug.message('successful', sHere);
} catch (e) {
     Debug.exception(e, sHere);
}
 
Two caveats:
  1. It appears that you can't use global variables that don't exist, so when you remove the Debug script library from your XPages, you also have to remove all the calls.  Even code like "if ( Debug ) { Debug.exception(e, sHere); }" will crash if Debug doesn't exist.
  2. If you're debugging partial refreshes then your display of requestScope.debugOutput must be within what you're refreshing.


John Smart - about 1 year ago | 
Voting
Vote on the answer to show whether you think the answer is correct or useful to the rest of the community.

Answers with more votes are more visible to the rest of the community