error-collector
Version:
Simple error bag collector ---
63 lines (62 loc) • 1.67 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ErrorBag = void 0;
var ErrorBag = /** @class */ (function () {
function ErrorBag() {
this.errors = [];
}
/**
* @param {Error} error
* @description Adds an error to the bag
* @returns void
*/
ErrorBag.prototype.add = function (error) {
if (error instanceof Error) {
this.errors.push(error);
return;
}
};
/**
* @description Returns all errors in the bag
* @returns {Error[]} errors
*/
ErrorBag.prototype.getErrors = function () {
return this.errors;
};
/**
* @description Returns true if bag is not empty
* @returns boolean
*/
ErrorBag.prototype.hasErrors = function () {
return this.errors.length > 0;
};
/**
* @description Removes all errors from the bag
* @returns void
*/
ErrorBag.prototype.clear = function () {
this.errors = [];
};
/**
* @description Logs all error messages to the console
* @returns void
*/
ErrorBag.prototype.logErrors = function () {
if (this.hasErrors()) {
this.errors.forEach(function (error) {
console.error(error.message);
});
}
};
/**
* @description Handles all errors in one Error
* @returns void
*/
ErrorBag.prototype.handleErrors = function () {
if (this.hasErrors()) {
throw new Error(this.errors.map(function (error) { return error.message; }).join("\n"));
}
};
return ErrorBag;
}());
exports.ErrorBag = ErrorBag;
;