Skip to content

Commit

Permalink
feat(rpc/errorToObject): extract errorToObject function
Browse files Browse the repository at this point in the history
  • Loading branch information
CheerlessCloud committed Sep 27, 2018
1 parent 17f6b4c commit ad8b109
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/rpc/errorToObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @flow
export default function errorToObject(error: ?Error): void | Object {
const errorObj = {};

if (error === undefined) {
return undefined;
} else if (error === null) {
return undefined;
}

Object.getOwnPropertyNames(error).forEach(key => {
if (/.*__old\d+/.test(key)) {
return;
}

errorObj[key] = (error: any)[key];
});

return errorObj;
}
26 changes: 26 additions & 0 deletions src/rpc/errorToObject.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @flow
import test from 'ava';
import EError from 'eerror';
import errorToObject from './errorToObject';

test('call with undefined', t => {
t.is(errorToObject(undefined), undefined);
});

test('call with null', t => {
t.is(errorToObject(null), undefined);
});

test('call with eerror', t => {
const error = new EError('My awesome error').combine({
name: 'AwesomeError',
foo: { bar: 42 },
});

t.deepEqual(errorToObject(error), {
name: 'AwesomeError',
foo: { bar: 42 },
message: 'My awesome error',
stack: error.stack,
});
});

0 comments on commit ad8b109

Please sign in to comment.