As mentioned in https://forum.nim-lang.org/t/2863#17817 we now throw errors with:
type
MyCustomError* = object of Exception
...
proc doStuff(): int =
raise newException(MyCustomError, "didn't do stuff")
what if I want add informations, like:
{"message": "Invalid value",
"position": {"line": 1, "column": 2, "from": "formatTime"}}
can I do that in Nim? Or do I have to encode that in the string for the purpose that I can catch information from outer functions?
Of course, just add the fields to your exception type:
Nim
type
MyException = ref object of Exception
extraData: string
proc raiseSomething() =
var e: MyException
new(e)
e.extraData = "Hello world"
e.msg = "This is the normal message"
raise e
try:
raiseSomething()
except MyException as e:
echo e.msg
echo e.extraData