UNPKG

@sentzunhat/zacatl

Version:

A modular, high-performance TypeScript microservice framework for Node.js, featuring layered architecture, dependency injection, and robust validation for building scalable APIs and distributed systems.

86 lines 2.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AbstractRouteHandler = void 0; const error_1 = require("../../../../../../../error/index.js"); class AbstractRouteHandler { url; method; schema; constructor(args) { this.url = args.url; this.method = args.method; this.schema = args.schema; } handleError(error) { if (error instanceof error_1.NotFoundError) { return { message: error.message, statusCode: 404, }; } if (error instanceof error_1.BadRequestError || error instanceof error_1.BadResourceError) { return { message: error.message, statusCode: 400, }; } if (error instanceof error_1.ValidationError) { return { message: error.message, statusCode: 422, }; } if (error instanceof error_1.UnauthorizedError) { return { message: error.message, statusCode: 401, }; } if (error instanceof error_1.ForbiddenError) { return { message: error.message, statusCode: 403, }; } if (error instanceof error_1.InternalServerError) { return { message: error.message, statusCode: 500, }; } return { message: 'Something went wrong', statusCode: 500, }; } async execute(request, reply) { try { const result = await this.handler(request); if (!reply.sent) { await reply.send(result); } return result; } catch (error) { if (!reply.sent) { const errorResponse = this.handleError(error); const statusCode = typeof errorResponse === 'object' && errorResponse !== null && 'statusCode' in errorResponse ? errorResponse.statusCode || 500 : 500; let body = errorResponse; if (typeof errorResponse === 'object' && errorResponse !== null && 'statusCode' in errorResponse) { const { statusCode: _, ...rest } = errorResponse; body = rest; } await reply.code(statusCode).send(body); } throw error; } } } exports.AbstractRouteHandler = AbstractRouteHandler; //# sourceMappingURL=abstract.js.map