Skip to content

Commit f568e15

Browse files
committed
feat: Add better error handling support
1 parent f90d425 commit f568e15

File tree

6 files changed

+4725
-19
lines changed

6 files changed

+4725
-19
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
language: node_js
22
node_js:
3-
- "7"
3+
- "10"

README.md

+57-1
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,68 @@ const productResolver = {
392392
if (results.length > 0)
393393
return results
394394
else
395-
throw graphqlError(404, `Product with id ${id} does not exist.`)
395+
throw graphqlError({ code: 404, text: `Product with id ${id} does not exist.` })
396396
}
397397
}
398398
}
399399
```
400400

401+
In case of errors, the response will look like this:
402+
403+
```js
404+
{
405+
"errors": [
406+
{
407+
"message": "Product with id 123 does not exist."
408+
}
409+
],
410+
"data": {
411+
"products": null
412+
}
413+
}
414+
```
415+
416+
The `graphqlError` function also supports serializing errors:
417+
418+
```js
419+
throw graphqlError({ code: 422, errors:[error] })
420+
```
421+
422+
The output in case of errors is similar to:
423+
424+
```js
425+
{
426+
"errors": [
427+
{
428+
"message": "Some error message",
429+
"locations": [
430+
{
431+
"line": 382,
432+
"col": 17,
433+
"method": "wrapErrors",
434+
"path": "/Users/nicolasdao/Documents/myproject/src/services/_utils.js"
435+
},
436+
{
437+
"line": 65,
438+
"col": 19,
439+
"method": "onFulfilled",
440+
"path": "/Users/nicolasdao/Documents/myproject/src/services/core/node_modules/co/index.js"
441+
}
442+
]
443+
}
444+
],
445+
"data": {
446+
"products": null
447+
}
448+
}
449+
```
450+
451+
If the stack information in the `locations` field are sensitive, they can be turned off as follow:
452+
453+
```js
454+
throw graphqlError({ code: 422, errors:[error], noStack:true })
455+
```
456+
401457
### API
402458

403459
__*graphqlError('Oops, the product does not exist.')*__

0 commit comments

Comments
 (0)