Build Error Reporter for Golang projects
I have always liked Python’s and Laravel's way of reporting errors, especially in developer mode where you are taken through an actual walkthrough of your code with a highlight of your error and why that error happened. So I started working on something similar to that. There’s no name yet so I just called it “error_handler”.
The codebase is hosted publicly at https://github.com/joshuaetim/error_handler and is pretty self-explanatory. I took advantage of the recover method for Go’s panic handling (this error handler is reserved for panics only), and the debug package.
What happened here is fairly simple: I used recover() as I normally would — to catch panics, then used the debug package to get information from the stack trace. Some strings operations happen to extract the useful information we need and we give some prettier error responses for commonly encountered errors. After that, I went into the file in question to extract lines of code that might give the error better context, formatted it for better reading, and print it to the standard output. The result is an error report like the screenshot above.
There’s still more to be done — this was a little adventure that turned out useful. Let me know what you think in the comments or my socials on the repository.
Defer, Panics, and Recover: https://go.dev/blog/defer-panic-and-recover
Debug package: https://pkg.go.dev/runtime/debug