mirror of
https://github.com/vale981/apollo-server
synced 2025-03-06 10:11:40 -05:00
Removes flattening of Errors inside of formatApolloErrors (#1288)
* remove unnesting of errors to support yup * add test for error thrown by yup
This commit is contained in:
parent
dae5349d57
commit
19478920e4
3 changed files with 79 additions and 24 deletions
|
@ -222,8 +222,6 @@ export function formatApolloErrors(
|
||||||
}
|
}
|
||||||
const { formatter, debug } = options;
|
const { formatter, debug } = options;
|
||||||
|
|
||||||
const flattenedErrors: Error[] = [];
|
|
||||||
errors.forEach(error => {
|
|
||||||
// Errors that occur in graphql-tools can contain an errors array that contains the errors thrown in a merged schema
|
// Errors that occur in graphql-tools can contain an errors array that contains the errors thrown in a merged schema
|
||||||
// https://github.com/apollographql/graphql-tools/blob/3d53986ca/src/stitching/errors.ts#L104-L107
|
// https://github.com/apollographql/graphql-tools/blob/3d53986ca/src/stitching/errors.ts#L104-L107
|
||||||
//
|
//
|
||||||
|
@ -231,21 +229,20 @@ export function formatApolloErrors(
|
||||||
// https://github.com/apollographql/graphql-tools/blob/3d53986ca/src/stitching/errors.ts#L109-L113
|
// https://github.com/apollographql/graphql-tools/blob/3d53986ca/src/stitching/errors.ts#L109-L113
|
||||||
// which calls:
|
// which calls:
|
||||||
// https://github.com/graphql/graphql-js/blob/0a30b62964/src/error/locatedError.js#L18-L37
|
// https://github.com/graphql/graphql-js/blob/0a30b62964/src/error/locatedError.js#L18-L37
|
||||||
if (Array.isArray((error as any).errors)) {
|
// Some processing for these nested errors could be done here:
|
||||||
(error as any).errors.forEach(e => flattenedErrors.push(e));
|
//
|
||||||
} else if (
|
// if (Array.isArray((error as any).errors)) {
|
||||||
(error as any).originalError &&
|
// (error as any).errors.forEach(e => flattenedErrors.push(e));
|
||||||
Array.isArray((error as any).originalError.errors)
|
// } else if (
|
||||||
) {
|
// (error as any).originalError &&
|
||||||
(error as any).originalError.errors.forEach(e => flattenedErrors.push(e));
|
// Array.isArray((error as any).originalError.errors)
|
||||||
} else {
|
// ) {
|
||||||
flattenedErrors.push(error);
|
// (error as any).originalError.errors.forEach(e => flattenedErrors.push(e));
|
||||||
}
|
// } else {
|
||||||
});
|
// flattenedErrors.push(error);
|
||||||
|
// }
|
||||||
|
|
||||||
const enrichedErrors = flattenedErrors.map(error =>
|
const enrichedErrors = errors.map(error => enrichError(error, debug));
|
||||||
enrichError(error, debug),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!formatter) {
|
if (!formatter) {
|
||||||
return enrichedErrors;
|
return enrichedErrors;
|
||||||
|
|
|
@ -39,7 +39,8 @@
|
||||||
"graphql-tag": "^2.9.2",
|
"graphql-tag": "^2.9.2",
|
||||||
"js-sha256": "^0.9.0",
|
"js-sha256": "^0.9.0",
|
||||||
"subscriptions-transport-ws": "^0.9.11",
|
"subscriptions-transport-ws": "^0.9.11",
|
||||||
"ws": "^5.2.0"
|
"ws": "^5.2.0",
|
||||||
|
"yup": "^0.25.1"
|
||||||
},
|
},
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist/index.d.ts",
|
||||||
"typescript": {
|
"typescript": {
|
||||||
|
|
|
@ -7,6 +7,7 @@ import 'mocha';
|
||||||
import { sha256 } from 'js-sha256';
|
import { sha256 } from 'js-sha256';
|
||||||
import express = require('express');
|
import express = require('express');
|
||||||
import bodyParser = require('body-parser');
|
import bodyParser = require('body-parser');
|
||||||
|
import yup = require('yup');
|
||||||
|
|
||||||
import { Trace } from 'apollo-engine-reporting-protobuf';
|
import { Trace } from 'apollo-engine-reporting-protobuf';
|
||||||
|
|
||||||
|
@ -33,6 +34,7 @@ import {
|
||||||
import { createApolloFetch } from 'apollo-fetch';
|
import { createApolloFetch } from 'apollo-fetch';
|
||||||
import {
|
import {
|
||||||
AuthenticationError,
|
AuthenticationError,
|
||||||
|
UserInputError,
|
||||||
gql,
|
gql,
|
||||||
Config,
|
Config,
|
||||||
ApolloServerBase,
|
ApolloServerBase,
|
||||||
|
@ -345,6 +347,61 @@ export function testApolloServer<AS extends ApolloServerBase>(
|
||||||
expect(formatError.calledTwice).true;
|
expect(formatError.calledTwice).true;
|
||||||
expect(throwError.calledTwice).true;
|
expect(throwError.calledTwice).true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('works with errors similar to GraphQL errors, such as yup', async () => {
|
||||||
|
const throwError = stub().callsFake(async () => {
|
||||||
|
const schema = yup.object().shape({
|
||||||
|
email: yup
|
||||||
|
.string()
|
||||||
|
.email()
|
||||||
|
.required('Please enter your email address'),
|
||||||
|
});
|
||||||
|
|
||||||
|
await schema.validate({ email: 'lol' });
|
||||||
|
});
|
||||||
|
|
||||||
|
const formatError = stub().callsFake(error => {
|
||||||
|
expect(error instanceof Error).true;
|
||||||
|
expect(error.extensions.code).to.equal('INTERNAL_SERVER_ERROR');
|
||||||
|
expect(error.extensions.exception.name).to.equal('ValidationError');
|
||||||
|
expect(error.extensions.exception.message).to.exist;
|
||||||
|
const inputError = new UserInputError('User Input Error');
|
||||||
|
return {
|
||||||
|
message: inputError.message,
|
||||||
|
extensions: inputError.extensions,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
const { url: uri } = await createApolloServer({
|
||||||
|
typeDefs: gql`
|
||||||
|
type Query {
|
||||||
|
error: String
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
resolvers: {
|
||||||
|
Query: {
|
||||||
|
error: () => {
|
||||||
|
return throwError();
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
introspection: true,
|
||||||
|
debug: true,
|
||||||
|
formatError,
|
||||||
|
});
|
||||||
|
|
||||||
|
const apolloFetch = createApolloFetch({ uri });
|
||||||
|
|
||||||
|
const result = await apolloFetch({
|
||||||
|
query: '{error}',
|
||||||
|
});
|
||||||
|
expect(result.data).to.deep.equal({ error: null });
|
||||||
|
expect(result.errors, 'errors should exist').to.exist;
|
||||||
|
expect(result.errors[0].extensions.code).equals('BAD_USER_INPUT');
|
||||||
|
expect(result.errors[0].message).equals('User Input Error');
|
||||||
|
expect(formatError.calledOnce).true;
|
||||||
|
expect(throwError.calledOnce).true;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('lifecycle', () => {
|
describe('lifecycle', () => {
|
||||||
|
|
Loading…
Add table
Reference in a new issue