diff --git a/core/base-service/base.js b/core/base-service/base.js index 1a3375a83a..ab0d9010f3 100644 --- a/core/base-service/base.js +++ b/core/base-service/base.js @@ -217,7 +217,7 @@ module.exports = class BaseService { logTrace(emojic.bowAndArrow, 'Request', url, '\n', options) const { res, buffer } = await this._requestFetcher(url, options) logTrace(emojic.dart, 'Response status code', res.statusCode) - return checkErrorResponse.asPromise(errorMessages)({ buffer, res }) + return checkErrorResponse(errorMessages)({ buffer, res }) } static _validate( diff --git a/lib/error-helper.js b/lib/error-helper.js index e91a6f96b9..8cf9553e0c 100644 --- a/lib/error-helper.js +++ b/lib/error-helper.js @@ -5,30 +5,12 @@ const { InvalidResponse, Inaccessible, } = require('../core/base-service/errors') + const defaultErrorMessages = { 404: 'not found', } -function checkErrorResponse(badgeData, err, res, errorMessages = {}) { - errorMessages = { ...defaultErrorMessages, ...errorMessages } - if (err != null) { - badgeData.text[1] = 'inaccessible' - badgeData.colorscheme = 'red' - return true - } else if (errorMessages[res.statusCode] !== undefined) { - badgeData.text[1] = errorMessages[res.statusCode] - badgeData.colorscheme = 'lightgrey' - return true - } else if (res.statusCode !== 200) { - badgeData.text[1] = 'invalid' - badgeData.colorscheme = 'lightgrey' - return true - } else { - return false - } -} - -checkErrorResponse.asPromise = function(errorMessages = {}) { +function checkErrorResponse(errorMessages = {}) { return async function({ buffer, res }) { let error errorMessages = { ...defaultErrorMessages, ...errorMessages } diff --git a/lib/error-helper.spec.js b/lib/error-helper.spec.js index d4f54acb65..d008569be3 100644 --- a/lib/error-helper.spec.js +++ b/lib/error-helper.spec.js @@ -8,58 +8,16 @@ const { } = require('../core/base-service/errors') const { checkErrorResponse } = require('./error-helper') -describe('Standard Error Handler', function() { - it('makes inaccessible badge', function() { - const badgeData = { text: [] } - expect(checkErrorResponse(badgeData, 'something other than null', {})).to.be - .true - expect(badgeData.text[1]).to.equal('inaccessible') - expect(badgeData.colorscheme).to.equal('red') - }) - - it('makes not found badge', function() { - const badgeData = { text: [] } - expect(checkErrorResponse(badgeData, null, { statusCode: 404 })).to.be.true - expect(badgeData.text[1]).to.equal('not found') - expect(badgeData.colorscheme).to.equal('lightgrey') - }) - - it('makes not found badge with custom error', function() { - const badgeData = { text: [] } - expect( - checkErrorResponse( - badgeData, - null, - { statusCode: 404 }, - { 404: 'custom message' } - ) - ).to.be.true - expect(badgeData.text[1]).to.equal('custom message') - expect(badgeData.colorscheme).to.equal('lightgrey') - }) - - it('makes invalid badge', function() { - const badgeData = { text: [] } - expect(checkErrorResponse(badgeData, null, { statusCode: 500 })).to.be.true - expect(badgeData.text[1]).to.equal('invalid') - expect(badgeData.colorscheme).to.equal('lightgrey') - }) - - it('return false on 200 status', function() { - expect(checkErrorResponse({ text: [] }, null, { statusCode: 200 })).to.be - .false - }) -}) - describe('async error handler', function() { const buffer = Buffer.from('some stuff') context('when status is 200', function() { it('passes through the inputs', async function() { const res = { statusCode: 200 } - expect( - await checkErrorResponse.asPromise()({ res, buffer }) - ).to.deep.equal({ res, buffer }) + expect(await checkErrorResponse()({ res, buffer })).to.deep.equal({ + res, + buffer, + }) }) }) @@ -69,7 +27,7 @@ describe('async error handler', function() { it('throws NotFound', async function() { try { - await checkErrorResponse.asPromise()({ res, buffer }) + await checkErrorResponse()({ res, buffer }) expect.fail('Expected to throw') } catch (e) { expect(e).to.be.an.instanceof(NotFound) @@ -83,9 +41,7 @@ describe('async error handler', function() { it('displays the custom not found message', async function() { const notFoundMessage = 'no goblins found' try { - await checkErrorResponse.asPromise({ - 404: notFoundMessage, - })({ res, buffer }) + await checkErrorResponse({ 404: notFoundMessage })({ res, buffer }) expect.fail('Expected to throw') } catch (e) { expect(e).to.be.an.instanceof(NotFound) @@ -99,7 +55,7 @@ describe('async error handler', function() { it('throws InvalidResponse', async function() { const res = { statusCode: 499 } try { - await checkErrorResponse.asPromise()({ res, buffer }) + await checkErrorResponse()({ res, buffer }) expect.fail('Expected to throw') } catch (e) { expect(e).to.be.an.instanceof(InvalidResponse) @@ -115,9 +71,7 @@ describe('async error handler', function() { it('displays the custom error message', async function() { const res = { statusCode: 403 } try { - await checkErrorResponse.asPromise({ - 403: 'access denied', - })({ res }) + await checkErrorResponse({ 403: 'access denied' })({ res }) expect.fail('Expected to throw') } catch (e) { expect(e).to.be.an.instanceof(InvalidResponse) @@ -133,7 +87,7 @@ describe('async error handler', function() { it('throws Inaccessible', async function() { const res = { statusCode: 503 } try { - await checkErrorResponse.asPromise()({ res, buffer }) + await checkErrorResponse()({ res, buffer }) expect.fail('Expected to throw') } catch (e) { expect(e).to.be.an.instanceof(Inaccessible) @@ -149,9 +103,7 @@ describe('async error handler', function() { it('displays the custom error message', async function() { const res = { statusCode: 500 } try { - await checkErrorResponse.asPromise({ - 500: 'server overloaded', - })({ res, buffer }) + await checkErrorResponse({ 500: 'server overloaded' })({ res, buffer }) expect.fail('Expected to throw') } catch (e) { expect(e).to.be.an.instanceof(Inaccessible) diff --git a/services/chrome-web-store/chrome-web-store-base.js b/services/chrome-web-store/chrome-web-store-base.js index b5451437fc..29c30ef659 100644 --- a/services/chrome-web-store/chrome-web-store-base.js +++ b/services/chrome-web-store/chrome-web-store-base.js @@ -17,7 +17,7 @@ module.exports = class BaseChromeWebStoreService extends BaseService { `statusCode` property so we can pass `e` to `checkErrorResponse` to throw the correct `ShieldsRuntimeError` for us. */ - return checkErrorResponse.asPromise({})({ buffer: '', res: e }) + return checkErrorResponse({})({ buffer: '', res: e }) } } } diff --git a/services/github/github-helpers.js b/services/github/github-helpers.js index 6e31be25d0..9851bf4404 100644 --- a/services/github/github-helpers.js +++ b/services/github/github-helpers.js @@ -2,9 +2,6 @@ const serverSecrets = require('../../lib/server-secrets') const { colorScale } = require('../color-formatters') -const { - checkErrorResponse: standardCheckErrorResponse, -} = require('../../lib/error-helper') const documentation = `
@@ -27,19 +24,6 @@ function errorMessagesFor(notFoundMessage = 'repo not found') { } } -function checkErrorResponse( - badgeData, - err, - res, - notFoundMessage = 'repo not found', - errorMessages = {} -) { - return standardCheckErrorResponse(badgeData, err, res, { - ...errorMessages, - ...errorMessagesFor(notFoundMessage), - }) -} - const commentsColor = colorScale([1, 3, 10, 25], undefined, true) function staticAuthConfigured() { @@ -51,6 +35,5 @@ module.exports = { stateColor, commentsColor, errorMessagesFor, - checkErrorResponse, staticAuthConfigured, } diff --git a/services/github/github-helpers.spec.js b/services/github/github-helpers.spec.js deleted file mode 100644 index 7e97504461..0000000000 --- a/services/github/github-helpers.spec.js +++ /dev/null @@ -1,37 +0,0 @@ -'use strict' - -const { expect } = require('chai') -const { checkErrorResponse } = require('./github-helpers') - -describe('GitHub Error Handler', function() { - it('makes repo not found badge when 422 is returned', function() { - const badgeData = { text: [] } - expect(checkErrorResponse(badgeData, null, { statusCode: 422 })).to.be.true - expect(badgeData.text[1]).to.equal('repo not found') - expect(badgeData.colorscheme).to.equal('lightgrey') - }) - - it('makes user not found badge when 404 is returned', function() { - const badgeData = { text: [] } - expect( - checkErrorResponse(badgeData, null, { statusCode: 404 }, 'user not found') - ).to.be.true - expect(badgeData.text[1]).to.equal('user not found') - expect(badgeData.colorscheme).to.equal('lightgrey') - }) - - it('makes badge with custom message when specified', function() { - const badgeData = { text: [] } - expect( - checkErrorResponse( - badgeData, - null, - { statusCode: 418 }, - 'repo not found', - { 418: "I'm a teapot" } - ) - ).to.be.true - expect(badgeData.text[1]).to.equal("I'm a teapot") - expect(badgeData.colorscheme).to.equal('lightgrey') - }) -})