Files
shields/core/base-service/check-error-response.spec.js
chris48s 45bb786147 log 429s to sentry (attempt 2); affects [dynamic endpoint uptimerobot weblate opencollective discord github] (#9546)
* log to sentry if upstream service responds with 429

* allow services to decide which error(s) to log, default to 429

* don't log 429s from endpoint or dynamic badges

* supress 429s from uptime robot badges

* supress 429s from weblate if not calling default server

* cache opencollective badges for longer

* cache discord badges for longer

* cache github workflow badges for longer
2023-12-04 13:37:58 +00:00

148 lines
4.8 KiB
JavaScript

import { expect } from 'chai'
import { NotFound, InvalidResponse, Inaccessible } from './errors.js'
import checkErrorResponse from './check-error-response.js'
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()({ 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()({ 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({ 404: notFoundMessage })({ res, buffer })
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(NotFound)
expect(e.message).to.equal('Not Found: no goblins found')
expect(e.prettyMessage).to.equal('no goblins found')
}
})
})
context('when status is 429', function () {
const buffer = Buffer.from('some stuff')
const res = { statusCode: 429, requestUrl: new URL('https://example.com/') }
it('throws InvalidResponse', async function () {
try {
await checkErrorResponse()({ res, buffer })
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(InvalidResponse)
expect(e.message).to.equal(
'Invalid Response: Got status code 429 (expected 200)',
)
expect(e.prettyMessage).to.equal('rate limited by upstream service')
expect(e.response).to.equal(res)
expect(e.buffer).to.equal(buffer)
}
})
it('displays the custom too many requests', async function () {
const notFoundMessage = "terribly sorry but that's one too many requests"
try {
await checkErrorResponse({ 429: notFoundMessage })({ res, buffer })
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(InvalidResponse)
expect(e.message).to.equal(
'Invalid Response: Got status code 429 (expected 200)',
)
expect(e.prettyMessage).to.equal(
"terribly sorry but that's one too many requests",
)
}
})
})
context('when status is 4xx', function () {
it('throws InvalidResponse', async function () {
const res = { statusCode: 499 }
try {
await checkErrorResponse()({ res, buffer })
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(InvalidResponse)
expect(e.message).to.equal(
'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)
}
})
it('displays the custom error message', async function () {
const res = { statusCode: 403 }
try {
await checkErrorResponse({ 403: 'access denied' })({ res })
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(InvalidResponse)
expect(e.message).to.equal(
'Invalid Response: Got status code 403 (expected 200)',
)
expect(e.prettyMessage).to.equal('access denied')
}
})
})
context('when status is 5xx', function () {
it('throws Inaccessible', async function () {
const res = { statusCode: 503 }
try {
await checkErrorResponse()({ res, buffer })
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(Inaccessible)
expect(e.message).to.equal(
'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)
}
})
it('displays the custom error message', async function () {
const res = { statusCode: 500 }
try {
await checkErrorResponse({ 500: 'server overloaded' })({ res, buffer })
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(Inaccessible)
expect(e.message).to.equal(
'Inaccessible: Got status code 500 (expected 200)',
)
expect(e.prettyMessage).to.equal('server overloaded')
}
})
})
})