errorhandling techniques in xpages, javascript, java...

22
errorhandling techniques in XPages, Javascript, Java, Lotusscript and @Formulas

Upload: others

Post on 30-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures

errorhandling techniques in XPages, Javascript, Java, Lotusscript and

@Formulas

Page 2: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures

�What is error handling?�Errorhandling in:◦@Formula◦Javascript◦Lotusscript◦Java◦xPages

�Openlog

Page 3: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures

Anticipate, detect and process of programming, user and environment failures or errors

Prevent partially by defensive programming

What counts is what you do with the errors

Page 4: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures
Page 5: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures
Page 6: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures

• Loads of cryptical error messages – no info about triggered events

• No central error handling possible - be sparse with your @Formulas - use display fields for re-usable calculations

• Defensive programming (@IsError, Test DataTypes: @Elements, @IsTime, @IsNumber, @Text ...)

Page 7: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures
Page 8: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures

Standard try - catch routine

try {

// call a function that could error out

var result = getInvoiceNumbers(customerNumber,invoiceDate);;

}

catch (e) {

result = "Error in retrieving invoices";

}

// optional use finally runs after the try/catch block

finally {

window.Forms[0].fldname.value = result

}

Throw your own errorsif (number > 10) { throw "NumberTooHighError" }

Page 9: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures

Try - catch routine based on error type

try {

var result = getInvoiceNumbers(customerNumber,invoiceDate);

}

catch (e

if e == "CustomerNumberException") {

result = "Incorrect customerNumber";

}

else if e == "DateErrorException") {

result = "Incorrect Date";

}

else result = "Error in retrieving invoices";

}

Page 10: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures

To register a general errorHandler use:

(window.)onerror=functionname()

This function will have 3 input variables like:

function functionname(msg,url,line_num)

You can use these variables to log/show the error details

Page 11: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures
Page 12: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures

◦ use On Error blocks

◦ On Error resume next is a big nono .. you are supposed to handle things, not ignoring them

◦ You have differtent audiences: users, developers, administrators. So throw custom errors.◦ Then handle specific errors

◦ Option Declare prevents some programmatic errors◦ new! Create a template in the new 8.5.1 LS IDE for your error

handling blocks in subs and functions

Page 13: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures
Page 14: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures

• As with JavaScript use try - catch - finally blocks and throw/catch custom errors

Try {

List list = new ArrayList(getInvoiceNumbers(customerNumber,invoiceD

ate));

} catch ( IOException e) {

// handle/log this error

} catch ( OtherErrorException e) {

// again handle this

} catch (Exception e) {

// other errors

}

// optional

}finally {

}

Page 15: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures

• Chose:o Handle errors in subfunctionso Throw the error back to higher functions

private Integer convertToInteger(String str) {

try {

Integer intObj2 = Integer.valueOf(str)

} catch (e) { // do something }

return int;

}

private Integer convertToInteger(String str) throws

NumberFormatException {

Integer intObj2 = Integer.valueOf(str)

}

Page 16: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures

class CustomException extends Exception {

public CustomException(String msg){

super(msg);

}

}

public class Test {

public static void main(String[] args) {

try {

System.out.println(divide(4,0));

}

catch (CustomException e) { e.printStackTrace(); }

}

static int divide(int first,int second) throws CustomException{

if(second==0) throw new MyException("dividing by zero");

return first/second;

}

}

Page 17: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures
Page 18: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures

Error handling in xPages• Default errorpage on server (admin can change xsp\nsf\xsp.properties)• Set errorpage in application• Custom Error page (general, debug, specific)• Logging to AgentLog

Page 19: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures

how wonderful, free stuff

Page 20: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures

• JavaScript• Java

• XPages• Lotusscript

DEMO!!!!!!

Page 21: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures

◦ Getting it to work is NOT ENOUGH! ◦ Always add errorhandling to production code◦ Thou shall avoid the use of hard coded stuff◦ When not sure: check◦ If it can go wrong, it will go wrong. At one point.◦ Think first, then build your logic◦ Do not repeat code

◦ Use only one line of code in events (click, query*)

◦ Your users are dumb ... but: you are even dumber (if you ignore them)◦ Make your code beautiful: comment not what,

but why and use meaningfull names

Page 22: errorhandling techniques in XPages, Javascript, Java ...vinceschuurman.com/home/ndt4.nsf/(lublogcontent... · Anticipate, detect and process of programming, user and environment failures

Vince Schuurman

[email protected]

Martin Schaefer

[email protected]