aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/migrator/index.js
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2018-04-06 04:15:08 +0800
committerkumavis <aaron@kumavis.me>2018-04-06 04:15:08 +0800
commit2b880dd4e060f8f7f95afe9ff2a3e2e6d540c922 (patch)
tree8d7875da5984f9df1a8258caba76c9ae03e40f02 /app/scripts/lib/migrator/index.js
parentbcb5f14b06959a9fd147361fa341e587b45baa76 (diff)
downloadtangerine-wallet-browser-2b880dd4e060f8f7f95afe9ff2a3e2e6d540c922.tar.gz
tangerine-wallet-browser-2b880dd4e060f8f7f95afe9ff2a3e2e6d540c922.tar.zst
tangerine-wallet-browser-2b880dd4e060f8f7f95afe9ff2a3e2e6d540c922.zip
migrations - report migrations errors to sentry with vault structure
Diffstat (limited to 'app/scripts/lib/migrator/index.js')
-rw-r--r--app/scripts/lib/migrator/index.js24
1 files changed, 20 insertions, 4 deletions
diff --git a/app/scripts/lib/migrator/index.js b/app/scripts/lib/migrator/index.js
index 4fd2cae92..203b2ddc9 100644
--- a/app/scripts/lib/migrator/index.js
+++ b/app/scripts/lib/migrator/index.js
@@ -1,6 +1,9 @@
-class Migrator {
+const EventEmitter = require('events')
+
+class Migrator extends EventEmitter {
constructor (opts = {}) {
+ super()
const migrations = opts.migrations || []
// sort migrations by version
this.migrations = migrations.sort((a, b) => a.version - b.version)
@@ -12,13 +15,26 @@ class Migrator {
// run all pending migrations on meta in place
async migrateData (versionedData = this.generateInitialState()) {
+ // get all migrations that have not yet been run
const pendingMigrations = this.migrations.filter(migrationIsPending)
+ // perform each migration
for (const index in pendingMigrations) {
const migration = pendingMigrations[index]
- versionedData = await migration.migrate(versionedData)
- if (!versionedData.data) throw new Error('Migrator - migration returned empty data')
- if (versionedData.version !== undefined && versionedData.meta.version !== migration.version) throw new Error('Migrator - Migration did not update version number correctly')
+ try {
+ // attempt migration and validate
+ const migratedData = await migration.migrate(versionedData)
+ if (!migratedData.data) throw new Error('Migrator - migration returned empty data')
+ if (migratedData.version !== undefined && migratedData.meta.version !== migration.version) throw new Error('Migrator - Migration did not update version number correctly')
+ // accept the migration as good
+ versionedData = migratedData
+ } catch (err) {
+ // emit error instead of throw so as to not break the run (gracefully fail)
+ const error = new Error(`MetaMask Migration Error #${version}:\n${err.stack}`)
+ this.emit('error', error)
+ // stop migrating and use state as is
+ return versionedData
+ }
}
return versionedData