Remove unused callback-based error helpers (#3371)
This commit is contained in:
@@ -217,7 +217,7 @@ module.exports = class BaseService {
|
|||||||
logTrace(emojic.bowAndArrow, 'Request', url, '\n', options)
|
logTrace(emojic.bowAndArrow, 'Request', url, '\n', options)
|
||||||
const { res, buffer } = await this._requestFetcher(url, options)
|
const { res, buffer } = await this._requestFetcher(url, options)
|
||||||
logTrace(emojic.dart, 'Response status code', res.statusCode)
|
logTrace(emojic.dart, 'Response status code', res.statusCode)
|
||||||
return checkErrorResponse.asPromise(errorMessages)({ buffer, res })
|
return checkErrorResponse(errorMessages)({ buffer, res })
|
||||||
}
|
}
|
||||||
|
|
||||||
static _validate(
|
static _validate(
|
||||||
|
|||||||
@@ -5,30 +5,12 @@ const {
|
|||||||
InvalidResponse,
|
InvalidResponse,
|
||||||
Inaccessible,
|
Inaccessible,
|
||||||
} = require('../core/base-service/errors')
|
} = require('../core/base-service/errors')
|
||||||
|
|
||||||
const defaultErrorMessages = {
|
const defaultErrorMessages = {
|
||||||
404: 'not found',
|
404: 'not found',
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkErrorResponse(badgeData, err, res, errorMessages = {}) {
|
function checkErrorResponse(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 = {}) {
|
|
||||||
return async function({ buffer, res }) {
|
return async function({ buffer, res }) {
|
||||||
let error
|
let error
|
||||||
errorMessages = { ...defaultErrorMessages, ...errorMessages }
|
errorMessages = { ...defaultErrorMessages, ...errorMessages }
|
||||||
|
|||||||
@@ -8,58 +8,16 @@ const {
|
|||||||
} = require('../core/base-service/errors')
|
} = require('../core/base-service/errors')
|
||||||
const { checkErrorResponse } = require('./error-helper')
|
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() {
|
describe('async error handler', function() {
|
||||||
const buffer = Buffer.from('some stuff')
|
const buffer = Buffer.from('some stuff')
|
||||||
|
|
||||||
context('when status is 200', function() {
|
context('when status is 200', function() {
|
||||||
it('passes through the inputs', async function() {
|
it('passes through the inputs', async function() {
|
||||||
const res = { statusCode: 200 }
|
const res = { statusCode: 200 }
|
||||||
expect(
|
expect(await checkErrorResponse()({ res, buffer })).to.deep.equal({
|
||||||
await checkErrorResponse.asPromise()({ res, buffer })
|
res,
|
||||||
).to.deep.equal({ res, buffer })
|
buffer,
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -69,7 +27,7 @@ describe('async error handler', function() {
|
|||||||
|
|
||||||
it('throws NotFound', async function() {
|
it('throws NotFound', async function() {
|
||||||
try {
|
try {
|
||||||
await checkErrorResponse.asPromise()({ res, buffer })
|
await checkErrorResponse()({ res, buffer })
|
||||||
expect.fail('Expected to throw')
|
expect.fail('Expected to throw')
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
expect(e).to.be.an.instanceof(NotFound)
|
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() {
|
it('displays the custom not found message', async function() {
|
||||||
const notFoundMessage = 'no goblins found'
|
const notFoundMessage = 'no goblins found'
|
||||||
try {
|
try {
|
||||||
await checkErrorResponse.asPromise({
|
await checkErrorResponse({ 404: notFoundMessage })({ res, buffer })
|
||||||
404: notFoundMessage,
|
|
||||||
})({ res, buffer })
|
|
||||||
expect.fail('Expected to throw')
|
expect.fail('Expected to throw')
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
expect(e).to.be.an.instanceof(NotFound)
|
expect(e).to.be.an.instanceof(NotFound)
|
||||||
@@ -99,7 +55,7 @@ describe('async error handler', function() {
|
|||||||
it('throws InvalidResponse', async function() {
|
it('throws InvalidResponse', async function() {
|
||||||
const res = { statusCode: 499 }
|
const res = { statusCode: 499 }
|
||||||
try {
|
try {
|
||||||
await checkErrorResponse.asPromise()({ res, buffer })
|
await checkErrorResponse()({ res, buffer })
|
||||||
expect.fail('Expected to throw')
|
expect.fail('Expected to throw')
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
expect(e).to.be.an.instanceof(InvalidResponse)
|
expect(e).to.be.an.instanceof(InvalidResponse)
|
||||||
@@ -115,9 +71,7 @@ describe('async error handler', function() {
|
|||||||
it('displays the custom error message', async function() {
|
it('displays the custom error message', async function() {
|
||||||
const res = { statusCode: 403 }
|
const res = { statusCode: 403 }
|
||||||
try {
|
try {
|
||||||
await checkErrorResponse.asPromise({
|
await checkErrorResponse({ 403: 'access denied' })({ res })
|
||||||
403: 'access denied',
|
|
||||||
})({ res })
|
|
||||||
expect.fail('Expected to throw')
|
expect.fail('Expected to throw')
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
expect(e).to.be.an.instanceof(InvalidResponse)
|
expect(e).to.be.an.instanceof(InvalidResponse)
|
||||||
@@ -133,7 +87,7 @@ describe('async error handler', function() {
|
|||||||
it('throws Inaccessible', async function() {
|
it('throws Inaccessible', async function() {
|
||||||
const res = { statusCode: 503 }
|
const res = { statusCode: 503 }
|
||||||
try {
|
try {
|
||||||
await checkErrorResponse.asPromise()({ res, buffer })
|
await checkErrorResponse()({ res, buffer })
|
||||||
expect.fail('Expected to throw')
|
expect.fail('Expected to throw')
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
expect(e).to.be.an.instanceof(Inaccessible)
|
expect(e).to.be.an.instanceof(Inaccessible)
|
||||||
@@ -149,9 +103,7 @@ describe('async error handler', function() {
|
|||||||
it('displays the custom error message', async function() {
|
it('displays the custom error message', async function() {
|
||||||
const res = { statusCode: 500 }
|
const res = { statusCode: 500 }
|
||||||
try {
|
try {
|
||||||
await checkErrorResponse.asPromise({
|
await checkErrorResponse({ 500: 'server overloaded' })({ res, buffer })
|
||||||
500: 'server overloaded',
|
|
||||||
})({ res, buffer })
|
|
||||||
expect.fail('Expected to throw')
|
expect.fail('Expected to throw')
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
expect(e).to.be.an.instanceof(Inaccessible)
|
expect(e).to.be.an.instanceof(Inaccessible)
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ module.exports = class BaseChromeWebStoreService extends BaseService {
|
|||||||
`statusCode` property so we can pass `e` to `checkErrorResponse`
|
`statusCode` property so we can pass `e` to `checkErrorResponse`
|
||||||
to throw the correct `ShieldsRuntimeError` for us.
|
to throw the correct `ShieldsRuntimeError` for us.
|
||||||
*/
|
*/
|
||||||
return checkErrorResponse.asPromise({})({ buffer: '', res: e })
|
return checkErrorResponse({})({ buffer: '', res: e })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,6 @@
|
|||||||
|
|
||||||
const serverSecrets = require('../../lib/server-secrets')
|
const serverSecrets = require('../../lib/server-secrets')
|
||||||
const { colorScale } = require('../color-formatters')
|
const { colorScale } = require('../color-formatters')
|
||||||
const {
|
|
||||||
checkErrorResponse: standardCheckErrorResponse,
|
|
||||||
} = require('../../lib/error-helper')
|
|
||||||
|
|
||||||
const documentation = `
|
const documentation = `
|
||||||
<p>
|
<p>
|
||||||
@@ -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)
|
const commentsColor = colorScale([1, 3, 10, 25], undefined, true)
|
||||||
|
|
||||||
function staticAuthConfigured() {
|
function staticAuthConfigured() {
|
||||||
@@ -51,6 +35,5 @@ module.exports = {
|
|||||||
stateColor,
|
stateColor,
|
||||||
commentsColor,
|
commentsColor,
|
||||||
errorMessagesFor,
|
errorMessagesFor,
|
||||||
checkErrorResponse,
|
|
||||||
staticAuthConfigured,
|
staticAuthConfigured,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
Reference in New Issue
Block a user