UNPKG

@diy0r/nestjs-rabbitmq

Version:
88 lines 3.74 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RMQErrorHandler = exports.RMQError = exports.RmqErrorService = exports.RmqErrorGlobalService = void 0; const common_1 = require("@nestjs/common"); const constants_1 = require("../constants"); const os_1 = require("os"); let RmqErrorGlobalService = class RmqErrorGlobalService { buildError(error) { if (!error) return null; let errorHeaders = {}; errorHeaders['-x-error'] = error.message; errorHeaders['-x-host'] = (0, os_1.hostname)(); errorHeaders['-x-service'] = error.service; if (this.isRMQError(error)) { errorHeaders = { ...errorHeaders, '-x-date': error.date, '-x-status-code': error.status, }; } return errorHeaders; } errorHandler(msg) { const { headers } = msg.properties; const errorHandler = this.rmQoptions.extendedOptions?.globalBroker.replyTo.errorHandler; return errorHandler ? errorHandler.handle(headers) : RMQErrorHandler.handle(headers); } isRMQError(error) { return error.status !== undefined; } }; exports.RmqErrorGlobalService = RmqErrorGlobalService; __decorate([ (0, common_1.Inject)(constants_1.RMQ_OPTIONS), __metadata("design:type", Object) ], RmqErrorGlobalService.prototype, "rmQoptions", void 0); exports.RmqErrorGlobalService = RmqErrorGlobalService = __decorate([ (0, common_1.Injectable)() ], RmqErrorGlobalService); let RmqErrorService = class RmqErrorService extends RmqErrorGlobalService { constructor(options) { super(); this.options = options; } errorHandler(msg) { const { headers } = msg.properties; const errorHandler = this.options.replyTo.errorHandler; return errorHandler ? errorHandler.handle(headers) : RMQErrorHandler.handle(headers); } }; exports.RmqErrorService = RmqErrorService; exports.RmqErrorService = RmqErrorService = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)(constants_1.RMQ_BROKER_OPTIONS)), __metadata("design:paramtypes", [Object]) ], RmqErrorService); class RMQError extends Error { constructor(message, service, status, host, date) { super(); Object.setPrototypeOf(this, new.target.prototype); this.message = message; this.date = date; this.status = status; this.host = host; this.service = service; } } exports.RMQError = RMQError; class RMQErrorHandler { static handle(headers) { return new RMQError(headers['-x-error'], headers['-x-service'], headers['-x-status-code'], headers['-x-host'], headers['-x-date']); } } exports.RMQErrorHandler = RMQErrorHandler; //# sourceMappingURL=error.class.js.map