Skip to content

Commit

Permalink
feature - Added -x-status-code
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Larichev committed Feb 25, 2020
1 parent 2e0eff1 commit ddf6d36
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<number[], number>('sum.rpc', [1, 2, 3]).then(reply => {
//...
});
this.rmqService.send<number[], number>('sum.rpc', [1, 2, 3])
.then(reply => {
//...
})
.catch(error: RMQError => {
//...
});
```

If you want to just notify services:
Expand Down Expand Up @@ -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');
//...
}
```
Expand Down
8 changes: 8 additions & 0 deletions lib/classes/rmq-error.class.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
1 change: 1 addition & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
15 changes: 12 additions & 3 deletions lib/rmq.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 {
Expand Down Expand Up @@ -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()}`);
Expand Down Expand Up @@ -172,14 +176,15 @@ 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)), {
correlationId: msg.properties.correlationId,
headers: error
? {
'-x-error': error.message,
'-x-status-code': this.isRMQError(error) ? error.statusCode : DEFAULT_ERROR_CODE
}
: null,
});
Expand Down Expand Up @@ -221,4 +226,8 @@ export class RMQService {
}
return res;
}

private isRMQError(error: Error | RMQError): error is RMQError {
return (error as RMQError).statusCode !== undefined;
}
}
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"rootDir": "./lib",
"skipLibCheck": true
},
"include": ["lib/**/*"],
"include": [
"lib/**/*"
],
"exclude": ["node_modules", "**/*.spec.ts"]
}

0 comments on commit ddf6d36

Please sign in to comment.