Skip to content

Commit

Permalink
fix for data serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew E. Rhyne committed Nov 11, 2016
1 parent de8b33b commit b10f55f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 27 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

1.0.2 / 2016-11-11
==================
* Fix for data serialization

1.0.1 / 2016-11-10
==================

Expand Down
26 changes: 14 additions & 12 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-errors",
"version": "1.0.1",
"version": "1.0.2",
"description": "Machine-readable custom errors for Apollostack's GraphQL server",
"main": "dist/index.js",
"scripts": {
Expand Down
23 changes: 11 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@ import ExtendableError from 'es6-error';

const errorMap = new Map();

const DELIMITER = ':';
const DELIMITER = '/::/';

const serializeName = (arr = []) => arr.reduce((str, val) => `${str.length > 0 ? str + DELIMITER : str}${val}`, '');
const deserializeName = (name = '') => {
const arr = [];
const str = name.split(DELIMITER);
arr.push(str.shift());
arr.push(str.join(DELIMITER));
return arr;
};
const serializeName = (arr = []) => arr.reduce((str, val) => `${str.length > 0 ? str + DELIMITER : str}${val.toString ? val.toString() : val}`, '');
const deserializeName = (name = '') => name.split(DELIMITER);

class ApolloError extends ExtendableError {
constructor (name, {
Expand All @@ -24,7 +18,10 @@ class ApolloError extends ExtendableError {

super(serializeName([
name,
t
t,
Object.assign({}, d, {
toString: () => JSON.stringify(d)
})
]));

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

export const formatError = (originalError, returnNull = false) => {
const [ name, thrown_at ] = deserializeName(originalError.message);
const [ name, thrown_at, d ] = deserializeName(originalError.message);
const data = d !== undefined ? JSON.parse(d) : {};
if (!name) return returnNull ? null : originalError;
const CustomError = errorMap.get(name);
if (!CustomError) return returnNull ? null : originalError;
const error = new CustomError({
thrown_at
thrown_at,
data
});
return error.serialize();
};
6 changes: 5 additions & 1 deletion test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ describe('formatError', () => {
message: 'A foo error has occurred'
});

const e = new FooError();
const e = new FooError({
data: {
oh: 'shit'
}
});

const s = formatError({
message: e.message
Expand Down

0 comments on commit b10f55f

Please sign in to comment.