Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add errorHandling option for custom error handling #44

Merged
merged 1 commit into from
Jul 10, 2019
Merged
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
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]);
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"
]
}
}