diff options
author | Frankie <frankie.diamond@gmail.com> | 2018-04-06 06:03:53 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-06 06:03:53 +0800 |
commit | 2dfa8a2e5049c21b30f6b70d06d0cfd56097904a (patch) | |
tree | ff7f5b0b7e3615193765929527875f1324371baa /app/scripts/lib | |
parent | 17931698a8bb2be77ea9671708135bba79ad2296 (diff) | |
parent | ffc71ff7d2c27d419bff4ca127ed5219bf9261c3 (diff) | |
download | tangerine-wallet-browser-2dfa8a2e5049c21b30f6b70d06d0cfd56097904a.tar tangerine-wallet-browser-2dfa8a2e5049c21b30f6b70d06d0cfd56097904a.tar.gz tangerine-wallet-browser-2dfa8a2e5049c21b30f6b70d06d0cfd56097904a.tar.bz2 tangerine-wallet-browser-2dfa8a2e5049c21b30f6b70d06d0cfd56097904a.tar.lz tangerine-wallet-browser-2dfa8a2e5049c21b30f6b70d06d0cfd56097904a.tar.xz tangerine-wallet-browser-2dfa8a2e5049c21b30f6b70d06d0cfd56097904a.tar.zst tangerine-wallet-browser-2dfa8a2e5049c21b30f6b70d06d0cfd56097904a.zip |
Merge pull request #3900 from MetaMask/migration-error-report
migrations - report migrations errors to sentry with vault structure
Diffstat (limited to 'app/scripts/lib')
-rw-r--r-- | app/scripts/lib/getObjStructure.js | 33 | ||||
-rw-r--r-- | app/scripts/lib/migrator/index.js | 27 |
2 files changed, 56 insertions, 4 deletions
diff --git a/app/scripts/lib/getObjStructure.js b/app/scripts/lib/getObjStructure.js new file mode 100644 index 000000000..3db389507 --- /dev/null +++ b/app/scripts/lib/getObjStructure.js @@ -0,0 +1,33 @@ +const clone = require('clone') + +module.exports = getObjStructure + +// This will create an object that represents the structure of the given object +// it replaces all values with the result of their type + +// { +// "data": { +// "CurrencyController": { +// "conversionDate": "number", +// "conversionRate": "number", +// "currentCurrency": "string" +// } +// } + +function getObjStructure(obj) { + const structure = clone(obj) + return deepMap(structure, (value) => { + return value === null ? 'null' : typeof value + }) +} + +function deepMap(target = {}, visit) { + Object.entries(target).forEach(([key, value]) => { + if (typeof value === 'object' && value !== null) { + target[key] = deepMap(value, visit) + } else { + target[key] = visit(value) + } + }) + return target +} diff --git a/app/scripts/lib/migrator/index.js b/app/scripts/lib/migrator/index.js index 4fd2cae92..85c2717ea 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,29 @@ 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) { + // rewrite error message to add context without clobbering stack + const originalErrorMessage = err.message + err.message = `MetaMask Migration Error #${migration.version}: ${originalErrorMessage}` + console.warn(err.stack) + // emit error instead of throw so as to not break the run (gracefully fail) + this.emit('error', err) + // stop migrating and use state as is + return versionedData + } } return versionedData |