Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
feat: simplfied error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zachferland committed May 25, 2020
1 parent 12d9e9d commit 6cb9064
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 34 deletions.
2 changes: 1 addition & 1 deletion iframe/html/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const template = (data,isMobile) => `
</p>
</div>
</div>
<div class='${style.actions}'>
<div class='${style.actions}' id='action'>
<button id='accept' class='${style.primaryButton}' ${data.error ? 'style="display:none;"' : ''} >
Continue
</button>
Expand Down
23 changes: 11 additions & 12 deletions iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@ import ThreeIdConnectService from './../src/threeIdConnectService.js'
const store = require('store')
const assets = require('./assets/assets.js')

store.remove('error')

/**
* UI Window Functions
*/

const mobileRegex = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i
const checkIsMobile = () => mobileRegex.test(navigator.userAgent)

const error = (error) => `
<p class='walletSelect_error'>${error}</p>
`

// Given a request will render UI module templates
const render = async (request) => {
const errorMessage = store.get('error')
let data = {
request
}
if (errorMessage) data.error = errorMessage
root.innerHTML = template(data, checkIsMobile())
document.getElementById('root').innerHTML = template({request}, checkIsMobile())
}

/**
Expand All @@ -32,11 +29,12 @@ const idwService = new ThreeIdConnectService()
const getConsent = async (req) => {
await idwService.displayIframe()
await render(req)
const accept = document.getElementById('accept')

const result = await new Promise((resolve, reject) => {
accept.addEventListener('click', () => {
accept.innerHTML = `Confirm in your wallet ${assets.Loading}`;
document.getElementById("accept").style.boxShadow = 'none';
accept.style.boxShadow = 'none';
resolve(true)
})
})
Expand All @@ -45,19 +43,20 @@ const getConsent = async (req) => {
}

// Service calls on error, renders error to UI
const errorCb = (err, msg) => {
const errorCb = (err, msg, req) => {
if (!msg) msg = err.toString()
msg = 'Error: Unable to connect'
console.log(err)
store.set('error', msg)
document.getElementById('action').innerHTML = error(msg)
}

// Closure to pass cancel state to IDW iframe service
let closecallback

window.hideIframe = () => {
idwService.hideIframe()
root.innerHTML = ``
const root = document.getElementById('root')
if (root) root.innerHTML = ``
if (closecallback) closecallback()
}

Expand Down
30 changes: 9 additions & 21 deletions src/threeIdConnectService.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ class ThreeIdConnectService {
* Tells parent window to hide iframe
*/
async hideIframe() {
store.remove('error') //TODO move, so specific to iframe implementation
root.innerHTML = ``
const root = document.getElementById('root')
if (root) root.innerHTML = ``
return this.hide()
}

Expand Down Expand Up @@ -184,7 +184,6 @@ class ThreeIdConnectService {
*/
async providerRelay(message) {
const domain = new Url(document.referrer).host
let loop = true

const responsePromise = new Promise(async (resolve, reject) => {
// Register request cancel calback
Expand All @@ -196,27 +195,16 @@ class ThreeIdConnectService {
error: "3id-connect: Request not authorized"
}
resolve(res)
loop = false
})

// Should not be neccessary, but sets bounds on any uncaught errors, so
// we dont have infinite loop and freeze
let tries = 0

if (message.method === '3id_authenticate') {
// Try until response valid, or canceled above
while (loop) {
tries++
try {
const res = await this.provider.send(message, domain)
if (message.method === `3id_authenticate`) this.hideIframe()
resolve(res)
loop = false
} catch (e) {
this.errorCb(e, 'There was an error. Use the same account you used for this app.')
this._removeConsents(message, domain)
}
if (tries >= 10) loop = false
try {
const res = await this.provider.send(message, domain)
this.hideIframe()
resolve(res)
} catch (e) {
this.errorCb(e, 'Error: Unable to connect')
this._removeConsents(message, domain)
}
} else {
const res = await this.provider.send(message, domain)
Expand Down

0 comments on commit 6cb9064

Please sign in to comment.