set a custom error on 429 (#9159)
Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
This commit is contained in:
@@ -2,6 +2,7 @@ import { NotFound, InvalidResponse, Inaccessible } from './errors.js'
|
||||
|
||||
const defaultErrorMessages = {
|
||||
404: 'not found',
|
||||
429: 'rate limited by upstream service',
|
||||
}
|
||||
|
||||
export default function checkErrorResponse(errorMessages = {}) {
|
||||
|
||||
@@ -45,6 +45,42 @@ describe('async error handler', function () {
|
||||
})
|
||||
})
|
||||
|
||||
context('when status is 429', function () {
|
||||
const buffer = Buffer.from('some stuff')
|
||||
const res = { statusCode: 429 }
|
||||
|
||||
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 }
|
||||
|
||||
@@ -119,7 +119,6 @@ export default class Matrix extends BaseJsonService {
|
||||
errorMessages: {
|
||||
401: 'auth failed',
|
||||
403: 'guests not allowed',
|
||||
429: 'rate limited by remote server',
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -138,7 +137,6 @@ export default class Matrix extends BaseJsonService {
|
||||
errorMessages: {
|
||||
401: 'bad auth token',
|
||||
404: 'room not found',
|
||||
429: 'rate limited by remote server',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -40,7 +40,6 @@ export default class WeblateComponentLicense extends WeblateBase {
|
||||
errorMessages: {
|
||||
403: 'access denied by remote server',
|
||||
404: 'component not found',
|
||||
429: 'rate limited by remote server',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -38,7 +38,6 @@ export default class WeblateEntities extends WeblateBase {
|
||||
url: `${server}/api/${type}/`,
|
||||
errorMessages: {
|
||||
403: 'access denied by remote server',
|
||||
429: 'rate limited by remote server',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -52,7 +52,6 @@ export default class WeblateProjectTranslatedPercentage extends WeblateBase {
|
||||
errorMessages: {
|
||||
403: 'access denied by remote server',
|
||||
404: 'project not found',
|
||||
429: 'rate limited by remote server',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -52,7 +52,6 @@ export default class WeblateUserStatistic extends WeblateBase {
|
||||
errorMessages: {
|
||||
403: 'access denied by remote server',
|
||||
404: 'user not found',
|
||||
429: 'rate limited by remote server',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user