diff --git a/CHANGELOG.md b/CHANGELOG.md index c6c437f..966d44f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ + +1.0.2 / 2016-11-11 +================== + * Fix for data serialization + 1.0.1 / 2016-11-10 ================== diff --git a/dist/index.js b/dist/index.js index 06bcb74..33bf57d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -23,22 +23,17 @@ function _inherits(subClass, superClass) { if (typeof superClass !== "function" var errorMap = new Map(); -var DELIMITER = ':'; +var DELIMITER = '/::/'; var serializeName = function serializeName() { var arr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; return arr.reduce(function (str, val) { - return '' + (str.length > 0 ? str + DELIMITER : str) + val; + return '' + (str.length > 0 ? str + DELIMITER : str) + (val.toString ? val.toString() : val); }, ''); }; var deserializeName = function deserializeName() { var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; - - var arr = []; - var str = name.split(DELIMITER); - arr.push(str.shift()); - arr.push(str.join(DELIMITER)); - return arr; + return name.split(DELIMITER); }; var ApolloError = function (_ExtendableError) { @@ -56,7 +51,11 @@ var ApolloError = function (_ExtendableError) { var t = arguments[2] && arguments[2].thrown_at || time_thrown; var d = Object.assign({}, data, arguments[2] && arguments[2].data || {}); - var _this = _possibleConstructorReturn(this, (ApolloError.__proto__ || Object.getPrototypeOf(ApolloError)).call(this, serializeName([name, t]))); + var _this = _possibleConstructorReturn(this, (ApolloError.__proto__ || Object.getPrototypeOf(ApolloError)).call(this, serializeName([name, t, Object.assign({}, d, { + toString: function toString() { + return JSON.stringify(d); + } + })]))); _this._name = name; _this._humanized_message = message || ''; @@ -96,15 +95,18 @@ var formatError = exports.formatError = function formatError(originalError) { var returnNull = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var _deserializeName = deserializeName(originalError.message), - _deserializeName2 = _slicedToArray(_deserializeName, 2), + _deserializeName2 = _slicedToArray(_deserializeName, 3), name = _deserializeName2[0], - thrown_at = _deserializeName2[1]; + thrown_at = _deserializeName2[1], + d = _deserializeName2[2]; + var data = d !== undefined ? JSON.parse(d) : {}; if (!name) return returnNull ? null : originalError; var CustomError = errorMap.get(name); if (!CustomError) return returnNull ? null : originalError; var error = new CustomError({ - thrown_at: thrown_at + thrown_at: thrown_at, + data: data }); return error.serialize(); }; diff --git a/dist/index.js.map b/dist/index.js.map index 5c6d1ac..6f1b65b 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"sources":["../src/index.js"],"names":["errorMap","Map","DELIMITER","serializeName","arr","reduce","str","val","length","deserializeName","name","split","push","shift","join","ApolloError","message","time_thrown","Date","toISOString","data","t","arguments","thrown_at","d","Object","assign","_name","_humanized_message","_time_thrown","_data","createError","e","bind","set","formatError","originalError","returnNull","CustomError","get","error","serialize"],"mappings":";;;;;;;;;;;AAAA;;;;;;;;;;;;AAEA,IAAMA,WAAW,IAAIC,GAAJ,EAAjB;;AAEA,IAAMC,YAAY,GAAlB;;AAEA,IAAMC,gBAAgB,SAAhBA,aAAgB;AAAA,MAACC,GAAD,uEAAO,EAAP;AAAA,SAAcA,IAAIC,MAAJ,CAAW,UAACC,GAAD,EAAMC,GAAN;AAAA,iBAAiBD,IAAIE,MAAJ,GAAa,CAAb,GAAiBF,MAAMJ,SAAvB,GAAmCI,GAApD,IAA0DC,GAA1D;AAAA,GAAX,EAA4E,EAA5E,CAAd;AAAA,CAAtB;AACA,IAAME,kBAAkB,SAAlBA,eAAkB,GAAe;AAAA,MAAdC,IAAc,uEAAP,EAAO;;AACrC,MAAMN,MAAM,EAAZ;AACA,MAAME,MAAMI,KAAKC,KAAL,CAAWT,SAAX,CAAZ;AACAE,MAAIQ,IAAJ,CAASN,IAAIO,KAAJ,EAAT;AACAT,MAAIQ,IAAJ,CAASN,IAAIQ,IAAJ,CAASZ,SAAT,CAAT;AACA,SAAOE,GAAP;AACD,CAND;;IAQMW,W;;;AACJ,uBAAaL,IAAb,QAIG;AAAA,QAHDM,OAGC,QA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;AAAA,MACxDN,IADwD;AAAA,MAClDa,SADkD;;AAEhE,MAAI,CAACb,IAAL,EAAW,OAAO2B,aAAa,IAAb,GAAoBD,aAA3B;AACX,MAAME,cAActC,SAASuC,GAAT,CAAa7B,IAAb,CAApB;AACA,MAAI,CAAC4B,WAAL,EAAkB,OAAOD,aAAa,IAAb,GAAoBD,aAA3B;AAClB,MAAMI,QAAQ,IAAIF,WAAJ,CAAgB;AAC5Bf;AAD4B,GAAhB,CAAd;AAGA,SAAOiB,MAAMC,SAAN,EAAP;AACD,CATM","file":"index.js","sourcesContent":["import ExtendableError from 'es6-error';\n\nconst errorMap = new Map();\n\nconst DELIMITER = ':';\n\nconst serializeName = (arr = []) => arr.reduce((str, val) => `${str.length > 0 ? str + DELIMITER : str}${val}`, '');\nconst deserializeName = (name = '') => {\n const arr = [];\n const str = name.split(DELIMITER);\n arr.push(str.shift());\n arr.push(str.join(DELIMITER));\n return arr;\n};\n\nclass ApolloError extends ExtendableError {\n constructor (name, {\n message,\n time_thrown = (new Date()).toISOString(),\n data = {}\n }) {\n const t = (arguments[2] && arguments[2].thrown_at) || time_thrown;\n const d = Object.assign({}, data, ((arguments[2] && arguments[2].data) || {}));\n\n super(serializeName([\n name,\n t\n ]));\n\n this._name = name;\n this._humanized_message = message || '';\n this._time_thrown = t;\n this._data = d;\n }\n serialize () {\n const name = this._name;\n const message = this._humanized_message;\n const time_thrown = this._time_thrown;\n const data = this._data;\n return {\n message,\n name,\n time_thrown,\n data\n };\n }\n}\n\nexport const createError = (name, data = { message: 'An error has occurred' }) => {\n const e = ApolloError.bind(null, name, data);\n errorMap.set(name, e);\n return e;\n};\n\nexport const formatError = (originalError, returnNull = false) => {\n const [ name, thrown_at ] = deserializeName(originalError.message);\n if (!name) return returnNull ? null : originalError;\n const CustomError = errorMap.get(name);\n if (!CustomError) return returnNull ? null : originalError;\n const error = new CustomError({\n thrown_at\n });\n return error.serialize();\n};\n"]} \ No newline at end of file +{"version":3,"sources":["../src/index.js"],"names":["errorMap","Map","DELIMITER","serializeName","arr","reduce","str","val","length","toString","deserializeName","name","split","ApolloError","message","time_thrown","Date","toISOString","data","t","arguments","thrown_at","d","Object","assign","JSON","stringify","_name","_humanized_message","_time_thrown","_data","createError","e","bind","set","formatError","originalError","returnNull","undefined","parse","CustomError","get","error","serialize"],"mappings":";;;;;;;;;;;AAAA;;;;;;;;;;;;AAEA,IAAMA,WAAW,IAAIC,GAAJ,EAAjB;;AAEA,IAAMC,YAAY,MAAlB;;AAEA,IAAMC,gBAAgB,SAAhBA,aAAgB;AAAA,MAACC,GAAD,uEAAO,EAAP;AAAA,SAAcA,IAAIC,MAAJ,CAAW,UAACC,GAAD,EAAMC,GAAN;AAAA,iBAAiBD,IAAIE,MAAJ,GAAa,CAAb,GAAiBF,MAAMJ,SAAvB,GAAmCI,GAApD,KAA0DC,IAAIE,QAAJ,GAAeF,IAAIE,QAAJ,EAAf,GAAgCF,GAA1F;AAAA,GAAX,EAA4G,EAA5G,CAAd;AAAA,CAAtB;AACA,IAAMG,kBAAkB,SAAlBA,eAAkB;AAAA,MAACC,IAAD,uEAAQ,EAAR;AAAA,SAAeA,KAAKC,KAAL,CAAWV,SAAX,CAAf;AAAA,CAAxB;;IAEMW,W;;;AACJ,uBAAaF,IAAb,QAIG;AAAA,QAHDG,OAGC,QAHDA,OAGC;AAAA,gCAFDC,WAEC;AAAA,QAFDA,WAEC,oCAFc,IAAIC,IAAJ,EAAD,CAAaC,WAAb,EAEb;AAAA,yBADDC,IACC;AAAA,QADDA,IACC,6BADM,EACN;;AAAA;;AACD,QAAMC,IAAKC,UAAU,CAAV,KAAgBA,UAAU,CAAV,EAAaC,SAA9B,IAA4CN,WAAtD;AACA,QAAMO,IAAIC,OAAOC,MAAP,CAAc,EAAd,EAAkBN,IAAlB,EAA0BE,UAAU,CAAV,KAAgBA,UAAU,CAAV,EAAaF,IAA9B,IAAuC,EAAhE,CAAV;;AAFC,0HAIKf,cAAc,CAClBQ,IADkB,EAElBQ,C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ApB;AACA,MAAI,CAAC6B,WAAL,EAAkB,OAAOH,aAAa,IAAb,GAAoBD,aAA3B;AAClB,MAAMM,QAAQ,IAAIF,WAAJ,CAAgB;AAC5BnB,wBAD4B;AAE5BH;AAF4B,GAAhB,CAAd;AAIA,SAAOwB,MAAMC,SAAN,EAAP;AACD,CAXM","file":"index.js","sourcesContent":["import ExtendableError from 'es6-error';\n\nconst errorMap = new Map();\n\nconst DELIMITER = '/::/';\n\nconst serializeName = (arr = []) => arr.reduce((str, val) => `${str.length > 0 ? str + DELIMITER : str}${val.toString ? val.toString() : val}`, '');\nconst deserializeName = (name = '') => name.split(DELIMITER);\n\nclass ApolloError extends ExtendableError {\n constructor (name, {\n message,\n time_thrown = (new Date()).toISOString(),\n data = {}\n }) {\n const t = (arguments[2] && arguments[2].thrown_at) || time_thrown;\n const d = Object.assign({}, data, ((arguments[2] && arguments[2].data) || {}));\n\n super(serializeName([\n name,\n t,\n Object.assign({}, d, {\n toString: () => JSON.stringify(d)\n })\n ]));\n\n this._name = name;\n this._humanized_message = message || '';\n this._time_thrown = t;\n this._data = d;\n }\n serialize () {\n const name = this._name;\n const message = this._humanized_message;\n const time_thrown = this._time_thrown;\n const data = this._data;\n return {\n message,\n name,\n time_thrown,\n data\n };\n }\n}\n\nexport const createError = (name, data = { message: 'An error has occurred' }) => {\n const e = ApolloError.bind(null, name, data);\n errorMap.set(name, e);\n return e;\n};\n\nexport const formatError = (originalError, returnNull = false) => {\n const [ name, thrown_at, d ] = deserializeName(originalError.message);\n const data = d !== undefined ? JSON.parse(d) : {};\n if (!name) return returnNull ? null : originalError;\n const CustomError = errorMap.get(name);\n if (!CustomError) return returnNull ? null : originalError;\n const error = new CustomError({\n thrown_at,\n data\n });\n return error.serialize();\n};\n"]} \ No newline at end of file diff --git a/package.json b/package.json index 6f251ee..816ab3a 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/index.js b/src/index.js index 8c69338..775d8dc 100644 --- a/src/index.js +++ b/src/index.js @@ -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, { @@ -24,7 +18,10 @@ class ApolloError extends ExtendableError { super(serializeName([ name, - t + t, + Object.assign({}, d, { + toString: () => JSON.stringify(d) + }) ])); this._name = name; @@ -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(); }; diff --git a/test/spec.js b/test/spec.js index 86d5488..5fc0dce 100644 --- a/test/spec.js +++ b/test/spec.js @@ -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