1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 01:39:44 +01:00

Properly handle JSON errors when restoring data in advanced settings (#15815)

This commit is contained in:
Dan J Miller 2022-09-13 22:29:04 -02:30 committed by GitHub
parent d3c7b9fb32
commit 0b9424acc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 9 deletions

View File

@ -903,6 +903,9 @@
"dataBackupFoundInfo": {
"message": "Some of your account data was backed up during a previous installation of MetaMask. This could include your settings, contacts, and tokens. Would you like to restore this data now?"
},
"dataBackupSeemsCorrupt": {
"message": "Can not restore your data. The file appears to be corrupt."
},
"dataHex": {
"message": "Hex"
},

View File

@ -25,6 +25,8 @@ import { exportAsFile } from '../../../../shared/modules/export-utils';
import ActionableMessage from '../../../components/ui/actionable-message';
import ZENDESK_URLS from '../../../helpers/constants/zendesk-url';
const CORRUPT_JSON_FILE = 'CORRUPT_JSON_FILE';
export default class AdvancedTab extends PureComponent {
static contextTypes = {
t: PropTypes.func,
@ -72,6 +74,7 @@ export default class AdvancedTab extends PureComponent {
showLedgerTransportWarning: false,
showResultMessage: false,
restoreSuccessful: true,
restoreMessage: null,
};
settingsRefs = Array(
@ -155,16 +158,35 @@ export default class AdvancedTab extends PureComponent {
*
*/
event.target.value = '';
try {
const result = await this.props.restoreUserData(jsonString);
this.setState({
showResultMessage: true,
restoreSuccessful: result,
restoreMessage: null,
});
} catch (e) {
if (e.message.match(/Unexpected.+JSON/u)) {
this.setState({
showResultMessage: true,
restoreSuccessful: false,
restoreMessage: CORRUPT_JSON_FILE,
});
}
}
}
renderRestoreUserData() {
const { t } = this.context;
const { showResultMessage, restoreSuccessful } = this.state;
const { showResultMessage, restoreSuccessful, restoreMessage } = this.state;
const defaultRestoreMessage = restoreSuccessful
? t('restoreSuccessful')
: t('restoreFailed');
const restoreMessageToRender =
restoreMessage === CORRUPT_JSON_FILE
? t('dataBackupSeemsCorrupt')
: defaultRestoreMessage;
return (
<div
@ -197,9 +219,17 @@ export default class AdvancedTab extends PureComponent {
{showResultMessage && (
<ActionableMessage
type={restoreSuccessful ? 'success' : 'danger'}
message={
restoreSuccessful ? t('restoreSuccessful') : t('restoreFailed')
}
message={restoreMessageToRender}
primaryActionV2={{
label: t('dismiss'),
onClick: () => {
this.setState({
showResultMessage: false,
restoreSuccessful: true,
restoreMessage: null,
});
},
}}
/>
)}
</div>