From aa8d93866afee84663480bcfe04c6c44e20c58d9 Mon Sep 17 00:00:00 2001 From: Dave Sewell Date: Fri, 9 Dec 2022 05:39:13 -0500 Subject: [PATCH] Prevent calling the callback more than once (#759) --- src/Auth0RestClient.js | 7 +++++-- test/auth0-rest-client.tests.js | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Auth0RestClient.js b/src/Auth0RestClient.js index 92080714b..88ff32958 100644 --- a/src/Auth0RestClient.js +++ b/src/Auth0RestClient.js @@ -73,9 +73,12 @@ class Auth0RestClient { this.restClient[method](...args, (err, data, headers) => { const payload = { data, headers }; if (err) { - (callback && callback(err)) || reject(err); + if (callback) callback(err); + else reject(err); + return; } - (callback && callback(null, payload)) || resolve(payload); + if (callback) callback(null, payload); + else resolve(payload); }); }); } diff --git a/test/auth0-rest-client.tests.js b/test/auth0-rest-client.tests.js index a0a3ef38a..96aed2b01 100644 --- a/test/auth0-rest-client.tests.js +++ b/test/auth0-rest-client.tests.js @@ -186,6 +186,29 @@ describe('Auth0RestClient', () => { }); }); + it('should accept a callback and handle errors with response headers', (done) => { + const providerMock = { + async getAccessToken() { + return 'fake-token'; + }, + }; + + const errorBody = { error: 'message' }; + nock(API_URL).get('/some-resource').reply(500, errorBody); + + const options = { + headers: {}, + includeResponseHeaders: true, + }; + const client = new Auth0RestClient(`${API_URL}/some-resource`, options, providerMock); + client.getAll((err) => { + expect(err).to.not.null; + expect(err.message).to.be.equal(JSON.stringify(errorBody)); + done(); + nock.cleanAll(); + }); + }); + it('should set access token as Authorization header in options object', async function () { nock(API_URL).get('/some-resource').reply(200);