Skip to content

Commit

Permalink
refactor: standard error when window.ipfs is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Jan 15, 2019
1 parent d179f32 commit ac150f2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
8 changes: 5 additions & 3 deletions add-on/src/lib/ipfs-proxy/pre-acl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
function createPreAcl (permission, getState, getScope, accessControl, requestAccess) {
return async (...args) => {
// Check if all access to the IPFS node is disabled
if (!getState().ipfsProxy) throw new Error('User disabled access to API proxy in IPFS Companion')
if (!getState().ipfsProxy) {
throw createProxyAclError(undefined, undefined, 'User disabled access to API proxy in IPFS Companion')
}

const scope = await getScope()
const access = await getAccessWithPrompt(accessControl, requestAccess, scope, permission)
Expand All @@ -28,8 +30,8 @@ async function getAccessWithPrompt (accessControl, requestAccess, scope, permiss

// Standardized error thrown when a command access is denied
// TODO: return errors following conventions from https://github.com/ipfs/js-ipfs/pull/1746
function createProxyAclError (scope, permission) {
const err = new Error(`User denied access to selected commands over IPFS proxy: ${permission}`)
function createProxyAclError (scope, permission, message) {
const err = new Error(message || `User denied access to selected commands over IPFS proxy: ${permission}`)
const permissions = Array.isArray(permission) ? permission : [permission]
err.output = {
payload: {
Expand Down
2 changes: 2 additions & 0 deletions test/functional/lib/ipfs-proxy/enable-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ describe('lib/ipfs-proxy/enable-command', () => {
}

expect(() => { if (error) throw error }).to.throw('User disabled access to API proxy in IPFS Companion')
expect(error.scope).to.equal(undefined)
expect(error.permissions).to.be.equal(undefined)
})

it('should throw if access to unknown command is requested', async () => {
Expand Down
2 changes: 2 additions & 0 deletions test/functional/lib/ipfs-proxy/pre-acl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ describe('lib/ipfs-proxy/pre-acl', () => {
}

expect(() => { if (error) throw error }).to.throw('User disabled access to API proxy in IPFS Companion')
expect(error.scope).to.equal(undefined)
expect(error.permissions).to.be.equal(undefined)
})

it('should request access if no grant exists', async () => {
Expand Down

0 comments on commit ac150f2

Please sign in to comment.