From ddf6d36f46c65e3c88864b5d7cfcaf31686ba67b Mon Sep 17 00:00:00 2001 From: Anton Larichev Date: Tue, 25 Feb 2020 11:20:51 +0300 Subject: [PATCH] feature - Added -x-status-code --- CHANGELOG.md | 4 ++++ README.md | 15 ++++++++++----- lib/classes/rmq-error.class.ts | 8 ++++++++ lib/constants.ts | 1 + lib/index.ts | 1 + lib/rmq.service.ts | 15 ++++++++++++--- package-lock.json | 8 ++++---- package.json | 4 ++-- tsconfig.json | 4 +++- 9 files changed, 45 insertions(+), 15 deletions(-) create mode 100644 lib/classes/rmq-error.class.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 75cbd01..ba6da3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change log +## 1.4.0 + +- Added -x-status-code and RMQError class + ## 1.3.3 - Fixed no RMQRoute issue, added error message diff --git a/README.md b/README.md index 7f737d4..9d5b980 100644 --- a/README.md +++ b/README.md @@ -145,9 +145,13 @@ This method returns a Promise. First type - is a type you send, and the second - To get a reply: ```javascript -this.rmqService.send('sum.rpc', [1, 2, 3]).then(reply => { - //... -}); +this.rmqService.send('sum.rpc', [1, 2, 3]) + .then(reply => { + //... + }) + .catch(error: RMQError => { + //... + }); ``` If you want to just notify services: @@ -190,13 +194,14 @@ this.rmqService.send('sum.rpc', [1, 2, 3]).then(reply => { }); ``` -Each '@RMQRoute' topic will be automatically bound to queue specified in 'queueName' option. If you want to return an Error just throw it in your method: +Each '@RMQRoute' topic will be automatically bound to queue specified in 'queueName' option. If you want to return an Error just throw it in your method. To set '-x-status-code' use custom RMQError class. ```javascript @RMQRoute('my.rpc') myMethod(numbers: number[]): number { //... - throw new Error('Error message') + throw new RMQError('Error message', 2); + throw new Error('Error message'); //... } ``` diff --git a/lib/classes/rmq-error.class.ts b/lib/classes/rmq-error.class.ts new file mode 100644 index 0000000..928d352 --- /dev/null +++ b/lib/classes/rmq-error.class.ts @@ -0,0 +1,8 @@ +export class RMQError extends Error { + statusCode: number; + constructor(message: string, statusCode: number) { + super(message); + Object.setPrototypeOf(this, new.target.prototype); + this.statusCode = statusCode; + } +} diff --git a/lib/constants.ts b/lib/constants.ts index 1b3a606..a91ace7 100644 --- a/lib/constants.ts +++ b/lib/constants.ts @@ -14,6 +14,7 @@ export const ERROR_TIMEOUT: string = 'Response timeout error'; export const DEFAULT_RECONNECT_TIME: number = 5; export const DEFAULT_TIMEOUT: number = 30000; +export const DEFAULT_ERROR_CODE: number = 268435456; export const CUSTOM_LOGS = { recieved: { diff --git a/lib/index.ts b/lib/index.ts index 33027ed..38954ce 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -6,4 +6,5 @@ export * from './decorators/rmq-pipe.decorator'; export * from './decorators/validate.decorator'; export * from './classes/rmq-pipe.class'; export * from './classes/rmq-intercepter.class'; +export * from './classes/rmq-error.class'; export { Message } from 'amqplib'; diff --git a/lib/rmq.service.ts b/lib/rmq.service.ts index 3b726b1..b906f1d 100644 --- a/lib/rmq.service.ts +++ b/lib/rmq.service.ts @@ -12,7 +12,7 @@ import { ERROR_TIMEOUT, DEFAULT_TIMEOUT, CUSTOM_LOGS, - RMQ_ROUTES_META, + RMQ_ROUTES_META, DEFAULT_ERROR_CODE, } from './constants'; import { EventEmitter } from 'events'; import { Message, Channel } from 'amqplib'; @@ -24,6 +24,7 @@ import { responseEmitter, requestEmitter, ResponseEmmiterResult } from './emmite import * as amqp from 'amqp-connection-manager'; import 'reflect-metadata'; import { IQueueMeta } from './interfaces/queue-meta.interface'; +import { RMQError } from './classes/rmq-error.class'; @Injectable() export class RMQService { @@ -104,7 +105,10 @@ export class RMQService { clearTimeout(timerId); const { content } = msg; if (msg.properties.headers['-x-error']) { - reject(new Error(msg.properties.headers['-x-error'])); + reject(new RMQError( + msg.properties.headers['-x-error'], + msg.properties.headers['-x-status-code'] ?? DEFAULT_ERROR_CODE + )); } if (content.toString()) { this.logger.recieved(`[${topic}] ${content.toString()}`); @@ -172,7 +176,7 @@ export class RMQService { }); } - private async reply(res: any, msg: Message, error: Error = null) { + private async reply(res: any, msg: Message, error: Error | RMQError = null) { this.logger.recieved(`[${msg.fields.routingKey}] ${msg.content}`); res = await this.intercept(res, msg, error); this.channel.sendToQueue(msg.properties.replyTo, Buffer.from(JSON.stringify(res)), { @@ -180,6 +184,7 @@ export class RMQService { headers: error ? { '-x-error': error.message, + '-x-status-code': this.isRMQError(error) ? error.statusCode : DEFAULT_ERROR_CODE } : null, }); @@ -221,4 +226,8 @@ export class RMQService { } return res; } + + private isRMQError(error: Error | RMQError): error is RMQError { + return (error as RMQError).statusCode !== undefined; + } } diff --git a/package-lock.json b/package-lock.json index 683d9db..283fa00 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "nestjs-rmq", - "version": "1.3.3", + "version": "1.3.4", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -817,9 +817,9 @@ "dev": true }, "typescript": { - "version": "3.5.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.5.3.tgz", - "integrity": "sha512-ACzBtm/PhXBDId6a6sDJfroT2pOWt/oOnk4/dElG5G33ZL776N3Y6/6bKZJBFpd+b05F3Ct9qDjMeJmRWtE2/g==", + "version": "3.8.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.8.2.tgz", + "integrity": "sha512-EgOVgL/4xfVrCMbhYKUQTdF37SQn4Iw73H5BgCrF1Abdun7Kwy/QZsE/ssAy0y4LxBbvua3PIbFsbRczWWnDdQ==", "dev": true }, "url-parse": { diff --git a/package.json b/package.json index f7a1513..3338071 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nestjs-rmq", - "version": "1.3.4", + "version": "1.4.0", "description": "NestJS RabbitMQ Module", "main": "./dist/index.js", "types": "./dist/index.d.ts", @@ -38,7 +38,7 @@ "@types/node": "^12.11.7", "ts-node": "^7.0.1", "tslint": "^5.8.0", - "typescript": "^3.2.2" + "typescript": "^3.8.2" }, "peerDependencies": { "@nestjs/common": "^6.5.3" diff --git a/tsconfig.json b/tsconfig.json index a0e4c5d..f705bb6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,6 +13,8 @@ "rootDir": "./lib", "skipLibCheck": true }, - "include": ["lib/**/*"], + "include": [ + "lib/**/*" + ], "exclude": ["node_modules", "**/*.spec.ts"] }