|
1 | 1 | # apollo-errors
|
2 |
| -A sane way to create and throw custom errors with Apollo's graphql server |
| 2 | +Machine-readable custom errors for Apollostack's GraphQL server |
| 3 | + |
| 4 | +[](https://nodei.co/npm/apollo-errors/) |
| 5 | + |
| 6 | +[](https://circleci.com/gh/thebigredgeek/apollo-errors/tree/master) |
| 7 | + |
| 8 | + |
| 9 | +## Installation and usage |
| 10 | + |
| 11 | +Install the package: |
| 12 | + |
| 13 | +```bash |
| 14 | +npm install apollo-errors |
| 15 | +``` |
| 16 | + |
| 17 | +Create some errors: |
| 18 | + |
| 19 | +```javascript |
| 20 | +import { createError } from 'apollo-errors'; |
| 21 | + |
| 22 | +export const FooError = createError('FooError', { |
| 23 | + message: 'A foo error has occurred' |
| 24 | +}); |
| 25 | +``` |
| 26 | + |
| 27 | +Hook up formatting: |
| 28 | + |
| 29 | +```javascript |
| 30 | +import express from 'express'; |
| 31 | +import bodyParser from 'body-parser'; |
| 32 | +import { formatError } from 'apollo-errors'; |
| 33 | +import schema from './schema'; |
| 34 | + |
| 35 | +const app = express(); |
| 36 | + |
| 37 | +app.use('/graphql', |
| 38 | + bodyParser.json(), |
| 39 | + graphqlExpress({ |
| 40 | + formatError, |
| 41 | + schema |
| 42 | + }) |
| 43 | +); |
| 44 | + |
| 45 | +app.listen(8080) |
| 46 | +``` |
| 47 | + |
| 48 | +Throw some errors: |
| 49 | + |
| 50 | +```javascript |
| 51 | +import { FooError } from './errors'; |
| 52 | + |
| 53 | +const resolverThatThrowsError = (root, params, context) => { |
| 54 | + throw new FooError({ |
| 55 | + data: { |
| 56 | + something: 'important' |
| 57 | + } |
| 58 | + }); |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +Witness glorious simplicity: |
| 63 | + |
| 64 | +`POST /graphql (200)` |
| 65 | + |
| 66 | +```json |
| 67 | +{ |
| 68 | + "data": {}, |
| 69 | + "errors": [ |
| 70 | + { |
| 71 | + "message":"A foo error has occurred", |
| 72 | + "name":"FooError", |
| 73 | + "time_thrown":"2016-11-11T00:40:50.954Z", |
| 74 | + "data":{ |
| 75 | + "something": "important" |
| 76 | + } |
| 77 | + } |
| 78 | + ] |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +## API |
| 83 | + |
| 84 | +### ApolloError ({ [time_thrown: String, data: Object]}) |
| 85 | + |
| 86 | +Creates a new ApolloError object. Note that `ApolloError` in this context refers |
| 87 | +to an error class created and returned by `createError` documented below. Error can be |
| 88 | +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). |
| 89 | + |
| 90 | + |
| 91 | +### createError(name, {message: String, [data: Object]}): ApolloError |
| 92 | + |
| 93 | +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. |
| 94 | + |
| 95 | +### formatError (error, strict = false): ApolloError|Error|null |
| 96 | +If the error is a known ApolloError, returns the serialized form of said error. |
| 97 | + |
| 98 | +**Otherwise**, *if strict is not truthy*, returns the original error passed into formatError. |
| 99 | + |
| 100 | +**Otherwise**, *if strict is truthy*, returns null. |
| 101 | + |
| 102 | +## TODO |
| 103 | + |
| 104 | +- Add better docs |
0 commit comments