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

Ensures formatError gets a value that passes instanceof Error #1235

Merged
merged 4 commits into from
Jun 25, 2018
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
3 changes: 1 addition & 2 deletions packages/apollo-server-errors/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ export class ApolloError extends Error implements GraphQLError {
}

function enrichError(error: Partial<GraphQLError>, debug: boolean = false) {
const expanded = {} as any;
// follows similar structure to https://github.com/graphql/graphql-js/blob/master/src/error/GraphQLError.js#L145-L193
// with the addition of name
Object.defineProperties(expanded, {
const expanded = Object.create(Object.getPrototypeOf(error), {
name: {
value: error.name,
},
Expand Down
42 changes: 42 additions & 0 deletions packages/apollo-server-integration-testsuite/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,16 @@ export function testApolloServer<AS extends ApolloServerBase>(
},
});

const formatError = stub().callsFake(error => {
expect(error instanceof Error).true;
return error;
});

const { url: uri } = await createApolloServer({
schema,
validationRules: [NoTestString],
introspection: false,
formatError,
});

const apolloFetch = createApolloFetch({ uri });
Expand All @@ -151,6 +157,7 @@ export function testApolloServer<AS extends ApolloServerBase>(
const result = await apolloFetch({ query: TEST_STRING_QUERY });
expect(result.data, 'data should not exist').not.to.exist;
expect(result.errors, 'errors should exist').to.exist;
expect(formatError.called).true;
});

it('allows introspection by default', async () => {
Expand Down Expand Up @@ -289,6 +296,41 @@ export function testApolloServer<AS extends ApolloServerBase>(
});
});

describe('formatError', () => {
it('wraps thrown error from validation rules', async () => {
const throwError = stub().callsFake(() => {
throw new Error('nope');
});

const formatError = stub().callsFake(error => {
expect(error instanceof Error).true;
expect(error.constructor.name).to.equal('Error');
return error;
});

const { url: uri } = await createApolloServer({
schema,
validationRules: [throwError],
introspection: true,
formatError,
});

const apolloFetch = createApolloFetch({ uri });

const introspectionResult = await apolloFetch({
query: INTROSPECTION_QUERY,
});
expect(introspectionResult.data, 'data should not exist').not.to.exist;
expect(introspectionResult.errors, 'errors should exist').to.exist;

const result = await apolloFetch({ query: TEST_STRING_QUERY });
expect(result.data, 'data should not exist').not.to.exist;
expect(result.errors, 'errors should exist').to.exist;
expect(formatError.called).true;
expect(throwError.called).true;
});
});

describe('lifecycle', () => {
it('defers context eval with thunk until after options creation', async () => {
const uniqueContext = { key: 'major' };
Expand Down
28 changes: 27 additions & 1 deletion packages/apollo-server-integration-testsuite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,33 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => {
app = await createApp({
graphqlOptions: {
schema,
formatError: () => ({ message: expected }),
formatError: error => {
expect(error instanceof Error).true;
return { message: expected };
},
},
});
const req = request(app)
.post('/graphql')
.send({
query: 'query test{ testError }',
});
return req.then(res => {
expect(res.status).to.equal(200);
expect(res.body.errors[0].message).to.equal(expected);
});
});

it('formatError receives error that passes instanceof checks', async () => {
const expected = '--blank--';
app = await createApp({
graphqlOptions: {
schema,
formatError: error => {
expect(error instanceof Error).true;
expect(error instanceof GraphQLError).true;
return { message: expected };
},
},
});
const req = request(app)
Expand Down