Skip to content

Commit

Permalink
fix: close #27, use try/catch to prevent throwing error (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
BuptStEve authored Jun 19, 2019
1 parent cd616be commit 3c27bbe
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
50 changes: 25 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tua-storage",
"version": "1.7.2",
"version": "1.7.3",
"description": "🏗 A common storage for web(localStorage), for RN(AsyncStorage), for mini-program(wx) or just memory cache(Node.js)",
"main": "dist/TuaStorage.cjs.js",
"module": "dist/TuaStorage.esm.js",
Expand Down Expand Up @@ -71,41 +71,41 @@
},
"homepage": "https://tuateam.github.io/tua-storage/",
"devDependencies": {
"@babel/core": "^7.3.3",
"@babel/core": "^7.4.5",
"@babel/plugin-external-helpers": "^7.2.0",
"@babel/plugin-proposal-decorators": "^7.3.0",
"@babel/plugin-proposal-object-rest-spread": "^7.3.2",
"@babel/preset-env": "^7.3.1",
"@commitlint/cli": "^7.5.2",
"@commitlint/config-conventional": "^7.5.0",
"@types/jest": "^24.0.12",
"@babel/plugin-proposal-decorators": "^7.4.4",
"@babel/plugin-proposal-object-rest-spread": "^7.4.4",
"@babel/preset-env": "^7.4.5",
"@commitlint/cli": "^8.0.0",
"@commitlint/config-conventional": "^8.0.0",
"@types/jest": "^24.0.15",
"@vue/test-utils": "^1.0.0-beta.29",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.1.0",
"codecov": "^3.2.0",
"babel-eslint": "^10.0.2",
"babel-jest": "^24.8.0",
"codecov": "^3.5.0",
"cross-env": "^5.2.0",
"eslint": "^5.14.1",
"eslint": "^5.16.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-import": "^2.17.3",
"eslint-plugin-node": "^9.1.0",
"eslint-plugin-promise": "^4.1.1",
"eslint-plugin-standard": "^4.0.0",
"gh-pages": "^2.0.1",
"husky": "^1.3.1",
"jest": "^24.1.0",
"husky": "^2.4.1",
"jest": "^24.8.0",
"jest-localstorage-mock": "^2.4.0",
"lint-staged": "^8.1.4",
"mock-async-storage": "^2.0.4",
"lint-staged": "^8.2.1",
"mock-async-storage": "^2.1.0",
"rimraf": "^2.6.3",
"rollup": "^1.2.2",
"rollup": "^1.15.6",
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-eslint": "^5.0.0",
"rollup-plugin-json": "^3.1.0",
"rollup-plugin-replace": "^2.1.0",
"rollup-plugin-eslint": "^6.0.0",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-uglify": "^6.0.2",
"typescript": "^3.4.5",
"vuepress": "^1.0.0-alpha.39"
"typescript": "^3.5.2",
"vuepress": "^1.0.1"
},
"keywords": [
"tua-storage",
Expand Down
19 changes: 13 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,20 @@ class TuaStorage {
return defaultSEMap
}

const promiseTest = this.SE.setItem('test', 'test')
this.SE.removeItem('test')
const isPromise = !!(promiseTest && promiseTest.then)
try {
const testKey = `__TUA_STORAGE_TEST__`
const promiseTest = this.SE.setItem(testKey, 'test')
const isPromise = !!(promiseTest && promiseTest.then)
this.SE.removeItem(testKey)

return isPromise
? formatMethodsByAS.call(this)
: formatMethodsByLS.call(this)
} catch (error) {
logger.error(error)

return isPromise
? formatMethodsByAS.call(this)
: formatMethodsByLS.call(this)
return defaultSEMap
}
}

/**
Expand Down
4 changes: 1 addition & 3 deletions test/AsyncStorage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,7 @@ describe('error handling', () => {
invalidAsyncStorage.setItem = () => {
throw Error('invalid SE')
}
const fn = () => new TuaStorage({ storageEngine: invalidAsyncStorage })

expect(fn).toThrowError('invalid SE')
new TuaStorage({ storageEngine: invalidAsyncStorage })
})

test('throw error when invoke sync methods', () => {
Expand Down

0 comments on commit 3c27bbe

Please sign in to comment.