Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

Commit

Permalink
properly propagate the errors from the session
Browse files Browse the repository at this point in the history
  • Loading branch information
http://jneen.net/ committed Dec 2, 2019
1 parent fed458a commit 59009d7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
13 changes: 3 additions & 10 deletions utils/context.helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,9 @@ const contextMiddleware = (req, res, next) => {
res.locals.enterContext = (key) => { keyPath.push(key) }
res.locals.exitContext = () => { keyPath.pop() }

// errors in the session
const errorState = req.session.errorState

// look up a key at the current keypath
res.locals.getData = (...keys) => lookupKeypath(res.locals, keyPath.concat(keys))
res.locals.getError = (...keys) => {
if (!errorState) return undefined

errorState.errors[errorPath(keyPath.concat(keys))]
}
res.locals.getError = (...keys) => (res.locals.errors || {})[errorPath(keyPath.concat(keys))]

res.locals.pad = (arr, len) => {
if (!len || len < 1) len = 1
Expand All @@ -52,8 +45,8 @@ const contextMiddleware = (req, res, next) => {
}

res.locals.isFirstError = (...keys) => {
if (!errorState) return false
return errorPath(keyPath.concat(keys)) === errorState.firstError
if (!req.session.errorState) return false
return errorPath(keyPath.concat(keys)) === req.session.errorState.firstError
}

next()
Expand Down
7 changes: 6 additions & 1 deletion utils/route.helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ class Route {
return this
}

render() { return (req, res) => res.render(this.name) }
render() {
return (req, res) => {
Object.assign(res.locals, req.session.errorState || {})
res.render(this.name)
}
}

/**
* The default middleware for this route, intended
Expand Down
2 changes: 1 addition & 1 deletion utils/validate.helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const checkErrors = (req, res, next) => {
if (!errors.isEmpty()) {
req.session.errorState = {
errors: errorArray2ErrorObject(errors),
firstError: errors[0].msg
firstError: errors.errors[0].msg
}

return res.redirect('back')
Expand Down

0 comments on commit 59009d7

Please sign in to comment.