Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Add support for errorsCallback so we got errors detail on server … #37

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"lib": "./dist"
},
"files": [
"src",
"dist",
"README.md",
"LICENSE",
Expand All @@ -40,6 +41,7 @@
"lint": "eslint src",
"check": "flow check",
"build": "rm -rf dist/* && babel src --ignore __tests__ --out-dir dist",
"postinstall": "rm -rf dist/* && babel src --ignore __tests__ --out-dir dist",
"watch": "babel --optional runtime resources/watch.js | node",
"cover": "babel-node node_modules/.bin/isparta cover --root src --report html node_modules/.bin/_mocha -- $npm_package_options_mocha",
"cover:lcov": "babel-node node_modules/.bin/isparta cover --root src --report lcovonly node_modules/.bin/_mocha -- $npm_package_options_mocha",
Expand All @@ -66,7 +68,7 @@
"express": "4.13.3",
"express3": "*",
"flow-bin": "0.18.1",
"graphql": "0.4.8",
"graphql": "0.4.14",
"isparta": "3.0.3",
"mocha": "2.2.5",
"multer": "1.0.3",
Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,26 @@ describe('test harness', () => {
});

describe('Error handling functionality', () => {
it('call errorsCallback on errors', async done => {
var app = express();

app.use(urlString(), graphqlHTTP({
schema: TestSchema,
pretty: true
}, errors => {
expect(errors).to.have.length(1);
let error = errors[0];
expect(error.message).to.equal('Throws!');
expect(error.originalError).to.be.an('object');
done();
}));

await request(app)
.get(urlString({
query: '{thrower}',
}));
});

it('handles field errors caught by GraphQL', async () => {
var app = express();

Expand Down
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Middleware = (request: Request, response: Response) => void;
* Middleware for express; takes an options object or function as input to
* configure behavior, and returns an express middleware.
*/
export default function graphqlHTTP(options: Options): Middleware {
export default function graphqlHTTP(options: Options, errorsCallback: ?Function): Middleware {
if (!options) {
throw new Error('GraphQL middleware requires options.');
}
Expand Down Expand Up @@ -169,8 +169,14 @@ export default function graphqlHTTP(options: Options): Middleware {
response.status(error.status || 500);
return { errors: [ error ] };
}).then(result => {

// Format any encountered errors.
if (result && result.errors) {
// Call errorsCallback if errors
if (errorsCallback ) {
errorsCallback(result.errors);
}

result.errors = result.errors.map(formatError);
}

Expand Down