Repository navigation
How is onAfterResponse supposed to know if the onError event was triggered? #1910
Replies: 2 comments
|
In the lifecycle docs, For error-specific work, do it in .onError(({ error, status, set }) => {
const code = error instanceof APIHandledError ? error.code : 500
set.status = code
return status(code, { status: 'error', message: error.message })
})
.onAfterResponse(({ set }) => {
if (Number(set.status ?? 200) >= 400) {
// error response path
}
}) |
|
c.response = c.responseValue = er // er = the value returned by your onError handlerbefore Two things follow. 1. Return something from every 2. if (c.responseValue) {
if (c.responseValue instanceof ElysiaCustomStatusResponse) c.set.status = c.responseValue.code
}So .onError(({ error, status, set }) => {
const code = error instanceof APIHandledError ? error.code : 500
set.status = code
return status(code, { status: 'error', message: error.message })
})
.onAfterResponse(({ set }) => {
const failed = Number(set.status ?? 200) >= 400
// ...
})If you need to distinguish "my error handler produced this" from "the handler returned a 4xx on purpose", put a flag on .onError(({ error, status, store }) => {
store.errored = true
// ...
})
.onAfterResponse(({ store }) => {
if (store.errored) { /* ... */ }
})One more thing worth knowing: |
Uh oh!
There was an error while loading. Please reload this page.
Tried to find in the documentation, but there has no clear way to do it.
you can shape the response with onError but the onAfterResponse only takes the response from the handler itself which is moot if the inside of that handler threw an error.
All reactions