Skip to content

Commit

Permalink
Fix dom96#269 - you can now redirect within error handlers again
Browse files Browse the repository at this point in the history
  • Loading branch information
iffy committed Mar 17, 2021
1 parent 6009065 commit 70cf6c3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
4 changes: 4 additions & 0 deletions changelog.markdown
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Jester changelog

## x.x.x

Fix a bug that prevented redirecting from within error handlers ([#269]([#269](https://github.com/dom96/jester/issues/269)))

## 0.4.3 - 12/08/2019

Minor release correcting a few packaging issues and includes some other
Expand Down
36 changes: 17 additions & 19 deletions jester.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1342,29 +1342,27 @@ proc routesEx(name: string, body: NimNode): NimNode =
# Error handler proc
let errorHandlerIdent = newIdentNode(name & "ErrorHandler")
let errorIdent = newIdentNode("error")
let exceptionIdent = newIdentNode("exception")
let resultIdent = newIdentNode("result")
var errorHandlerProc = quote do:
proc `errorHandlerIdent`(
`reqIdent`: Request, `errorIdent`: RouteError
): Future[ResponseData] {.gcsafe, async.} =
block `routesListIdent`:
`setDefaultRespIdent`()
case `errorIdent`.kind
of RouteException:
discard
of RouteCode:
discard
let allRoutesIdent = ident("allRoutes")
var exceptionStmts = newStmtList()
if exceptionBranches.len != 0:
var stmts = newStmtList()
for branch in exceptionBranches:
stmts.add(newIfStmt(branch))
errorHandlerProc[6][0][1][^1][1][1][0] = stmts
exceptionStmts.add(newIfStmt(branch))
var codeStmts = newStmtList()
if httpCodeBranches.len != 0:
var stmts = newStmtList()
for branch in httpCodeBranches:
stmts.add(newIfStmt(branch))
errorHandlerProc[6][0][1][^1][2][1][0] = stmts
codeStmts.add(newIfStmt(branch))
var errorHandlerProc = quote do:
proc `errorHandlerIdent`(
`reqIdent`: Request, `errorIdent`: RouteError
): Future[ResponseData] {.gcsafe, async.} =
block `allRoutesIdent`:
block `routesListIdent`:
`setDefaultRespIdent`()
case `errorIdent`.kind
of RouteException:
`exceptionStmts`
of RouteCode:
`codeStmts`
result.add(errorHandlerProc)

# Pair the matcher and error matcher
Expand Down
7 changes: 6 additions & 1 deletion tests/customRouter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ import jester
router myrouter:
get "/":
resp "Hello world"

get "/404":
resp "you got 404"

get "/raise":
raise newException(Exception, "Foobar")

error Exception:
resp Http500, "Something bad happened: " & exception.msg

error Http404:
redirect uri("/404")

when isMainModule:
let s = newSettings(
Port(5454),
bindAddr="127.0.0.1",
)
var jest = initJester(myrouter, s)
# jest.register(myrouterErrorHandler)
jest.serve()
6 changes: 6 additions & 0 deletions tests/tester.nim
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ proc customRouterTest(useStdLib: bool) =
let body = (waitFor resp.body)
checkpoint body
check body.startsWith("Something bad happened: Foobar")

test "redirect in error":
let resp = waitFor client.get(address & "/definitely404route")
check resp.code == Http303
check resp.headers["location"] == address & "/404"
check (waitFor resp.body) == ""

when isMainModule:
try:
Expand Down

0 comments on commit 70cf6c3

Please sign in to comment.