Skip to content

Commit 0b4ac83

Browse files
Pedro Brancoruimarinho
Pedro Branco
authored andcommitted
Fix bug in rpc error class
1 parent 1e0d75c commit 0b4ac83

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/errors/rpc-error.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import StandardError from './standard-error';
1111
*/
1212

1313
export default class RpcError extends StandardError {
14-
constructor(code, msg, props) {
14+
constructor(code, msg, props = {}) {
1515
if (typeof code != 'number') {
1616
throw new TypeError(`Non-numeric HTTP code`);
1717
}
@@ -21,9 +21,9 @@ export default class RpcError extends StandardError {
2121
msg = null;
2222
}
2323

24-
super(msg || STATUS_CODES[code], props);
24+
props.code = code;
2525

26-
this.code = code;
26+
super(msg || STATUS_CODES[code], props);
2727
}
2828

2929
get status() {

test/errors/rpc-error_test.js

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import RpcError from '../../src/errors/rpc-error';
7+
import { STATUS_CODES } from 'http';
78
import should from 'should';
89

910
/**
@@ -47,4 +48,10 @@ describe('RpcError', () => {
4748
it('should return a well-formatted string representation', () => {
4849
new RpcError(-32601, 'Method not found').toString().should.equal('RpcError: -32601 Method not found');
4950
});
51+
52+
it('should return the correct message by its http code', () => {
53+
for (const code in STATUS_CODES) {
54+
new RpcError(Number(code)).toString().should.equal(`RpcError: ${code} ${STATUS_CODES[code]}`);
55+
}
56+
});
5057
});

0 commit comments

Comments
 (0)