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.