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

"error" block of Jester does not work when using custom routers #211

Closed
hokamoto opened this issue Sep 20, 2019 · 2 comments · May be fixed by #267
Closed

"error" block of Jester does not work when using custom routers #211

hokamoto opened this issue Sep 20, 2019 · 2 comments · May be fixed by #267

Comments

@hokamoto
Copy link

This issue is from https://forum.nim-lang.org/t/5196

"error" block of Jester does not work when using custom routers. The below code dumps a stack trace instead of the expected message "An exception occurred".

import asyncdispatch, jester, os, strutils, htmlgen

router myrouter:
  get "/":
    raise(newException(OSError, "exception"))
  
  error Exception:
    resp h1("An exception occurred")

proc main() =
  let port = Port(5000)
  let settings = newSettings(port=port)
  var jester = initJester(myrouter, settings=settings)
  jester.serve()

when isMainModule:
  main()
$ nim -v
Nim Compiler Version 0.20.2 [MacOSX: amd64]
Compiled at 2019-07-18
Copyright (c) 2006-2019 by Andreas Rumpf

active boot switches: -d:release -d:useLinenoise
$ nimble list -i --ver
asynctools  [#pr_fix_compilation]
httpbeast  [0.2.2]
jester  [#head]
@capocasa
Copy link

I managed to use this workaround

proc errorHandler(request: Request, error: RouteError): Future[ResponseData] {.async.} =
  block route:
    case error.kind:
      of RouteException:
        let e = getCurrentException()  # error.exc is private
        if e of MyAuthError:
          resp Http401, {"Access-Control-Allow-Origin":"*", "Content-Type":"application/json"}, e.msg
        elif e of ValueError:
          resp Http400, {"Access-Control-Allow-Origin":"*", "Content-Type":"application/json"}, e.msg
        else:
          resp Http500, {"Access-Control-Allow-Origin":"*", "Content-Type":"application/json"}, e.msg
      of RouteCode:
        discard

proc main() =
  let port = Port(5000)
  let settings = newSettings(port=port)
  var jester = initJester(myrouter, settings=settings)
  jester.register(errorHandler)
  jester.serve()

when isMainModule:
  main()

@iffy
Copy link
Contributor

iffy commented Sep 18, 2020

Another workaround that registers the jester-provided error handler:

import asyncdispatch, jester, os, strutils, htmlgen

router myrouter:
  get "/":
    raise(newException(OSError, "exception"))
  
  error Exception:
    resp h1("An exception occurred")

proc main() =
  let port = Port(5000)
  let settings = newSettings(port=port)
  var jester = initJester(myrouter, settings=settings)
  jester.register(myrouterErrorHandler) # <---- name this ROUTERNAME & "ErrorHandler"
  jester.serve()

when isMainModule:
  main()

iffy added a commit to iffy/jester that referenced this issue Sep 18, 2020
iffy added a commit to iffy/jester that referenced this issue Sep 23, 2020
iffy added a commit to iffy/jester that referenced this issue Jun 10, 2022
@dom96 dom96 closed this as completed in d2210c6 Jun 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants