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

Custom error formatting function #45

Merged
merged 1 commit into from Feb 3, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -33,6 +33,11 @@ The `graphqlHTTP` function accepts the following options:

* **`pretty`**: If `true`, any JSON response will be pretty-printed.

* **`formatError`**: An optional function which will be used to format any
errors produced by fulfilling a GraphQL operation. If no function is
provided, GraphQL's default spec-compliant [`formatError`][] function will
be used. *To enable stack traces, provide the function: `error => error`.*

* **`graphiql`**: If `true`, may present [GraphiQL][] when loaded directly
from a browser (a useful tool for debugging and exploration).

Expand Down Expand Up @@ -124,6 +129,7 @@ new GraphQLObjectType({
```

[`graphql-js`]: https://github.com/graphql/graphql-js
[`formatError`]: https://github.com/graphql/graphql-js/blob/master/src/error/formatError.js
[GraphiQL]: https://github.com/graphql/graphiql
[`multer`]: https://github.com/expressjs/multer
[`express-session`]: https://github.com/expressjs/session
61 changes: 55 additions & 6 deletions src/__tests__/http-test.js
Expand Up @@ -797,8 +797,7 @@ describe('test harness', () => {
var app = express();

app.use(urlString(), graphqlHTTP({
schema: TestSchema,
pretty: true
schema: TestSchema
}));

var response = await request(app)
Expand All @@ -816,12 +815,65 @@ describe('test harness', () => {
});
});

it('allows for custom error formatting to sanitize', async () => {
var app = express();

app.use(urlString(), graphqlHTTP({
schema: TestSchema,
formatError(error) {
return { message: 'Custom error format: ' + error.message };
}
}));

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

expect(response.status).to.equal(200);
expect(JSON.parse(response.text)).to.deep.equal({
data: null,
errors: [ {
message: 'Custom error format: Throws!',
} ]
});
});

it('allows for custom error formatting to elaborate', async () => {
var app = express();

app.use(urlString(), graphqlHTTP({
schema: TestSchema,
formatError(error) {
return {
message: error.message,
locations: error.locations,
stack: 'Stack trace'
};
}
}));

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

expect(response.status).to.equal(200);
expect(JSON.parse(response.text)).to.deep.equal({
data: null,
errors: [ {
message: 'Throws!',
locations: [ { line: 1, column: 2 } ],
stack: 'Stack trace',
} ]
});
});

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

app.use(urlString(), graphqlHTTP({
schema: TestSchema,
pretty: true
}));

var error = await catchError(
Expand All @@ -846,7 +898,6 @@ describe('test harness', () => {

app.use(urlString(), graphqlHTTP({
schema: TestSchema,
pretty: true
}));

var error = await catchError(
Expand All @@ -864,7 +915,6 @@ describe('test harness', () => {

app.use(urlString(), graphqlHTTP({
schema: TestSchema,
pretty: true
}));

var error = await catchError(
Expand All @@ -885,7 +935,6 @@ describe('test harness', () => {

app.use(urlString(), graphqlHTTP({
schema: TestSchema,
pretty: true
}));

var error = await catchError(
Expand Down
13 changes: 11 additions & 2 deletions src/index.js
Expand Up @@ -46,7 +46,14 @@ export type OptionsObj = {
pretty?: ?boolean,

/**
* A boolean to optionally enable GraphiQL mode
* An optional function which will be used to format any errors produced by
* fulfilling a GraphQL operation. If no function is provided, GraphQL's
* default spec-compliant `formatError` function will be used.
*/
formatError?: ?Function,

/**
* A boolean to optionally enable GraphiQL mode.
*/
graphiql?: ?boolean,
};
Expand All @@ -69,6 +76,7 @@ export default function graphqlHTTP(options: Options): Middleware {
let rootValue;
let pretty;
let graphiql;
let formatErrorFn;
let showGraphiQL;
let query;
let variables;
Expand All @@ -84,6 +92,7 @@ export default function graphqlHTTP(options: Options): Middleware {
rootValue = optionsObj.rootValue;
pretty = optionsObj.pretty;
graphiql = optionsObj.graphiql;
formatErrorFn = optionsObj.formatError;

// GraphQL HTTP only supports GET and POST methods.
if (request.method !== 'GET' && request.method !== 'POST') {
Expand Down Expand Up @@ -177,7 +186,7 @@ export default function graphqlHTTP(options: Options): Middleware {
}).then(result => {
// Format any encountered errors.
if (result && result.errors) {
result.errors = result.errors.map(formatError);
result.errors = result.errors.map(formatErrorFn || formatError);
}

// If allowed to show GraphiQL, present it instead of JSON.
Expand Down