From 9be5546a69885a75d75d20f9a7aae8800effead7 Mon Sep 17 00:00:00 2001 From: "Andrew E. Rhyne" Date: Wed, 7 Dec 2016 13:34:41 -0800 Subject: [PATCH] Release 1.2.0 --- CHANGELOG.md | 8 ++++---- README.md | 4 ++-- dist/index.js | 3 ++- dist/index.js.map | 2 +- package.json | 2 +- src/index.js | 3 ++- test/spec.js | 3 ++- 7 files changed, 14 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 027086c..bfcdc58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,11 @@ -1.1.0 / 2016-12-01 + +1.2.0 / 2016-12-07 ================== - * Added option to show original locations and paths in error (#1, @scf4) + * Added option to override message when throwing error (@thebigredgeek) 1.1.0 / 2016-12-01 ================== - - * Added option to show original locations and paths in error + * Added option to show original locations and paths in error (#1, @scf4) 1.0.2 / 2016-11-11 ================== diff --git a/README.md b/README.md index 2010009..7ef6ac1 100644 --- a/README.md +++ b/README.md @@ -81,11 +81,11 @@ Witness glorious simplicity: ## API -### ApolloError ({ [time_thrown: String, data: Object]}) +### ApolloError ({ [time_thrown: String, data: Object, message: String ]}) Creates a new ApolloError object. Note that `ApolloError` in this context refers to an error class created and returned by `createError` documented below. Error can be -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). +initialized with a custom `time_thrown` ISODate (default is current ISODate), `data` object (which will be merged with data specified through `createError`, if it exists), and `message` (which will override the message specified through `createError`). ### createError(name, {message: String, [data: Object, options: Object]}): ApolloError diff --git a/dist/index.js b/dist/index.js index d38f1be..66fc43e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -52,6 +52,7 @@ 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 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, { @@ -61,7 +62,7 @@ var ApolloError = function (_ExtendableError) { })]))); _this._name = name; - _this._humanized_message = message || ''; + _this._humanized_message = m || ''; _this._time_thrown = t; _this._data = d; _this._locations = opts.showLocations && arguments[2] && arguments[2].locations; diff --git a/dist/index.js.map b/dist/index.js.map index 1e14456..6903d3f 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","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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AE5BJ,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 +{"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,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AN,GAAaA,IAAb;AACV,aAAOC,KAAP;AACD;;;;;;AAGI,IAAMC,oCAAc,SAAdA,WAAc,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 diff --git a/package.json b/package.json index a921670..a6da4c1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "apollo-errors", - "version": "1.1.0", + "version": "1.2.0", "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 883da79..92c1810 100644 --- a/src/index.js +++ b/src/index.js @@ -16,6 +16,7 @@ class ApolloError extends ExtendableError { }) { const t = (arguments[2] && arguments[2].thrown_at) || time_thrown; const d = Object.assign({}, data, ((arguments[2] && arguments[2].data) || {})); + const m = (arguments[2] && arguments[2].message) || message; const opts = Object.assign({}, options, ((arguments[2] && arguments[2].options) || {})); super(serializeName([ @@ -27,7 +28,7 @@ class ApolloError extends ExtendableError { ])); this._name = name; - this._humanized_message = message || ''; + this._humanized_message = m || ''; this._time_thrown = t; this._data = d; this._locations = (opts.showLocations && arguments[2] && arguments[2].locations) diff --git a/test/spec.js b/test/spec.js index d0a4562..7b632e4 100644 --- a/test/spec.js +++ b/test/spec.js @@ -18,6 +18,7 @@ describe('createError', () => { const iso = new Date().toISOString(); const e = new FooError({ + message: 'A foo 2.0 error has occurred', data: { foo: 'bar' }, @@ -29,7 +30,7 @@ describe('createError', () => { const { message, name, time_thrown, data } = e.serialize(); - expect(message).to.equal('A foo error has occurred'); + expect(message).to.equal('A foo 2.0 error has occurred'); expect(name).to.equal('FooError'); expect(time_thrown).to.equal(e._time_thrown); expect(data).to.eql({