From ac150f284569b22653af6a832cb50dc4cbdde7e2 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Tue, 15 Jan 2019 12:51:23 +0100 Subject: [PATCH] refactor: standard error when window.ipfs is disabled --- add-on/src/lib/ipfs-proxy/pre-acl.js | 8 +++++--- test/functional/lib/ipfs-proxy/enable-command.test.js | 2 ++ test/functional/lib/ipfs-proxy/pre-acl.test.js | 2 ++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/add-on/src/lib/ipfs-proxy/pre-acl.js b/add-on/src/lib/ipfs-proxy/pre-acl.js index b8b2a7612..da9bac61b 100644 --- a/add-on/src/lib/ipfs-proxy/pre-acl.js +++ b/add-on/src/lib/ipfs-proxy/pre-acl.js @@ -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) @@ -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: { diff --git a/test/functional/lib/ipfs-proxy/enable-command.test.js b/test/functional/lib/ipfs-proxy/enable-command.test.js index 5791986b5..5614fae75 100644 --- a/test/functional/lib/ipfs-proxy/enable-command.test.js +++ b/test/functional/lib/ipfs-proxy/enable-command.test.js @@ -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 () => { diff --git a/test/functional/lib/ipfs-proxy/pre-acl.test.js b/test/functional/lib/ipfs-proxy/pre-acl.test.js index 4f5814029..658cb6e7b 100644 --- a/test/functional/lib/ipfs-proxy/pre-acl.test.js +++ b/test/functional/lib/ipfs-proxy/pre-acl.test.js @@ -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 () => {