Add InvalidParameter runtime error and return inaccessible for 5xx errors (#1890)
* InvalidParameter: New error type * Return inaccessible for 5xx errors from services * Add test for Inaccessible on 5xx * Add tests for named error types
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
'use strict'
|
||||
|
||||
const { NotFound, InvalidResponse } = require('../services/errors')
|
||||
const {
|
||||
NotFound,
|
||||
InvalidResponse,
|
||||
Inaccessible,
|
||||
} = require('../services/errors')
|
||||
|
||||
const checkErrorResponse = function(
|
||||
badgeData,
|
||||
@@ -27,13 +31,15 @@ const checkErrorResponse = function(
|
||||
|
||||
checkErrorResponse.asPromise = function({ notFoundMessage } = {}) {
|
||||
return async function({ buffer, res }) {
|
||||
const underlyingError = Error(
|
||||
`Got status code ${res.statusCode} (expected 200)`
|
||||
)
|
||||
if (res.statusCode === 404) {
|
||||
throw new NotFound({ prettyMessage: notFoundMessage })
|
||||
} else if (res.statusCode >= 500) {
|
||||
throw new Inaccessible({ underlyingError })
|
||||
} else if (res.statusCode !== 200) {
|
||||
const underlying = Error(
|
||||
`Got status code ${res.statusCode} (expected 200)`
|
||||
)
|
||||
throw new InvalidResponse({ underlyingError: underlying })
|
||||
throw new InvalidResponse({ underlyingError })
|
||||
}
|
||||
return { buffer, res }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user