Expand my Community achievements bar.

SOLVED

Creating custom exception class

Avatar

Level 4

As a part of some library, I want to be able to throw some custom exception. I define my exception class like this:

loadLibrary("xtk:shared/nl.js")

LibException = function(message, data) {

  this.message = message

  this.data = data

}

LibException.getMessage = function() {

  return this.message

}

LibException.getData = function() {

  return this.data

}

LibException.prototype.toString = function() {

     return "Library Exception: " + this.message

}

LibException.isLibException = function(obj) {

  return NL.isObject(obj) && obj.constructor === LibException

}

//...

throw new LibException("Test Exception", { test: 1 })

It works, but I cannot see any error message in the log. All I can see there is:

16/09/2019 14:21:56 js SCR-160012 JavaScript: error while evaluating script 'WKF11906/js'.

Now, if I throw Error class, the message will be written into the logs:

try {

  //...

throw new LibException("Test Exception", { test: 1 })

//...

} catch (e) {

  if (LibException.isLibException(e)) {

     throw new Error(e.toString())

}

throw e

}

logError also works but the result log message will not be so verbose.

catch (e) {

  if (LibException.isLibException(e)) {

     logError(e.toString())

}

logError(e)

}

I need custom class, so I can provide extra data, check for error type and potentially re-try the process if it has been network issue or something similar.

NB. I understand, that I can simply modify Error object, something like this:

But

var error = new Error("Network Error")

error.type = "NetworkError"

error.extraData = { port: 9999 }

throw error

//..

catch(e) {

if (e.type === "NetworkError") {

    //sleep for some time and re-try

} else {

   // any other error

   throw e

}

}

But I looking for a more structured solution.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hey,

How about using String groups?

Use below in your library

// Infos, errors or warnings

var Test_STRING_GROUP = new StringGroup("namespace:schema")

try

{

//....

}

catch (e)

{

    if (e.soapFault)

      logError(Test_STRING_GROUP .error000_1(e.soapFault.faultstring))

    else

      logError(Test_STRING_GROUP .error000_1(e))

  }

Once done, then you can set message and  Text in String groups , something like

Message : error000_1

Text : An error occurred: {1}

Thanks,

Kapil

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

Hey,

How about using String groups?

Use below in your library

// Infos, errors or warnings

var Test_STRING_GROUP = new StringGroup("namespace:schema")

try

{

//....

}

catch (e)

{

    if (e.soapFault)

      logError(Test_STRING_GROUP .error000_1(e.soapFault.faultstring))

    else

      logError(Test_STRING_GROUP .error000_1(e))

  }

Once done, then you can set message and  Text in String groups , something like

Message : error000_1

Text : An error occurred: {1}

Thanks,

Kapil