Skip to content

Commit b10f55f

Browse files
author
Andrew E. Rhyne
committed
fix for data serialization
1 parent de8b33b commit b10f55f

File tree

6 files changed

+37
-27
lines changed

6 files changed

+37
-27
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2+
1.0.2 / 2016-11-11
3+
==================
4+
* Fix for data serialization
5+
16
1.0.1 / 2016-11-10
27
==================
38

dist/index.js

+14-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "apollo-errors",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Machine-readable custom errors for Apollostack's GraphQL server",
55
"main": "dist/index.js",
66
"scripts": {

src/index.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@ import ExtendableError from 'es6-error';
22

33
const errorMap = new Map();
44

5-
const DELIMITER = ':';
5+
const DELIMITER = '/::/';
66

7-
const serializeName = (arr = []) => arr.reduce((str, val) => `${str.length > 0 ? str + DELIMITER : str}${val}`, '');
8-
const deserializeName = (name = '') => {
9-
const arr = [];
10-
const str = name.split(DELIMITER);
11-
arr.push(str.shift());
12-
arr.push(str.join(DELIMITER));
13-
return arr;
14-
};
7+
const serializeName = (arr = []) => arr.reduce((str, val) => `${str.length > 0 ? str + DELIMITER : str}${val.toString ? val.toString() : val}`, '');
8+
const deserializeName = (name = '') => name.split(DELIMITER);
159

1610
class ApolloError extends ExtendableError {
1711
constructor (name, {
@@ -24,7 +18,10 @@ class ApolloError extends ExtendableError {
2418

2519
super(serializeName([
2620
name,
27-
t
21+
t,
22+
Object.assign({}, d, {
23+
toString: () => JSON.stringify(d)
24+
})
2825
]));
2926

3027
this._name = name;
@@ -53,12 +50,14 @@ export const createError = (name, data = { message: 'An error has occurred' }) =
5350
};
5451

5552
export const formatError = (originalError, returnNull = false) => {
56-
const [ name, thrown_at ] = deserializeName(originalError.message);
53+
const [ name, thrown_at, d ] = deserializeName(originalError.message);
54+
const data = d !== undefined ? JSON.parse(d) : {};
5755
if (!name) return returnNull ? null : originalError;
5856
const CustomError = errorMap.get(name);
5957
if (!CustomError) return returnNull ? null : originalError;
6058
const error = new CustomError({
61-
thrown_at
59+
thrown_at,
60+
data
6261
});
6362
return error.serialize();
6463
};

test/spec.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ describe('formatError', () => {
4545
message: 'A foo error has occurred'
4646
});
4747

48-
const e = new FooError();
48+
const e = new FooError({
49+
data: {
50+
oh: 'shit'
51+
}
52+
});
4953

5054
const s = formatError({
5155
message: e.message

0 commit comments

Comments
 (0)