Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing error objects to HealthCheckError #175

Merged
merged 4 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/terminus.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ function noopResolves () {
return Promise.resolve()
}

function replaceErrors (key, value) {
if (value instanceof Error) {
const error = {}

Object.getOwnPropertyNames(value).forEach(function (key) {
error[key] = value[key]
})

return error
}

return value
}

async function sendSuccess (res, { info, verbatim }) {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
Expand Down Expand Up @@ -46,7 +60,7 @@ async function sendFailure (res, options) {
status: 'error',
error: error,
details: error
}))
}, replaceErrors))
}
res.end(FAILURE_RESPONSE)
}
Expand Down
41 changes: 40 additions & 1 deletion lib/terminus.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ describe('Terminus', () => {
expect(loggerRan).to.eql(true)
})

it('includes error on reject', async () => {
it('includes error on reject (custom errors)', async () => {
let onHealthCheckRan = false

createTerminus(server, {
Expand Down Expand Up @@ -254,6 +254,45 @@ describe('Terminus', () => {
})
})

it('includes error on reject (Error objects)', async () => {
let onHealthCheckRan = false

const errors = [
new Error('test error 1'),
new Error('test error 2')
]

createTerminus(server, {
healthChecks: {
'/health': () => {
onHealthCheckRan = true
const myError = new HealthCheckError('failed', errors)
return Promise.reject(myError)
}
}
})
server.listen(8000)

const res = await fetch('http://localhost:8000/health')
expect(res.status).to.eql(503)
expect(onHealthCheckRan).to.eql(true)
const json = await res.json()

const errorMessages = [{
message: errors[0].message,
stack: errors[0].stack.toString()
chriswiggins marked this conversation as resolved.
Show resolved Hide resolved
}, {
message: errors[1].message,
stack: errors[1].stack.toString()
}]

expect(json).to.deep.eql({
status: 'error',
error: errorMessages,
details: errorMessages
})
})

it('returns 503 once signal received', (done) => {
let responseAssertionsComplete = false

Expand Down