Skip to content

Commit

Permalink
fix: extensions argument not being passed down by Forbidden and Autho…
Browse files Browse the repository at this point in the history
…rization errors (#5309)

Co-authored-by: Jon Wyatt <[email protected]>
  • Loading branch information
bristoljon and Jon Wyatt authored Jun 15, 2021
1 parent ea2fe59 commit 429c504
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
28 changes: 27 additions & 1 deletion packages/apollo-server-errors/src/__tests__/ApolloError.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApolloError } from '..';
import { ApolloError, ForbiddenError, AuthenticationError } from '..';

describe('ApolloError', () => {
it("doesn't overwrite extensions when provided in the constructor", () => {
Expand Down Expand Up @@ -48,3 +48,29 @@ describe('ApolloError', () => {
).toThrow(/Pass extensions directly/);
});
});

describe("ForbiddenError", () => {
it("supports abritrary data being passed", () => {
const error = new ForbiddenError('My message', {
arbitrary: 'user_data',
});

expect(error.extensions).toEqual({
code: 'FORBIDDEN',
arbitrary: 'user_data',
});
})
})

describe("AuthenticationError", () => {
it("supports abritrary data being passed", () => {
const error = new AuthenticationError('My message', {
arbitrary: 'user_data',
});

expect(error.extensions).toEqual({
code: 'UNAUTHENTICATED',
arbitrary: 'user_data',
});
})
})
8 changes: 4 additions & 4 deletions packages/apollo-server-errors/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,16 @@ export class ValidationError extends ApolloError {
}

export class AuthenticationError extends ApolloError {
constructor(message: string) {
super(message, 'UNAUTHENTICATED');
constructor(message: string, extensions?: Record<string, any>) {
super(message, 'UNAUTHENTICATED', extensions);

Object.defineProperty(this, 'name', { value: 'AuthenticationError' });
}
}

export class ForbiddenError extends ApolloError {
constructor(message: string) {
super(message, 'FORBIDDEN');
constructor(message: string, extensions?: Record<string, any>) {
super(message, 'FORBIDDEN', extensions);

Object.defineProperty(this, 'name', { value: 'ForbiddenError' });
}
Expand Down

0 comments on commit 429c504

Please sign in to comment.