-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ApolloError bug and GraphQLError spec compliance (#3408)
* No longer allow the 'properties' object to overwrite the error message provided to the ApolloError constructor * Move ApolloError towards a GraphQLError spec compliance by using extensions rather than arbitrary properties on the class itself. Previous behavior is still supported, however it's now deprecated in favor of extensions. For more context, please read the large comment block associated with this commit.
- Loading branch information
1 parent
1de74ed
commit faba52c
Showing
6 changed files
with
88 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const config = require('../../jest.config.base'); | ||
|
||
module.exports = Object.assign(Object.create(null), config); |
49 changes: 49 additions & 0 deletions
49
packages/apollo-server-errors/src/__tests__/ApolloError.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { ApolloError } from '..'; | ||
|
||
describe('ApolloError', () => { | ||
it("doesn't overwrite extensions when provided in the constructor", () => { | ||
const error = new ApolloError('My message', 'A_CODE', { | ||
arbitrary: 'user_data', | ||
}); | ||
|
||
expect(error.extensions).toEqual({ | ||
code: 'A_CODE', | ||
arbitrary: 'user_data', | ||
}); | ||
}); | ||
|
||
it("a code property doesn't overwrite the code provided to the constructor", () => { | ||
const error = new ApolloError('My message', 'A_CODE', { | ||
code: 'CANT_OVERWRITE', | ||
}); | ||
|
||
expect(error.extensions).toEqual({ | ||
code: 'A_CODE', | ||
}); | ||
}); | ||
|
||
// This is a byproduct of how we currently assign properties from the 3rd constructor | ||
// argument onto properties of the class itself. This is expected, but deprecated behavior | ||
// and as such this test should be deleted in the future when we make that breaking change. | ||
it("a message property doesn't overwrite the message provided to the constructor", () => { | ||
const error = new ApolloError('My original message', 'A_CODE', { | ||
message: | ||
"This message can't overwrite the original message, but it does end up in extensions", | ||
}); | ||
|
||
expect(error.message).toEqual('My original message'); | ||
expect(error.extensions.message).toEqual( | ||
"This message can't overwrite the original message, but it does end up in extensions", | ||
); | ||
}); | ||
|
||
it('(back-compat) sets extensions correctly for users who use an extensions key in the third constructor argument', () => { | ||
const error = new ApolloError('My original message', 'A_CODE', { | ||
extensions: { | ||
arbitrary: 'user_data', | ||
}, | ||
}); | ||
|
||
expect(error.extensions.arbitrary).toEqual('user_data'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "../../../../tsconfig.test.base", | ||
"include": ["**/*"], | ||
"references": [ | ||
{ "path": "../../" } | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters