Rewrite GitHub commit status (#3186)

* WIP

* Parse the error response

* Clarify

* Restore one test

* Add a schema
This commit is contained in:
Paul Melnikow
2019-03-10 19:43:37 -04:00
committed by Caleb Cartwright
parent cbcf980182
commit 3733de6232
5 changed files with 96 additions and 149 deletions

View File

@@ -30,9 +30,10 @@ function checkErrorResponse(badgeData, err, res, errorMessages = {}) {
checkErrorResponse.asPromise = function(errorMessages = {}) {
return async function({ buffer, res }) {
let error
errorMessages = { ...defaultErrorMessages, ...errorMessages }
if (res.statusCode === 404) {
throw new NotFound({ prettyMessage: errorMessages[404] })
error = new NotFound({ prettyMessage: errorMessages[404] })
} else if (res.statusCode !== 200) {
const underlying = Error(
`Got status code ${res.statusCode} (expected 200)`
@@ -42,11 +43,18 @@ checkErrorResponse.asPromise = function(errorMessages = {}) {
props.prettyMessage = errorMessages[res.statusCode]
}
if (res.statusCode >= 500) {
throw new Inaccessible(props)
error = new Inaccessible(props)
} else {
error = new InvalidResponse(props)
}
throw new InvalidResponse(props)
}
return { buffer, res }
if (error) {
error.response = res
error.buffer = buffer
throw error
} else {
return { buffer, res }
}
}
}

View File

@@ -52,31 +52,40 @@ describe('Standard Error Handler', function() {
})
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 args = { buffer: 'buffer', res: { statusCode: 200 } }
expect(await checkErrorResponse.asPromise()(args)).to.deep.equal(args)
const res = { statusCode: 200 }
expect(
await checkErrorResponse.asPromise()({ res, buffer })
).to.deep.equal({ res, buffer })
})
})
context('when status is 404', function() {
const buffer = Buffer.from('some stuff')
const res = { statusCode: 404 }
it('throws NotFound', async function() {
try {
await checkErrorResponse.asPromise()({ res })
await checkErrorResponse.asPromise()({ res, buffer })
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(NotFound)
expect(e.message).to.equal('Not Found')
expect(e.prettyMessage).to.equal('not found')
expect(e.response).to.equal(res)
expect(e.buffer).to.equal(buffer)
}
})
it('displays the custom not found message', async function() {
const notFoundMessage = 'no goblins found'
try {
await checkErrorResponse.asPromise({ 404: notFoundMessage })({ res })
await checkErrorResponse.asPromise({
404: notFoundMessage,
})({ res, buffer })
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(NotFound)
@@ -90,7 +99,7 @@ describe('async error handler', function() {
it('throws InvalidResponse', async function() {
const res = { statusCode: 499 }
try {
await checkErrorResponse.asPromise()({ res })
await checkErrorResponse.asPromise()({ res, buffer })
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(InvalidResponse)
@@ -98,6 +107,8 @@ describe('async error handler', function() {
'Invalid Response: Got status code 499 (expected 200)'
)
expect(e.prettyMessage).to.equal('invalid')
expect(e.response).to.equal(res)
expect(e.buffer).to.equal(buffer)
}
})
@@ -122,7 +133,7 @@ describe('async error handler', function() {
it('throws Inaccessible', async function() {
const res = { statusCode: 503 }
try {
await checkErrorResponse.asPromise()({ res })
await checkErrorResponse.asPromise()({ res, buffer })
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(Inaccessible)
@@ -130,6 +141,8 @@ describe('async error handler', function() {
'Inaccessible: Got status code 503 (expected 200)'
)
expect(e.prettyMessage).to.equal('inaccessible')
expect(e.response).to.equal(res)
expect(e.buffer).to.equal(buffer)
}
})
@@ -138,7 +151,7 @@ describe('async error handler', function() {
try {
await checkErrorResponse.asPromise({
500: 'server overloaded',
})({ res })
})({ res, buffer })
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(Inaccessible)