@ackplus/nest-dynamic-templates
Version:
A powerful and flexible dynamic template rendering library for NestJS applications with support for Nunjucks, Handlebars, EJS, Pug, MJML, Markdown, and more.
121 lines • 5.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TemplateConflictError = exports.TemplateForbiddenError = exports.TemplateInputError = exports.TemplateEngineUnavailableError = exports.TemplateRenderError = exports.TemplateNotFoundError = exports.TemplateErrorCode = void 0;
exports.isTemplateError = isTemplateError;
const common_1 = require("@nestjs/common");
var TemplateErrorCode;
(function (TemplateErrorCode) {
TemplateErrorCode["NOT_FOUND"] = "TEMPLATE_NOT_FOUND";
TemplateErrorCode["RENDER_FAILED"] = "TEMPLATE_RENDER_FAILED";
TemplateErrorCode["ENGINE_UNAVAILABLE"] = "TEMPLATE_ENGINE_UNAVAILABLE";
TemplateErrorCode["INVALID_INPUT"] = "TEMPLATE_INVALID_INPUT";
TemplateErrorCode["FORBIDDEN"] = "TEMPLATE_FORBIDDEN";
TemplateErrorCode["CONFLICT"] = "TEMPLATE_CONFLICT";
})(TemplateErrorCode || (exports.TemplateErrorCode = TemplateErrorCode = {}));
const TEMPLATE_ERROR = Symbol.for('nest-dynamic-templates.error');
function isTemplateError(e) {
return typeof e === 'object' && e !== null && e[TEMPLATE_ERROR] === true;
}
function body(name, status, details) {
return { statusCode: status, error: name, ...details };
}
function cap(s) {
return s.charAt(0).toUpperCase() + s.slice(1);
}
class TemplateNotFoundError extends common_1.NotFoundException {
code = TemplateErrorCode.NOT_FOUND;
details;
[TEMPLATE_ERROR] = true;
constructor(args) {
const kind = args.kind ?? 'template';
const details = {
code: TemplateErrorCode.NOT_FOUND,
message: `${cap(kind)} "${args.templateName}" was not found in scope "${args.scope ?? 'system'}"${args.locale ? ` for locale "${args.locale}"` : ''}.`,
templateName: args.templateName,
scope: args.scope,
scopeId: args.scopeId,
locale: args.locale,
hint: `Create the ${kind} first, or check the name/scope/locale. Resolution falls back to scope "system" and locale "en".`,
};
super(body('TemplateNotFoundError', 404, details));
this.name = 'TemplateNotFoundError';
this.details = details;
}
}
exports.TemplateNotFoundError = TemplateNotFoundError;
class TemplateRenderError extends common_1.UnprocessableEntityException {
code = TemplateErrorCode.RENDER_FAILED;
details;
[TEMPLATE_ERROR] = true;
constructor(details, cause) {
const full = { code: TemplateErrorCode.RENDER_FAILED, ...details };
super(body('TemplateRenderError', 422, full), {
cause: cause instanceof Error ? cause : undefined,
});
this.name = 'TemplateRenderError';
this.details = full;
}
}
exports.TemplateRenderError = TemplateRenderError;
class TemplateEngineUnavailableError extends common_1.InternalServerErrorException {
code = TemplateErrorCode.ENGINE_UNAVAILABLE;
details;
[TEMPLATE_ERROR] = true;
constructor(args) {
const { engine, kind, reason, peerPackage } = args;
const configKey = kind === 'template' ? 'engines.template' : 'engines.language';
const message = reason === 'not-installed'
? `${cap(kind)} engine "${engine}" is enabled but its package${peerPackage ? ` "${peerPackage}"` : ''} is not installed.`
: `${cap(kind)} engine "${engine}" is not enabled.`;
const hint = reason === 'not-installed'
? `Run \`npm install ${peerPackage ?? engine}\`.`
: `Add it to NestDynamicTemplatesModule.forRoot({ ${configKey}: ['${engine}'] }).`;
const details = {
code: TemplateErrorCode.ENGINE_UNAVAILABLE,
message,
engine,
hint,
};
super(body('TemplateEngineUnavailableError', 500, details));
this.name = 'TemplateEngineUnavailableError';
this.details = details;
}
}
exports.TemplateEngineUnavailableError = TemplateEngineUnavailableError;
class TemplateInputError extends common_1.BadRequestException {
code = TemplateErrorCode.INVALID_INPUT;
details;
[TEMPLATE_ERROR] = true;
constructor(message, hint) {
const details = { code: TemplateErrorCode.INVALID_INPUT, message, hint };
super(body('TemplateInputError', 400, details));
this.name = 'TemplateInputError';
this.details = details;
}
}
exports.TemplateInputError = TemplateInputError;
class TemplateForbiddenError extends common_1.ForbiddenException {
code = TemplateErrorCode.FORBIDDEN;
details;
[TEMPLATE_ERROR] = true;
constructor(message, hint) {
const details = { code: TemplateErrorCode.FORBIDDEN, message, hint };
super(body('TemplateForbiddenError', 403, details));
this.name = 'TemplateForbiddenError';
this.details = details;
}
}
exports.TemplateForbiddenError = TemplateForbiddenError;
class TemplateConflictError extends common_1.ConflictException {
code = TemplateErrorCode.CONFLICT;
details;
[TEMPLATE_ERROR] = true;
constructor(message, hint) {
const details = { code: TemplateErrorCode.CONFLICT, message, hint };
super(body('TemplateConflictError', 409, details));
this.name = 'TemplateConflictError';
this.details = details;
}
}
exports.TemplateConflictError = TemplateConflictError;
//# sourceMappingURL=template.errors.js.map