From 901d30d587a581fb6b313f8be600ca442cf14a53 Mon Sep 17 00:00:00 2001 From: scf4 Date: Sat, 26 Nov 2016 20:00:54 +0000 Subject: [PATCH 1/6] Added option to show original locations and paths in error --- src/index.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 775d8dc..2e6712b 100644 --- a/src/index.js +++ b/src/index.js @@ -11,11 +11,14 @@ class ApolloError extends ExtendableError { constructor (name, { message, time_thrown = (new Date()).toISOString(), - data = {} + data = {}, + showLocations, + showPath, }) { const t = (arguments[2] && arguments[2].thrown_at) || time_thrown; const d = Object.assign({}, data, ((arguments[2] && arguments[2].data) || {})); - + const l = showLocations && (arguments[2] && arguments[2].locations); + const p = showPath && (arguments[2] && arguments[2].path); super(serializeName([ name, t, @@ -28,22 +31,28 @@ class ApolloError extends ExtendableError { this._humanized_message = message || ''; this._time_thrown = t; this._data = d; + this._locations = l; + this._path = p; } serialize () { const name = this._name; const message = this._humanized_message; const time_thrown = this._time_thrown; const data = this._data; + const locations = this._locations; + const path = this._path; return { message, name, time_thrown, - data + data, + locations, + path, }; } } -export const createError = (name, data = { message: 'An error has occurred' }) => { +export const createError = (name, data = { message: 'An error has occurred', showLocations, showPath }) => { const e = ApolloError.bind(null, name, data); errorMap.set(name, e); return e; @@ -51,13 +60,16 @@ export const createError = (name, data = { message: 'An error has occurred' }) = export const formatError = (originalError, returnNull = false) => { const [ name, thrown_at, 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({ thrown_at, - data + data, + locations, + path, }); return error.serialize(); }; From 87e90fc902d3bab1ba3973035ea127c90fb07422 Mon Sep 17 00:00:00 2001 From: scf4 Date: Tue, 29 Nov 2016 13:57:14 +0000 Subject: [PATCH 2/6] options --- dist/index.js | 22 ++++++++++++++++++---- dist/index.js.map | 2 +- src/index.js | 19 +++++++++---------- test/spec.js | 18 ++++++++++++++---- 4 files changed, 42 insertions(+), 19 deletions(-) diff --git a/dist/index.js b/dist/index.js index 33bf57d..52b4ecf 100644 --- a/dist/index.js +++ b/dist/index.js @@ -44,12 +44,15 @@ var ApolloError = function (_ExtendableError) { _ref$time_thrown = _ref.time_thrown, time_thrown = _ref$time_thrown === undefined ? new Date().toISOString() : _ref$time_thrown, _ref$data = _ref.data, - data = _ref$data === undefined ? {} : _ref$data; + data = _ref$data === undefined ? {} : _ref$data, + _ref$options = _ref.options, + options = _ref$options === undefined ? {} : _ref$options; _classCallCheck(this, ApolloError); var t = arguments[2] && arguments[2].thrown_at || time_thrown; var d = Object.assign({}, data, arguments[2] && arguments[2].data || {}); + 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, { toString: function toString() { @@ -61,6 +64,8 @@ var ApolloError = function (_ExtendableError) { _this._humanized_message = message || ''; _this._time_thrown = t; _this._data = d; + _this._locations = opts.showLocations && arguments[2] && arguments[2].locations; + _this._path = opts.showPath && arguments[2] && arguments[2].path; return _this; } @@ -71,11 +76,15 @@ var ApolloError = function (_ExtendableError) { var message = this._humanized_message; var time_thrown = this._time_thrown; var data = this._data; + var locations = this._locations || undefined; + var path = this._path || undefined; return { message: message, name: name, time_thrown: time_thrown, - data: data + data: data, + locations: locations, + path: path }; } }]); @@ -84,7 +93,7 @@ var ApolloError = function (_ExtendableError) { }(_es6Error2.default); var createError = exports.createError = function createError(name) { - var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { message: 'An error has occurred' }; + var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { message: 'An error has occurred', options: options }; var e = ApolloError.bind(null, name, data); errorMap.set(name, e); @@ -100,13 +109,18 @@ var formatError = exports.formatError = function formatError(originalError) { thrown_at = _deserializeName2[1], d = _deserializeName2[2]; + var locations = originalError.locations, + path = originalError.path; + 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, - data: data + data: data, + locations: locations, + path: path }); return error.serialize(); }; diff --git a/dist/index.js.map b/dist/index.js.map index 6f1b65b..551f88c 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","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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ACjC3B,gBAAgB0B,cAActB,OAA9B,CADiC;AAAA;AAAA,MACxDH,IADwD;AAAA,MAClDU,SADkD;AAAA,MACvCC,CADuC;;AAEhE,MAAMJ,OAAOI,MAAMgB,SAAN,GAAkBb,KAAKc,KAAL,CAAWjB,CAAX,CAAlB,GAAkC,EAA/C;AACA,MAAI,CAACX,IAAL,EAAW,OAAO0B,aAAa,IAAb,GAAoBD,aAA3B;AACX,MAAMI,cAAcxC,SAASyC,GAAT,CAAa9B,IAAb,CAApB;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 +{"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","opts","JSON","stringify","_name","_humanized_message","_time_thrown","_data","_locations","showLocations","locations","_path","showPath","path","undefined","createError","e","bind","set","formatError","originalError","returnNull","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,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,QAD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AHK;AAILG,kBAJK;AAKLiB,4BALK;AAMLG;AANK,OAAP;AAQD;;;;;;AAGI,IAAME,oCAAc,SAAdA,WAAc,CAAC7B,IAAD,EAAgE;AAAA,MAAzDO,IAAyD,uEAAlD,EAAEJ,SAAS,uBAAX,EAAoCK,gBAApC,EAAkD;;AACzF,MAAMsB,IAAI5B,YAAY6B,IAAZ,CAAiB,IAAjB,EAAuB/B,IAAvB,EAA6BO,IAA7B,CAAV;AACAlB,WAAS2C,GAAT,CAAahC,IAAb,EAAmB8B,CAAnB;AACA,SAAOA,CAAP;AACD,CAJM;;AAMA,IAAMG,oCAAc,SAAdA,WAAc,CAACC,aAAD,EAAuC;AAAA,MAAvBC,UAAuB,uEAAV,KAAU;;AAAA,yBACjCpC,gBAAgBmC,cAAc/B,OAA9B,CADiC;AAAA;AAAA,MACxDH,IADwD;AAAA,MAClDW,SADkD;AAAA,MACvCC,CADuC;;AAAA,MAExDY,SAFwD,GAEpCU,aAFoC,CAExDV,SAFwD;AAAA,MAE7CG,IAF6C,GAEpCO,aAFoC,CAE7CP,IAF6C;;AAGhE,MAAMpB,OAAOK,MAAMgB,SAAN,GAAkBZ,KAAKoB,KAAL,CAAWxB,CAAX,CAAlB,GAAkC,EAA/C;AACA,MAAI,CAACZ,IAAL,EAAW,OAAOmC,aAAa,IAAb,GAAoBD,aAA3B;AACX,MAAMG,cAAchD,SAASiD,GAAT,CAAatC,IAAb,CAApB;AACA,MAAI,CAACqC,WAAL,EAAkB,OAAOF,aAAa,IAAb,GAAoBD,aAA3B;AAClB,MAAMK,QAAQ,IAAIF,WAAJ,CAAgB;AAC5B1B,wBAD4B;AAE5BJ,cAF4B;AAG5BiB,wBAH4B;AAI5BG;AAJ4B,GAAhB,CAAd;AAMA,SAAOY,MAAMC,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 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 = message || '';\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 || undefined;\n const path = this._path || undefined;\n return {\n message,\n name,\n time_thrown,\n data,\n locations,\n path,\n };\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 diff --git a/src/index.js b/src/index.js index 2e6712b..c2c5be2 100644 --- a/src/index.js +++ b/src/index.js @@ -12,35 +12,34 @@ class ApolloError extends ExtendableError { message, time_thrown = (new Date()).toISOString(), data = {}, - showLocations, - showPath, + options = {}, }) { const t = (arguments[2] && arguments[2].thrown_at) || time_thrown; const d = Object.assign({}, data, ((arguments[2] && arguments[2].data) || {})); - const l = showLocations && (arguments[2] && arguments[2].locations); - const p = showPath && (arguments[2] && arguments[2].path); + const opts = Object.assign({}, options, ((arguments[2] && arguments[2].options) || {})); + super(serializeName([ name, t, Object.assign({}, d, { toString: () => JSON.stringify(d) - }) + }), ])); this._name = name; this._humanized_message = message || ''; this._time_thrown = t; this._data = d; - this._locations = l; - this._path = p; + this._locations = (opts.showLocations && arguments[2] && arguments[2].locations) + this._path = (opts.showPath && arguments[2] && arguments[2].path); } serialize () { const name = this._name; const message = this._humanized_message; const time_thrown = this._time_thrown; const data = this._data; - const locations = this._locations; - const path = this._path; + const locations = this._locations || undefined; + const path = this._path || undefined; return { message, name, @@ -52,7 +51,7 @@ class ApolloError extends ExtendableError { } } -export const createError = (name, data = { message: 'An error has occurred', showLocations, showPath }) => { +export const createError = (name, data = { message: 'An error has occurred', options }) => { const e = ApolloError.bind(null, name, data); errorMap.set(name, e); return e; diff --git a/test/spec.js b/test/spec.js index 5fc0dce..edb8a2f 100644 --- a/test/spec.js +++ b/test/spec.js @@ -8,7 +8,11 @@ describe('createError', () => { message: 'A foo error has occurred', data: { hello: 'world' - } + }, + options: { + showLocations: false, + showPath: true, + }, }); const iso = new Date().toISOString(); @@ -16,11 +20,15 @@ describe('createError', () => { const e = new FooError({ data: { foo: 'bar' - } + }, + options: { + showLocations: true, + showPath: false, + }, }); - const { message, name, time_thrown, data } = e.serialize(); - + const { message, name, time_thrown, data, locations, path } = e.serialize(); + expect(message).to.equal('A foo error has occurred'); expect(name).to.equal('FooError'); expect(time_thrown).to.equal(e._time_thrown); @@ -28,6 +36,8 @@ describe('createError', () => { hello: 'world', foo: 'bar' }); + expect(locations).to.be.undefined; + expect(path).to.be.undefined; }); }); From e1f3b20c9fdc71a05f00d4aeb7815f7570afb9e0 Mon Sep 17 00:00:00 2001 From: scf4 Date: Tue, 29 Nov 2016 16:52:57 +0000 Subject: [PATCH 3/6] removed undefined --- src/index.js | 11 ++++++----- test/spec.js | 6 ++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/index.js b/src/index.js index c2c5be2..883da79 100644 --- a/src/index.js +++ b/src/index.js @@ -38,16 +38,17 @@ class ApolloError extends ExtendableError { const message = this._humanized_message; const time_thrown = this._time_thrown; const data = this._data; - const locations = this._locations || undefined; - const path = this._path || undefined; - return { + const locations = this._locations; + const path = this._path; + let error = { message, name, time_thrown, data, - locations, - path, }; + if (locations) error.locations = locations; + if (path) error.path = path; + return error; } } diff --git a/test/spec.js b/test/spec.js index edb8a2f..d0a4562 100644 --- a/test/spec.js +++ b/test/spec.js @@ -27,8 +27,8 @@ describe('createError', () => { }, }); - const { message, name, time_thrown, data, locations, path } = e.serialize(); - + const { message, name, time_thrown, data } = e.serialize(); + expect(message).to.equal('A foo error has occurred'); expect(name).to.equal('FooError'); expect(time_thrown).to.equal(e._time_thrown); @@ -36,8 +36,6 @@ describe('createError', () => { hello: 'world', foo: 'bar' }); - expect(locations).to.be.undefined; - expect(path).to.be.undefined; }); }); From c614421d86caf064b2a83f7533052ec4786b864e Mon Sep 17 00:00:00 2001 From: scf4 Date: Tue, 29 Nov 2016 16:55:02 +0000 Subject: [PATCH 4/6] circle fix? --- dist/index.js | 13 +++++++------ dist/index.js.map | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/dist/index.js b/dist/index.js index 52b4ecf..d38f1be 100644 --- a/dist/index.js +++ b/dist/index.js @@ -76,16 +76,17 @@ var ApolloError = function (_ExtendableError) { var message = this._humanized_message; var time_thrown = this._time_thrown; var data = this._data; - var locations = this._locations || undefined; - var path = this._path || undefined; - return { + var locations = this._locations; + var path = this._path; + var error = { message: message, name: name, time_thrown: time_thrown, - data: data, - locations: locations, - path: path + data: data }; + if (locations) error.locations = locations; + if (path) error.path = path; + return error; } }]); diff --git a/dist/index.js.map b/dist/index.js.map index 551f88c..1e14456 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","opts","JSON","stringify","_name","_humanized_message","_time_thrown","_data","_locations","showLocations","locations","_path","showPath","path","undefined","createError","e","bind","set","formatError","originalError","returnNull","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,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,EAAh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IAAjB,EAAuB/B,IAAvB,EAA6BO,IAA7B,CAAV;AACAlB,WAAS2C,GAAT,CAAahC,IAAb,EAAmB8B,CAAnB;AACA,SAAOA,CAAP;AACD,CAJM;;AAMA,IAAMG,oCAAc,SAAdA,WAAc,CAACC,aAAD,EAAuC;AAAA,MAAvBC,UAAuB,uEAAV,KAAU;;AAAA,yBACjCpC,gBAAgBmC,cAAc/B,OAA9B,CADiC;AAAA;AAAA,MACxDH,IADwD;AAAA,MAClDW,SADkD;AAAA,MACvCC,CADuC;;AAAA,MAExDY,SAFwD,GAEpCU,aAFoC,CAExDV,SAFwD;AAAA,MAE7CG,IAF6C,GAEpCO,aAFoC,CAE7CP,IAF6C;;AAGhE,MAAMpB,OAAOK,MAAMgB,SAAN,GAAkBZ,KAAKoB,KAAL,CAAWxB,CAAX,CAAlB,GAAkC,EAA/C;AACA,MAAI,CAACZ,IAAL,EAAW,OAAOmC,aAAa,IAAb,GAAoBD,aAA3B;AACX,MAAMG,cAAchD,SAASiD,GAAT,CAAatC,IAAb,CAApB;AACA,MAAI,CAACqC,WAAL,EAAkB,OAAOF,aAAa,IAAb,GAAoBD,aAA3B;AAClB,MAAMK,QAAQ,IAAIF,WAAJ,CAAgB;AAC5B1B,wBAD4B;AAE5BJ,cAF4B;AAG5BiB,wBAH4B;AAI5BG;AAJ4B,GAAhB,CAAd;AAMA,SAAOY,MAAMC,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 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 = message || '';\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 || undefined;\n const path = this._path || undefined;\n return {\n message,\n name,\n time_thrown,\n data,\n locations,\n path,\n };\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","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,EA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AExDY,SAFwD,GAEpCU,aAFoC,CAExDV,SAFwD;AAAA,MAE7CG,IAF6C,GAEpCO,aAFoC,CAE7CP,IAF6C;;AAGhE,MAAMpB,OAAOK,MAAMwB,SAAN,GAAkBpB,KAAKqB,KAAL,CAAWzB,CAAX,CAAlB,GAAkC,EAA/C;AACA,MAAI,CAACZ,IAAL,EAAW,OAAOmC,aAAa,IAAb,GAAoBD,aAA3B;AACX,MAAMI,cAAcjD,SAASkD,GAAT,CAAavC,IAAb,CAApB;AACA,MAAI,CAACsC,WAAL,EAAkB,OAAOH,aAAa,IAAb,GAAoBD,aAA3B;AAClB,MAAMN,QAAQ,IAAIU,WAAJ,CAAgB;AAC5B3B,wBAD4B;AAE5BJ,cAF4B;AAG5BiB,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 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 = message || '';\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 From f325be8675afd9cfb42ae65be9b52c36a11c8a88 Mon Sep 17 00:00:00 2001 From: scf4 Date: Tue, 29 Nov 2016 17:45:35 +0000 Subject: [PATCH 5/6] added options to docs --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8c230eb..8f52e1f 100644 --- a/README.md +++ b/README.md @@ -88,9 +88,14 @@ to an error class created and returned by `createError` documented below. Error initialized with a custom `time_thrown` ISODate (default is current ISODate) and `data` object (which will be merged with data specified through `createError`, if it exists). -### createError(name, {message: String, [data: Object]}): ApolloError +### createError(name, {message: String, [data: Object, options: Object]}): ApolloError -Creates and returns an error class with the given `name` and `message`, optionally initialized with the given `data`. `data` passed to `createError` will later be merged with any data passed to the constructor. +Creates and returns an error class with the given `name` and `message`, optionally initialized with the given `data` and `options`. `data` passed to `createError` will later be merged with any data passed to the constructor. + +#### Options (default): + + - `showPath` *(false)*: Preserve the GraphQLError `path` data. + - `showLocations` *(false)*: Preserve the GraphQLError `path` data. ### formatError (error, strict = false): ApolloError|Error|null If the error is a known ApolloError, returns the serialized form of said error. From 8f437c04d03f97ccccc74098349e2ac37f32fea6 Mon Sep 17 00:00:00 2001 From: scf4 Date: Tue, 29 Nov 2016 17:46:19 +0000 Subject: [PATCH 6/6] typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8f52e1f..90c1714 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ Creates and returns an error class with the given `name` and `message`, optional #### Options (default): - `showPath` *(false)*: Preserve the GraphQLError `path` data. - - `showLocations` *(false)*: Preserve the GraphQLError `path` data. + - `showLocations` *(false)*: Preserve the GraphQLError `locations` data. ### formatError (error, strict = false): ApolloError|Error|null If the error is a known ApolloError, returns the serialized form of said error.