@rimbu/common
Version:
Common types and objects used in many other Rimbu packages
50 lines • 1.44 kB
JavaScript
/**
* Throws an `ErrBase.ForcedError` error when called.
* @example
* ```ts
* const emptyMap = HashMap.empty<number, string>()
* emptyMap.get(5, Err);
* // throws: ErrBase.ForcedError(message: 'Err: Forced to throw error')
* ```
*/
export function Err() {
return ErrBase.msg('Err: Forced to throw error')();
}
export var ErrBase;
(function (ErrBase) {
/**
* A custom error instance.
*/
class CustomError {
constructor(message) {
this.message = message;
}
get name() {
return this.constructor.name;
}
}
ErrBase.CustomError = CustomError;
/**
* Error type that is thrown by `Err` and the functions returned by `ErrBase.msg`.
*/
class ForcedError extends CustomError {
}
ErrBase.ForcedError = ForcedError;
/**
* Returns a function that, when called, throws an `Err.ForcedError` with the given `message` string.
* @param message - the message to put in the `Err.ForcedError` instance.
* @example
* ```ts
* const emptyMap = HashMap.empty<number, string>()
* emptyMap.get(5, ErrBase.msg('not found'));
* // throws: ErrBase.ForcedError(message: 'not found')
* ```
*/
function msg(message) {
return function () {
throw new ErrBase.ForcedError(message);
};
}
ErrBase.msg = msg;
})(ErrBase || (ErrBase = {}));
//# sourceMappingURL=err.mjs.map