@martinmilo/verve
Version:
TypeScript domain modeling library with field-level authorization, business rule validation, and context-aware access control
62 lines • 1.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VerveErrorList = exports.VerveError = void 0;
const ErrorRegistry_1 = require("./ErrorRegistry");
class VerveError extends Error {
constructor(code, ...args) {
super(VerveError.getMessageWithCode(code, ...args));
}
is(code) {
return this.message.startsWith(`${code}:`);
}
static getMessage(code, ...args) {
return ErrorRegistry_1.ErrorRegistry.getMessage(code).replace(/\{\{(.*?)\}\}/g, (_, p1) => {
const [obj] = args;
if (obj) {
return obj[p1] || '';
}
return '';
});
}
static getMessageWithCode(code, ...args) {
if (ErrorRegistry_1.ErrorRegistry.hasHiddenCodes()) {
return VerveError.getMessage(code, ...args);
}
return `${code}: ${VerveError.getMessage(code, ...args)}`;
}
}
exports.VerveError = VerveError;
class VerveErrorList {
constructor() {
this.errors = [];
}
static new() {
return new VerveErrorList();
}
count() {
return this.errors.length;
}
add(code, ...args) {
this.errors.push({ code, args });
}
merge(errors) {
this.errors.push(...errors.errors);
}
contains(code) {
return this.errors.some(error => error.code === code);
}
isEmpty() {
return this.errors.length === 0;
}
isPresent() {
return this.errors.length > 0;
}
toErrorMessages() {
return this.errors.map(error => VerveError.getMessage(error.code, ...error.args));
}
toErrorMessagesWithCode() {
return this.errors.map(error => VerveError.getMessageWithCode(error.code, ...error.args));
}
}
exports.VerveErrorList = VerveErrorList;
//# sourceMappingURL=VerveError.js.map