krisppurg / dimscord

A Discord Bot & REST Library for Nim.
https://krisppurg.github.io/dimscord/
MIT License
222 stars 22 forks source link

Bubble the exception up instead of creating a new one #104

Closed ire4ever1190 closed 1 year ago

ire4ever1190 commented 1 year ago

Uses a bare raise statement inside the except blocks so that it reraises the exception instead of making a new one. Has the benefits of

template reRaise() = 
  when defined(bareRaise):
    raise
  else:
    raise newException(Exception, getCurrentExceptionMsg())

proc foo() = 
  raise (ref ValueError)(msg: "Test")

proc bar() = 
  try:
    foo()
  except:
    reRaise()

proc main() =
  try:
    bar()
  except ValueError:
    reRaise()

main()

Bare raise

/tmp/test.nim(22)        test
/tmp/test.nim(17)        main
/tmp/test.nim(11)        bar
/tmp/test.nim(6)         foo
Error: unhandled exception: Test [ValueError]

New exception

/tmp/test.nim(22)        test
/tmp/test.nim(17)        main
/tmp/test.nim(13)        bar
Error: unhandled exception: Test [Exception]

See how the new exception loses the call to foo and also removes the ValueError type from the exception.

Sorry about the weird whitespace changes, my editor just does that