Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in rpc error class #42

Merged
merged 1 commit into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/errors/rpc-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import StandardError from './standard-error';
*/

export default class RpcError extends StandardError {
constructor(code, msg, props) {
constructor(code, msg, props = {}) {
if (typeof code != 'number') {
throw new TypeError(`Non-numeric HTTP code`);
}
Expand All @@ -21,9 +21,9 @@ export default class RpcError extends StandardError {
msg = null;
}

super(msg || STATUS_CODES[code], props);
props.code = code;

this.code = code;
super(msg || STATUS_CODES[code], props);
}

get status() {
Expand Down
7 changes: 7 additions & 0 deletions test/errors/rpc-error_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import RpcError from '../../src/errors/rpc-error';
import { STATUS_CODES } from 'http';
import should from 'should';

/**
Expand Down Expand Up @@ -47,4 +48,10 @@ describe('RpcError', () => {
it('should return a well-formatted string representation', () => {
new RpcError(-32601, 'Method not found').toString().should.equal('RpcError: -32601 Method not found');
});

it('should return the correct message by its http code', () => {
for (const code in STATUS_CODES) {
new RpcError(Number(code)).toString().should.equal(`RpcError: ${code} ${STATUS_CODES[code]}`);
}
});
});