Skip to content

Commit

Permalink
feat: create and use IpfsApiAccessError
Browse files Browse the repository at this point in the history
  • Loading branch information
JonKrone committed Apr 25, 2018
1 parent cf67417 commit 237cffb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
23 changes: 23 additions & 0 deletions add-on/src/lib/ipfs-proxy/api-access-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'
/* eslint-env browser */


/**
* An Error that indicates API access was somehow denied by the user
* @type {IpfsApiAccessError}
* @example
* try {
* await ipfs.files.add(Buffer.from('seed'))
* } catch (err) {
* if (err instanceof window.ipfs.types.IpfsApiAccessError) {
* // API access was denied by the user
* }
* }
*/
export default class IpfsApiAccessError extends Error {
constructor (message, permission, scope) {
super(message)
this.permission = permission
this.scope = scope
}
}
6 changes: 5 additions & 1 deletion add-on/src/lib/ipfs-proxy/pre-acl.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const IpfsApiAccessError = require('./api-access-error')

// This are the functions that DO NOT require an allow/deny decision by the user.
// All other IPFS functions require authorization.
const ACL_WHITELIST = Object.freeze(require('./acl-whitelist.json'))
Expand All @@ -21,7 +23,9 @@ function createPreAcl (permission, getState, getScope, accessControl, requestAcc
access = await accessControl.setAccess(scope, wildcard ? '*' : permission, allow)
}

if (!access.allow) throw new Error(`User denied access to ${permission}`)
if (!access.allow) {
throw new IpfsApiAccessError(`User denied access to ${permission}`, permission, scope)
}

return args
}
Expand Down
7 changes: 6 additions & 1 deletion add-on/src/lib/ipfs-proxy/request-access.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const { piggyback } = require('piggybacker')
const IpfsApiAccessError = require('./api-access-error')

const DIALOG_WIDTH = 540
const DIALOG_HEIGHT = 220
Expand Down Expand Up @@ -98,7 +99,11 @@ function createRequestAccess (browser, screen) {
const userTabRemoved = new Promise((resolve, reject) => {
onTabRemoved = (id) => {
if (id !== tabId) return
reject(new Error(`Failed to obtain access response for ${permission} at ${scope}`))
reject(new IpfsApiAccessError(
`Failed to obtain access response for ${permission} at ${scope}`,
permission,
scope
))
}
browser.tabs.onRemoved.addListener(onTabRemoved)
})
Expand Down

0 comments on commit 237cffb

Please sign in to comment.