'use strict';
/**
* Exception builder for throwable objects.
* @alias Rac.Exception
*/
class Exception {
constructor(name, message) {
this.name = name;
this.message = message;
}
toString() {
return `Exception:${this.name} - ${this.message}`;
}
/**
* When `true` the convenience static functions of this class will
* build `Error` objects, otherwise `Exception` objects are built.
*
* Set as `false` by default for browser use: throwing an `Exception`
* in chrome displays the error stack using source-maps when available,
* while throwing an `Error` object displays the error stack relative to
* the bundled file which is harder to read.
*
* Used as `true` for test runs in Jest: throwing an `Error` will be
* reported in the test report, while throwing a custom object (like
* `Exception`) within a matcher results in the expectation hanging
* indefinitely.
*/
static buildsErrors = false;
/**
* Returns an convenience function for building throwable objects.
*
* The function can can be used as following:
* ```
* func(message) // returns an `Exception`` object with `name` and `message`
* func.exceptionName // returns the `name` of the built throwable objects
* ```
*/
static named(name) {
let func = (message) => {
if (Exception.buildsErrors) {
const error = new Error(message);
error.name = name;
return error;
}
return new Exception(name, message);
};
func.exceptionName = name;
return func;
}
static drawerNotSetup = Exception.named('DrawerNotSetup');
static failedAssert = Exception.named('FailedAssert');
static invalidObjectType = Exception.named('InvalidObjectType');
static abstractFunctionCalled = Exception.named('AbstractFunctionCalled');
// TODO: migrate rest of invalidObjectConfiguration
static invalidObjectConfiguration = Exception.named('InvalidObjectConfiguration');
// invalidParameterCombination: 'Invalid parameter combination',
// invalidObjectToDraw: 'Invalid object to draw',
// invalidObjectToApply: 'Invalid object to apply',
} // class Exception
module.exports = Exception;