-
Notifications
You must be signed in to change notification settings - Fork 5.5k
fix(migrator): prevent crash by throwing AggregateError instead of mutating existing error
#39582
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
Merged
+84
−7
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7eb3141
switch to aggregate error and add tests
davidmurdoch 1787670
Merge branch 'main' into fix-migration-error-catch
davidmurdoch 1538e1d
Update app/scripts/lib/migrator/index.test.js
davidmurdoch 93e7590
Update app/scripts/lib/migrator/index.test.js
davidmurdoch a174614
stupid lint rules are stupid
davidmurdoch 14c66fd
Merge branch 'fix-migration-error-catch' of https://github.com/MetaMa…
davidmurdoch ffebfe3
asdfasdf
davidmurdoch 77d5acd
Merge branch 'main' into fix-migration-error-catch
davidmurdoch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -114,9 +114,81 @@ describe('migrations', () => { | |
| }, | ||
| ], | ||
| }); | ||
| await expect(async () => { | ||
| await migrator.migrateData({ meta: { version: 0 } }); | ||
| }).rejects.toThrow('Error: MetaMask Migration Error #1: test'); | ||
| const onError = jest.fn(); | ||
| migrator.on('error', onError); | ||
|
|
||
| const initialState = { meta: { version: 0 }, data: { hello: 'world' } }; | ||
| const migratedData = await migrator.migrateData(initialState); | ||
|
|
||
| expect(onError).toHaveBeenCalledTimes(1); | ||
| const [error] = onError.mock.calls[0]; | ||
| expect(error).toBeInstanceOf(AggregateError); | ||
| expect(error.message).toBe('MetaMask Migration Error #1'); | ||
| expect(error.errors[0].message).toBe('test'); | ||
| expect(migratedData.state).toBe(initialState); | ||
| }); | ||
|
|
||
| it('runs v2 migrations and reports changed controllers', async () => { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. specifically testing the v2 migration flow here |
||
| const migrate = jest.fn(async (state, localChangedControllers) => { | ||
| state.meta.version = 187; | ||
| state.data.foo = 'bar'; | ||
| localChangedControllers.add('TestController'); | ||
| }); | ||
|
|
||
| const migrator = new Migrator({ | ||
| migrations: [ | ||
| { | ||
| version: 187, | ||
| migrate, | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| const initialState = { meta: { version: 186 }, data: { hello: 'world' } }; | ||
| const migratedData = await migrator.migrateData(initialState); | ||
|
|
||
| expect(migrate).toHaveBeenCalledTimes(1); | ||
| expect(migrate.mock.calls[0]).toHaveLength(2); | ||
| expect(migratedData.state).not.toBe(initialState); | ||
| expect(migratedData.state.data).toEqual({ | ||
| hello: 'world', | ||
| foo: 'bar', | ||
| }); | ||
| expect(migratedData.changedKeys.has('TestController')).toBe(true); | ||
| }); | ||
|
|
||
| it('handles errors thrown when state is cloned for next migration', async () => { | ||
| const migrate = jest.fn(); | ||
| const migrator = new Migrator({ | ||
| migrations: [ | ||
| { | ||
| version: 186, | ||
| migrate, | ||
| }, | ||
| ], | ||
| }); | ||
| const onError = jest.fn(); | ||
| migrator.on('error', onError); | ||
|
|
||
| const initialState = { | ||
| meta: { version: 0 }, | ||
| // Regression test for https://github.com/MetaMask/metamask-extension/issues/39567 | ||
| // `bad` is a function, and cannot be serialized by `structuredClone` | ||
| // this will throw a DOMException, which doesn't allow its `message` | ||
| // property to be mutated | ||
| data: { bad: () => {} }, | ||
| }; | ||
| const migratedData = await migrator.migrateData(initialState); | ||
|
|
||
| expect(migrate).not.toHaveBeenCalled(); | ||
| expect(onError).toHaveBeenCalledTimes(1); | ||
| const [error] = onError.mock.calls[0]; | ||
| expect(error).toBeInstanceOf(AggregateError); | ||
| expect(error.message).toBe('MetaMask Migration Error #186'); | ||
| expect(error.errors[0]?.constructor?.name).toBe('DOMException'); | ||
| expect(error.errors[0].name).toBe('DataCloneError'); | ||
| expect(error.errors[0].message).toBe('() => {} could not be cloned.'); | ||
davidmurdoch marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| expect(migratedData.state).toBe(initialState); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The shape of the error is different now.
This DOES effect migration error display sentry.