Skip to content

Commit

Permalink
Add errorHandling option for custom error handling (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan authored Jul 10, 2019
1 parent 3a71e7e commit 6d759f2
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
19 changes: 19 additions & 0 deletions docs/api/error-handler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: Error Handling
---

By default, Sofa returns a response that includes JSON representation of thrown error object from GraphQL with HTTP status code 500. But, you can enhance error handler by adding your `errorHandler` function.

```typescript
api.use(
'/api',
sofa({
schema,
errorHandler(res, error) {
logError(error);
res.code(500);
res.json(formatError(error));
},
})
);
```
3 changes: 3 additions & 0 deletions example/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export const resolvers: any = {
feed() {
return PostsCollection.all();
},
never() {
throw new Error('Some Message');
},
},
Mutation: {
addBook(_: any, { title }: any) {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
"devDependencies": {
"@types/body-parser": "1.17.0",
"@types/express": "4.16.0",
"@types/express-graphql": "0.6.2",
"@types/graphql": "14.0.3",
"@types/express-graphql": "0.8.0",
"@types/graphql": "14.2.2",
"@types/jest": "23.3.10",
"@types/request-promise-native": "1.0.15",
"@types/supertest": "2.0.7",
Expand All @@ -72,7 +72,7 @@
"swagger-ui-express": "4.0.2",
"ts-jest": "23.10.5",
"ts-loader": "5.3.1",
"ts-node": "7.0.1",
"ts-node": "8.3.0",
"typescript": "3.2.2",
"webpack": "4.27.1",
"webpack-cli": "3.1.2"
Expand Down
9 changes: 8 additions & 1 deletion src/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,14 @@ function useHandler(config: {

// TODO: add error handling for result.errors
if (result.errors) {
throw new Error(result.errors.toString());
const defaultErrorHandler: ErrorHandler = (res, error) => {
res.status(500);
res.json(error);
};
const errorHandler: ErrorHandler =
sofa.errorHandler || defaultErrorHandler;
errorHandler(res, result.errors[0]);

This comment has been minimized.

Copy link
@kamilkisiela

kamilkisiela Jul 15, 2019

Collaborator

@ardatan we should pass an array of errors and let user decide what to do with it. The default should return a first error.

return;
}

res.json(result.data && result.data[fieldName]);
Expand Down
3 changes: 3 additions & 0 deletions src/sofa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { Ignore, Context, ContextFn, ExecuteFn, OnRoute } from './types';
import { convertName } from './common';
import { logger } from './logger';
import { ErrorHandler } from './express';

// user passes:
// - schema
Expand All @@ -27,6 +28,7 @@ export interface SofaConfig {
ignore?: Ignore; // treat an Object with an ID as not a model - accepts ['User', 'Message.author']
onRoute?: OnRoute;
depthLimit?: number;
errorHandler?: ErrorHandler;
}

export interface Sofa {
Expand All @@ -36,6 +38,7 @@ export interface Sofa {
ignore: Ignore;
execute: ExecuteFn;
onRoute?: OnRoute;
errorHandler?: ErrorHandler;
}

export function createSofa(config: SofaConfig): Sofa {
Expand Down
8 changes: 7 additions & 1 deletion website/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
"essentials/subscriptions"
],
"Recipes": ["recipes/open-api"],
"API": ["api/models", "api/context", "api/ignore", "api/execute"]
"API": [
"api/models",
"api/context",
"api/ignore",
"api/execute",
"api/error-handler"
]
}
}

0 comments on commit 6d759f2

Please sign in to comment.