Skip to content

Commit

Permalink
Merge branch 'release/3.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
yentsun committed Mar 30, 2018
2 parents 1d18142 + 92bc9c7 commit cd53227
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,8 @@
[3.3.0] - 2018-03-30
--------------------
- [x] Add custom error class #8


[3.2.2] - 2018-03-30
--------------------
- [x] Change `nats` to `tasu` @ logger
Expand Down
5 changes: 3 additions & 2 deletions index.js
Expand Up @@ -3,6 +3,7 @@ const {EventEmitter} = require('events');
const merge = require('lodash/merge');
const hyperid = require('hyperid');
const Logger = require('./lib/logger');
const RequestError = require('./lib/RequestError');


module.exports = class extends EventEmitter {
Expand Down Expand Up @@ -126,12 +127,12 @@ module.exports = class extends EventEmitter {
this._nats.requestOne(subject, JSON.stringify(message), null, this._options.requestTimeout, (response) => {
if (response.code && response.code === nats.REQ_TIMEOUT) {
this._logger.error('[!!', id, '!!] timeout', subject, message);
reject(new Error('response timeout'));
reject(new RequestError('response timeout', id));
} else {
const [error, res] = JSON.parse(response);
if (error) {
this._logger.error('[!!', id, '!!] ', error.message, error.stack);
reject(new Error(error.message));
reject(new RequestError(error.message, id));
} else {
const meta = JSON.parse(JSON.stringify(res));
this._logger.debug('[<<', id, '<<]', subject, meta);
Expand Down
10 changes: 10 additions & 0 deletions lib/RequestError.js
@@ -0,0 +1,10 @@
module.exports = class RequestError extends Error {

constructor (message, requestId) {

super(message || 'request error');
this.name = this.constructor.name;
Error.captureStackTrace(this, this.constructor);
this.requestId = requestId;
}
};
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "tasu",
"version": "3.2.2",
"version": "3.3.0",
"description": "A NATS-based message transport for Node.js services",
"main": "index.js",
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions test.js
Expand Up @@ -74,6 +74,7 @@ describe('tasu: options set', () => {
await tasu.request('request.error', {foo: 'bar'});
} catch (error) {
assert.equal(error.message, 'service error');
assert.isOk(error.requestId);
}
});

Expand All @@ -87,6 +88,7 @@ describe('tasu: options set', () => {
await tasu.request('request.error.detail', {foo: 'bar'});
} catch (error) {
assert.equal(error.message, 'service error');
assert.isOk(error.requestId);
}
});

Expand All @@ -95,6 +97,7 @@ describe('tasu: options set', () => {
await tasu.request('request.timeout', {foo: 'bar'});
} catch (error) {
assert.equal(error.message, 'response timeout');
assert.isOk(error.requestId);
}
}).timeout(110);
});
Expand Down

0 comments on commit cd53227

Please sign in to comment.