Skip to content

Commit

Permalink
[auth][m]: upgrade error handler to work with kratos v0.4.6-alpha.
Browse files Browse the repository at this point in the history
  • Loading branch information
anuveyatsu committed Apr 1, 2021
1 parent 2727088 commit bb0b0fe
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions routes/auth/errorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,9 @@ module.exports.errorHandler = (req, res, next) => {

kratos
.getSelfServiceError(error)
.then(
({
body,
response,
}) => {
if (response.statusCode == 404) {
// The error could not be found, redirect back to home.
res.redirect(config.get('SITE_URL'))
return
}

return body
}
)
.then((errorContainer = {}) => {
if ('errors' in errorContainer) {
const errorMessage = JSON.stringify(errorContainer.errors, null, 2)
.then(({ status, data: body }) => {
if ('errors' in body) {
const errorMessage = JSON.stringify(body.errors, null, 2)
logger.warn(errorMessage)
req.flash(
'info',
Expand All @@ -45,11 +31,23 @@ module.exports.errorHandler = (req, res, next) => {
}

return Promise.reject(
`expected errorContainer to contain "errors" but got ${JSON.stringify(
errorContainer
`expected body to contain "errors" but got ${JSON.stringify(
body
)}`
)
})
.catch(err => next(err))
}
.catch((err) => {
if (!err.response) {
next(err)
return
}

if (err.response.status === 404) {
// The error could not be found, redirect back to home.
res.redirect(config.get('SITE_URL'))
return
}

next(err)
})
}

0 comments on commit bb0b0fe

Please sign in to comment.