diff --git a/CHANGELOG.md b/CHANGELOG.md index bfcdc58..ea639ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +1.2.1 / 2016-12-07 +================== + * Fixed a bug with overriding message serialization 1.2.0 / 2016-12-07 ================== @@ -13,5 +16,4 @@ 1.0.1 / 2016-11-10 ================== - * Initial release (@thebigredgeek) diff --git a/dist/index.js b/dist/index.js index 66fc43e..7a099b5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -55,7 +55,7 @@ var ApolloError = function (_ExtendableError) { var m = arguments[2] && arguments[2].message || message; var opts = Object.assign({}, options, arguments[2] && arguments[2].options || {}); - var _this = _possibleConstructorReturn(this, (ApolloError.__proto__ || Object.getPrototypeOf(ApolloError)).call(this, serializeName([name, t, Object.assign({}, d, { + var _this = _possibleConstructorReturn(this, (ApolloError.__proto__ || Object.getPrototypeOf(ApolloError)).call(this, serializeName([name, t, m !== message ? m : 'null', Object.assign({}, d, { toString: function toString() { return JSON.stringify(d); } @@ -106,10 +106,11 @@ 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, 3), + _deserializeName2 = _slicedToArray(_deserializeName, 4), name = _deserializeName2[0], thrown_at = _deserializeName2[1], - d = _deserializeName2[2]; + m = _deserializeName2[2], + d = _deserializeName2[3]; var locations = originalError.locations, path = originalError.path; @@ -119,6 +120,7 @@ var formatError = exports.formatError = function formatError(originalError) { var CustomError = errorMap.get(name); if (!CustomError) return returnNull ? null : originalError; var error = new CustomError({ + message: m === 'null' ? undefined : m, thrown_at: thrown_at, data: data, locations: locations, diff --git a/dist/index.js.map b/dist/index.js.map index 6903d3f..20173b4 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","toString","deserializeName","name","split","ApolloError","message","time_thrown","Date","toISOString","data","options","t","arguments","thrown_at","d","Object","assign","m","opts","JSON","stringify","_name","_humanized_message","_time_thrown","_data","_locations","showLocations","locations","_path","showPath","path","error","createError","e","bind","set","formatError","originalError","returnNull","undefined","parse","CustomError","get","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,QAKG;AAAA,QAJDG,OAIC,QAJDA,OAIC;AAAA,gCAHDC,WAGC;AAAA,QAHDA,WAGC,oCAHc,IAAIC,IAAJ,EAAD,CAAaC,WAAb,EAGb;AAAA,yBAFDC,IAEC;AAAA,QAFDA,IAEC,6BAFM,EAEN;AAAA,4BADDC,OACC;AAAA,QADDA,OACC,gCADS,EACT;;AAAA;;AACD,QAAMC,IAAKC,UAAU,CAAV,KAAgBA,UAAU,CAAV,EAAaC,SAA9B,IAA4CP,WAAtD;AACA,QAAMQ,IAAIC,OAAOC,MAAP,CAAc,EAAd,EAAkBP,IAAlB,EAA0BG,UAAU,CAAV,KAAgBA,UAAU,CAAV,EAAaH,IAA9B,IAAuC,EAAhE,CAAV;AACA,QAAMQ,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AAc,CAAC9B,IAAD,EAAgE;AAAA,MAAzDO,IAAyD,uEAAlD,EAAEJ,SAAS,uBAAX,EAAoCK,gBAApC,EAAkD;;AACzF,MAAMuB,IAAI7B,YAAY8B,IAAZ,CAAiB,IAAjB,EAAuBhC,IAAvB,EAA6BO,IAA7B,CAAV;AACAlB,WAAS4C,GAAT,CAAajC,IAAb,EAAmB+B,CAAnB;AACA,SAAOA,CAAP;AACD,CAJM;;AAMA,IAAMG,oCAAc,SAAdA,WAAc,CAACC,aAAD,EAAuC;AAAA,MAAvBC,UAAuB,uEAAV,KAAU;;AAAA,yBACjCrC,gBAAgBoC,cAAchC,OAA9B,CADiC;AAAA;AAAA,MACxDH,IADwD;AAAA,MAClDW,SADkD;AAAA,MACvCC,CADuC;;AAAA,MAExDa,SAFwD,GAEpCU,aAFoC,CAExDV,SAFwD;AAAA,MAE7CG,IAF6C,GAEpCO,aAFoC,CAE7CP,IAF6C;;AAGhE,MAAMrB,OAAOK,MAAMyB,SAAN,GAAkBpB,KAAKqB,KAAL,CAAW1B,CAAX,CAAlB,GAAkC,EAA/C;AACA,MAAI,CAACZ,IAAL,EAAW,OAAOoC,aAAa,IAAb,GAAoBD,aAA3B;AACX,MAAMI,cAAclD,SAASmD,GAAT,CAAaxC,IAAb,CAApB;AACA,MAAI,CAACuC,WAAL,EAAkB,OAAOH,aAAa,IAAb,GAAoBD,aAA3B;AAClB,MAAMN,QAAQ,IAAIU,WAAJ,CAAgB;AAC5B5B,wBAD4B;AAE5BJ,cAF4B;AAG5BkB,wBAH4B;AAI5BG;AAJ4B,GAAhB,CAAd;AAMA,SAAOC,MAAMY,SAAN,EAAP;AACD,CAdM","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 options = {},\n }) {\n const t = (arguments[2] && arguments[2].thrown_at) || time_thrown;\n const d = Object.assign({}, data, ((arguments[2] && arguments[2].data) || {}));\n const m = (arguments[2] && arguments[2].message) || message;\n const opts = Object.assign({}, options, ((arguments[2] && arguments[2].options) || {}));\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 = m || '';\n this._time_thrown = t;\n this._data = d;\n this._locations = (opts.showLocations && arguments[2] && arguments[2].locations)\n this._path = (opts.showPath && arguments[2] && arguments[2].path);\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 const locations = this._locations;\n const path = this._path;\n let error = {\n message,\n name,\n time_thrown,\n data,\n };\n if (locations) error.locations = locations;\n if (path) error.path = path;\n return error;\n }\n}\n\nexport const createError = (name, data = { message: 'An error has occurred', options }) => {\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 { locations, path } = originalError;\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 locations,\n path,\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","options","t","arguments","thrown_at","d","Object","assign","m","opts","JSON","stringify","_name","_humanized_message","_time_thrown","_data","_locations","showLocations","locations","_path","showPath","path","error","createError","e","bind","set","formatError","originalError","returnNull","undefined","parse","CustomError","get","serialize"],"mappings":";;;;;;;;;;;AAAA;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EAAuBhC,IAAvB,EAA6BO,IAA7B,CAAV;AACAlB,WAAS4C,GAAT,CAAajC,IAAb,EAAmB+B,CAAnB;AACA,SAAOA,CAAP;AACD,CAJM;;AAMA,IAAMG,oCAAc,SAAdA,WAAc,CAACC,aAAD,EAAuC;AAAA,MAAvBC,UAAuB,uEAAV,KAAU;;AAAA,yBAC9BrC,gBAAgBoC,cAAchC,OAA9B,CAD8B;AAAA;AAAA,MACxDH,IADwD;AAAA,MAClDW,SADkD;AAAA,MACvCI,CADuC;AAAA,MACpCH,CADoC;;AAAA,MAExDa,SAFwD,GAEpCU,aAFoC,CAExDV,SAFwD;AAAA,MAE7CG,IAF6C,GAEpCO,aAFoC,CAE7CP,IAF6C;;AAGhE,MAAMrB,OAAOK,MAAMyB,SAAN,GAAkBpB,KAAKqB,KAAL,CAAW1B,CAAX,CAAlB,GAAkC,EAA/C;AACA,MAAI,CAACZ,IAAL,EAAW,OAAOoC,aAAa,IAAb,GAAoBD,aAA3B;AACX,MAAMI,cAAclD,SAASmD,GAAT,CAAaxC,IAAb,CAApB;AACA,MAAI,CAACuC,WAAL,EAAkB,OAAOH,aAAa,IAAb,GAAoBD,aAA3B;AAClB,MAAMN,QAAQ,IAAIU,WAAJ,CAAgB;AAC5BpC,aAASY,MAAM,MAAN,GAAesB,SAAf,GAA2BtB,CADR;AAE5BJ,wBAF4B;AAG5BJ,cAH4B;AAI5BkB,wBAJ4B;AAK5BG;AAL4B,GAAhB,CAAd;AAOA,SAAOC,MAAMY,SAAN,EAAP;AACD,CAfM","file":"index.js","sourcesContent":["import ExtendableError from 'es6-error';\n\nconst errorMap = new Map();\n\nconst DELIMITER = '/::/';\n\nconst serializeName = (arr = []) => arr\n .reduce((str, val) => (\n `${str.length > 0 ? str + DELIMITER : str}${val.toString ? val.toString() : val}`\n ), '');\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 options = {},\n }) {\n const t = (arguments[2] && arguments[2].thrown_at) || time_thrown;\n const d = Object.assign({}, data, ((arguments[2] && arguments[2].data) || {}));\n const m = (arguments[2] && arguments[2].message) || message;\n const opts = Object.assign({}, options, ((arguments[2] && arguments[2].options) || {}));\n\n super(serializeName([\n name,\n t,\n (m !== message ? m : 'null'),\n Object.assign({}, d, {\n toString: () => JSON.stringify(d)\n }),\n ]));\n\n this._name = name;\n this._humanized_message = m || '';\n this._time_thrown = t;\n this._data = d;\n this._locations = (opts.showLocations && arguments[2] && arguments[2].locations)\n this._path = (opts.showPath && arguments[2] && arguments[2].path);\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 const locations = this._locations;\n const path = this._path;\n let error = {\n message,\n name,\n time_thrown,\n data,\n };\n if (locations) error.locations = locations;\n if (path) error.path = path;\n return error;\n }\n}\n\nexport const createError = (name, data = { message: 'An error has occurred', options }) => {\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, m, d ] = deserializeName(originalError.message);\n const { locations, path } = originalError;\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 message: m === 'null' ? undefined : m,\n thrown_at,\n data,\n locations,\n path,\n });\n return error.serialize();\n};\n"]} \ No newline at end of file diff --git a/package.json b/package.json index a6da4c1..b426957 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "apollo-errors", - "version": "1.2.0", + "version": "1.2.1", "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 92c1810..0ff50fb 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,10 @@ const errorMap = new Map(); const DELIMITER = '/::/'; -const serializeName = (arr = []) => arr.reduce((str, val) => `${str.length > 0 ? str + DELIMITER : str}${val.toString ? val.toString() : val}`, ''); +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 { @@ -22,6 +25,7 @@ class ApolloError extends ExtendableError { super(serializeName([ name, t, + (m !== message ? m : 'null'), Object.assign({}, d, { toString: () => JSON.stringify(d) }), @@ -60,13 +64,14 @@ export const createError = (name, data = { message: 'An error has occurred', opt }; export const formatError = (originalError, returnNull = false) => { - const [ name, thrown_at, d ] = deserializeName(originalError.message); + const [ name, thrown_at, m, d ] = deserializeName(originalError.message); const { locations, path } = originalError; 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({ + message: m === 'null' ? undefined : m, thrown_at, data, locations, diff --git a/test/spec.js b/test/spec.js index 7b632e4..daeeee0 100644 --- a/test/spec.js +++ b/test/spec.js @@ -55,6 +55,7 @@ describe('formatError', () => { }); const e = new FooError({ + message: 'A foo 2.0 error has occurred', data: { oh: 'shit' }