Hello,
I have a long-running daemon process in production that is generally stable. However, in the rare event that a defect occurs in the future, I want to:
I understand that catching defects is generally discouraged. However, in my experiments, I observed the following behaviors:
With --panics:on, the program terminates immediately, outputting an error message (to what I think is stderr). For example:
fatal.nim(53) sysFatal
Error: unhandled exception: index 3 not in 0 .. 1 [IndexDefect]
With --panics:off, I can indeed catch the defect, but the trace member in the exception object refers only to sysFatal, lacking detailed stack trace information about the origin of the error:
@[StackTraceEntry(procname: "sysFatal", line: 53, filename: "fatal.nim")]
Is there an effective method to achieve detailed logging of defects, including stack traces that pinpoints the origin of the error (for logging), before terminating the process?
Thank you for your assistance.
For logging you can use the unhandledExceptionHook like so:
unhandledExceptionHook = proc(e: ref Exception) {.nimcall.} =
if e of Defect:
echo "we're doing stuff"
proc main() =
raise (ref IndexDefect)(msg: "No")
main()