| 
 | 1 | +<h1 align="center">  | 
 | 2 | + <img src="https://cdn.rawgit.com/EasyGraphQL/easygraphql-mock/baab331c/EasyGraphQL.png" alt="EasyGraphQL " width="350">  | 
 | 3 | + <br>  | 
 | 4 | + EasyGraphQL Format Error  | 
 | 5 | + <br>  | 
 | 6 | + <br>  | 
 | 7 | +</h1>  | 
 | 8 | + | 
 | 9 | +Easy GraphQL Format Error is a node library used to handle the errors and return them with the respective  | 
 | 10 | +status code.  | 
 | 11 | + | 
 | 12 | +## Installation  | 
 | 13 | +```bash  | 
 | 14 | +$ npm install easygraphql-format-error --save  | 
 | 15 | +```  | 
 | 16 | + | 
 | 17 | +## Usage  | 
 | 18 | +To get started with the format error, you might need to follow the next steps:  | 
 | 19 | + | 
 | 20 | +### Basic  | 
 | 21 | +```js  | 
 | 22 | +// App.js  | 
 | 23 | +const FormatError = require('easygraphql-format-error')  | 
 | 24 | + | 
 | 25 | +const formatError = new FormatError()  | 
 | 26 | +// pass the errorName on the context  | 
 | 27 | +const errorName = formatError.errorName  | 
 | 28 | + | 
 | 29 | +app.use('/graphql', (req, res) => {  | 
 | 30 | + graphqlHTTP({  | 
 | 31 | + schema,  | 
 | 32 | + rootValue: root,  | 
 | 33 | + graphiql: true,  | 
 | 34 | + context: { errorName },  | 
 | 35 | + formatError: (err) => {  | 
 | 36 | + return formatError.getError(err)  | 
 | 37 | + }  | 
 | 38 | + })(req, res)  | 
 | 39 | +})  | 
 | 40 | + | 
 | 41 | +// Resolver  | 
 | 42 | +userInformation: ({ isLoggedIn }, { errorName }) => {  | 
 | 43 | + if (!isLoggedIn) {  | 
 | 44 | + throw new Error(errorName.UNAUTHORIZED)  | 
 | 45 | + }  | 
 | 46 | + | 
 | 47 | + return 'My username'  | 
 | 48 | +}  | 
 | 49 | + | 
 | 50 | +// If the user is not loggedIn the response will be  | 
 | 51 | +{  | 
 | 52 | + "errors": [  | 
 | 53 | + {  | 
 | 54 | + "message": "Unauthorized",  | 
 | 55 | + "statusCode": 401  | 
 | 56 | + }  | 
 | 57 | + ],  | 
 | 58 | + "data": null  | 
 | 59 | +}  | 
 | 60 | +```  | 
 | 61 | + | 
 | 62 | +### With Custom errors  | 
 | 63 | +You can pass custom error and access those errors from the resolver, calling `errorName.YOUR_ERROR_NAME`  | 
 | 64 | + | 
 | 65 | +```js  | 
 | 66 | +// App.js  | 
 | 67 | +const FormatError = require('easygraphql-format-error')  | 
 | 68 | + | 
 | 69 | +const formatError = new FormatError([{  | 
 | 70 | + name: 'INVALID_EMAIL',  | 
 | 71 | + message: 'The email is not valid',  | 
 | 72 | + statusCode: '400'  | 
 | 73 | +}])  | 
 | 74 | +// pass the errorName on the context  | 
 | 75 | +const errorName = formatError.errorName  | 
 | 76 | + | 
 | 77 | +app.use('/graphql', (req, res) => {  | 
 | 78 | + graphqlHTTP({  | 
 | 79 | + schema,  | 
 | 80 | + rootValue: root,  | 
 | 81 | + graphiql: true,  | 
 | 82 | + context: { errorName },  | 
 | 83 | + formatError: (err) => {  | 
 | 84 | + return formatError.getError(err)  | 
 | 85 | + }  | 
 | 86 | + })(req, res)  | 
 | 87 | +})  | 
 | 88 | + | 
 | 89 | +// Resolver  | 
 | 90 | +findUserByEmail: ({ email }, { errorName }) => {  | 
 | 91 | + const re = /\S+@\S+\.\S+/;  | 
 | 92 | + if (!re.test(email)) {  | 
 | 93 | + throw new Error(errorName.INVALID_EMAIL)  | 
 | 94 | + }  | 
 | 95 | + | 
 | 96 | + return email  | 
 | 97 | +}  | 
 | 98 | + | 
 | 99 | +// If the email is not valid the response will be  | 
 | 100 | +{  | 
 | 101 | + "errors": [  | 
 | 102 | + {  | 
 | 103 | + "message": "The email is not valid",  | 
 | 104 | + "statusCode": "400"  | 
 | 105 | + }  | 
 | 106 | + ],  | 
 | 107 | + "data": null  | 
 | 108 | +}  | 
 | 109 | +```  | 
 | 110 | + | 
 | 111 | +## Demo  | 
 | 112 | +Here is an [Example](https://github.com/EasyGraphQL/easygraphql-format-error/tree/master/example) that can be useful!  | 
 | 113 | + | 
 | 114 | +# License  | 
 | 115 | +### The MIT License  | 
 | 116 | + | 
 | 117 | +Copyright (c) 2018 EasyGraphQL  | 
 | 118 | + | 
 | 119 | +Permission is hereby granted, free of charge, to any person obtaining a copy  | 
 | 120 | +of this software and associated documentation files (the "Software"), to deal  | 
 | 121 | +in the Software without restriction, including without limitation the rights  | 
 | 122 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  | 
 | 123 | +copies of the Software, and to permit persons to whom the Software is  | 
 | 124 | +furnished to do so, subject to the following conditions:  | 
 | 125 | + | 
 | 126 | +The above copyright notice and this permission notice shall be included in  | 
 | 127 | +all copies or substantial portions of the Software.  | 
 | 128 | + | 
 | 129 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  | 
 | 130 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  | 
 | 131 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  | 
 | 132 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  | 
 | 133 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  | 
 | 134 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  | 
 | 135 | +THE SOFTWARE.  | 
0 commit comments