M. Schaetzl
M. Schaetzl

Reputation: 251

XPages Custom Error Page - get error message and line

I'm trying to generate a custom error page for my xpages. I googled a lot of solutions and so far I get an error page telling me, that an error occured.

But I can't get the information what exactly happened (in this case the error is, that an "doc" has to be saved, but i named the variable "docs" just to get error).

All I do is:

var errObj = requestScope.error; 
output = errObj.getCause().getErrorPropertyId();
output = errObj.getCause().getComponentId();

As soon as I try to call getExpressionText() I get an error 500.

How do I get the information, where the error happened (line number) and the variable that caused the error? - just like I do using the standard error page.

Upvotes: 0

Views: 2379

Answers (3)

Paul Stephen Withers
Paul Stephen Withers

Reputation: 15739

The error line and details are not easily accessible from requestScope.error. If you look at the source code for the latest release of Mark Leusink's Debug Toolbar, you'll see he's parsing the stack trace to get the details.

However, you can access all the relevant information using the underlying Java class for the SSJS exception - com.ibm.jscript.InterpretException using getErrorLine(). The getLocalizedMessage() method gets the error detail that usually starts "Script interpreter error". The getExpressionText() method retrieves the line that threw the error.

If you take a look at the XPages OpenLog Logger project I put on OpenNTF, that's what I use to log full details to OpenLog. http://www.openntf.org/Internal/home.nsf/project.xsp?action=openDocument&name=XPages%20OpenLog%20Logger

You can see the source code of the OpenLogPhaseListener which uses those methods here: https://github.com/paulswithers/openlogjava/blob/master/OpenLogJava/WebContent/WEB-INF/src/com/paulwithers/openLog/OpenLogPhaseListener.java

Even if you're not a Java expert, from use of SSJS the key parts should be understandable. Line 84 captures uncaught exceptions - when XPages routes to the default error page. That uses the methods I mentioned.

Lines 98 to 105 are the ones that log out all the details if you just use a catch block, passing OpenLogBean.addError(e, this) where e is the error object and this is the component the error occurs on. error.getError() in the Java code retrieves that error object. To get the typeahead in SSJS you'll need to use catch(e:com.ibm.jscript.InterpretException) I believe.

I haven't tested this, I've just working back from what I used for the project on OpenNTF.

Upvotes: 1

M. Schaetzl
M. Schaetzl

Reputation: 251

For now I've dealt with this problem by using the Debug Toolbar and the OpenLog Database.

If an error occurs the user only gets a custom error page (using the example of the Debug Toolbar) with the information, that something went wrong. So he does not have to bother with any other problems or even the Stack Trace. But at the same moment he gets the error page, the error is logged in our Log-Database with all informations needed (like line, exact error message etc.).

I've also implemented an "Report this problem" Link-Button to create a new E-Mail containing important information about the session the user is currently in.

Upvotes: 0

Per Henrik Lausten
Per Henrik Lausten

Reputation: 21709

Have a look at this XSnippet by Tony McGuckin: http://openntf.org/XSnippets.nsf/snippet.xsp?id=custom-error-page-cw-cause-and-stacktrace-information. It uses the following to output details on the error:

  var output = requestScope.error.toString()+"\n\n";
  if(requestScope.error instanceof com.ibm.xsp.exception.XSPExceptionInfo){
    var codeSnippet = requestScope.error.getErrorText(); 
    var control = requestScope.error.getErrorComponentId();
    var cause = requestScope.error.getCause();
    output += "In the control : " + control + "\n\n";
    if(cause instanceof com.ibm.jscript.InterpretException){
      var errorLine = cause.getErrorLine();
      var errorColumn = cause.getErrorCol();
      output += "At line " + errorLine;
      output += ", column " + errorColumn + " of:\n";
    }else{
      output += "In the script:\n";
    }
    output += codeSnippet;
  }
  return output;

Upvotes: 1

Related Questions