timezonecomplete
Version:
DateTime, TimeZone, Duration and Period library aimed at providing a consistent and complete date-time interface, away from the original JavaScript Date class.
72 lines • 2 kB
JavaScript
;
/**
* Copyright (c) 2019 ABB Switzerland Ltd.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertError = exports.errorIs = exports.error = exports.throwError = void 0;
var util = require("util");
/**
* Throws an error with the given name and message
* @param name error name, without timezonecomplete prefix
* @param format message with percent-style placeholders
* @param args arguments for the placeholders
* @throws the given error
*/
function throwError(name, format) {
var args = [];
for (var _i = 2; _i < arguments.length; _i++) {
args[_i - 2] = arguments[_i];
}
var error = new Error(util.format(format, args));
error.name = "timezonecomplete." + name;
throw error;
}
exports.throwError = throwError;
/**
* Returns an error with the given name and message
* @param name
* @param format
* @param args
* @throws nothing
*/
function error(name, format) {
var args = [];
for (var _i = 2; _i < arguments.length; _i++) {
args[_i - 2] = arguments[_i];
}
var error = new Error(util.format(format, args));
error.name = "timezonecomplete." + name;
return error;
}
exports.error = error;
/**
* Returns true iff `error.name` is equal to or included by `name`
* @param error
* @param name string or array of strings
* @throws nothing
*/
function errorIs(error, name) {
if (typeof name === "string") {
return error.name === "timezonecomplete." + name;
}
else {
return error.name.startsWith("timezonecomplete.") && name.includes(error.name.substr("timezonecomplete.".length));
}
}
exports.errorIs = errorIs;
/**
* Converts all errors thrown by `cb` to the given error name
* @param errorName
* @param cb
* @throws [errorName]
*/
function convertError(errorName, cb) {
try {
return cb();
}
catch (e) {
return throwError(errorName, e.message);
}
}
exports.convertError = convertError;
//# sourceMappingURL=error.js.map