Skip to content

Commit

Permalink
fix: support liquality errors
Browse files Browse the repository at this point in the history
- get liquality error message by using translation
  • Loading branch information
koderholic committed Nov 2, 2022
1 parent 0a58484 commit 0317669
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"dependencies": {
"@liquality/cryptoassets": "1.16.2",
"@liquality/error-parser": "^1.1.3",
"@liquality/ethereum-utils": "^1.13.12",
"@liquality/terra-networks": "^1.13.12",
"@liquality/types": "^1.13.12",
Expand Down
11 changes: 9 additions & 2 deletions src/broker/Background.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { buildConfig } from '@liquality/wallet-core'
import { BG_PREFIX, handleConnection, removeConnectId, getRootURL } from './utils'
import { getChain } from '@liquality/cryptoassets'
import { LiqualityError } from '@liquality/error-parser'
import { connectRemote } from './terra-injection'
import { translateLiqualityError } from '../utils/liqualityErrors'

function attemptOrWarn(func, message) {
try {
Expand Down Expand Up @@ -155,7 +157,10 @@ class Background {
.then((result) => ({ result }))
.catch((error) => {
console.error(error)
return { error: error.message }
return {
error:
error instanceof LiqualityError ? translateLiqualityError(error) : error.message
}
})
.then((response) =>
attemptOrWarn(
Expand Down Expand Up @@ -279,7 +284,9 @@ class Background {
.then((result) => ({ result }))
.catch((error) => {
console.error(error) /* eslint-disable-line */
return { error: error.toString() }
return {
error: error instanceof LiqualityError ? translateLiqualityError(error) : error.toString()
}
})
.then((response) => {
connection.postMessage({
Expand Down
5 changes: 4 additions & 1 deletion src/store/modules/app/replyPermission.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { translateLiqualityError } from '../../../utils/liqualityErrors'
import { LiqualityError } from '@liquality/error-parser'
import { emitter } from '../../utils'

export const replyPermission = async ({ dispatch }, { request, allowed }) => {
Expand All @@ -6,7 +8,8 @@ export const replyPermission = async ({ dispatch }, { request, allowed }) => {
try {
response.result = await dispatch('executeRequest', { request })
} catch (error) {
response.error = error.message
response.error =
error instanceof LiqualityError ? translateLiqualityError(error) : error.message
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/utils/liqualityErrors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { i18n } from './localization'

export function translateLiqualityError(error) {
const cause = i18n.translate(error.causeKey, error.data)
const suggestions = i18n.translate(error.suggestionKey, error.data)
return `${cause} ${suggestions}`
}
2 changes: 1 addition & 1 deletion src/utils/localization.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { I18n } from 'i18n-js'

const i18n = new I18n()
export const i18n = new I18n()

export const loadLocale = async (locale, namespaces = ['common', 'components', 'pages']) => {
if (!i18n.translations[locale]) {
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4369,6 +4369,17 @@ __metadata:
languageName: node
linkType: hard

"@liquality/error-parser@npm:^1.1.3":
version: 1.1.3
resolution: "@liquality/error-parser@npm:1.1.3"
dependencies:
"@liquality/cryptoassets": 1.16.2
"@liquality/wallet-core": 1.31.3
randombytes: ^2.1.0
checksum: ea781441bd48e1452ace4505c0405ace9243010d3c0ac94f1caf721d47de28bb08e6ac72efb1d12c64645548c8e5ee8593a330c6cd918ac92b54df7c8b8bb1a2
languageName: node
linkType: hard

"@liquality/errors@npm:^1.13.12":
version: 1.13.12
resolution: "@liquality/errors@npm:1.13.12"
Expand Down Expand Up @@ -18845,6 +18856,7 @@ __metadata:
resolution: "liquality-wallet@workspace:."
dependencies:
"@liquality/cryptoassets": 1.16.2
"@liquality/error-parser": ^1.1.3
"@liquality/ethereum-utils": ^1.13.12
"@liquality/terra-networks": ^1.13.12
"@liquality/types": ^1.13.12
Expand Down

0 comments on commit 0317669

Please sign in to comment.