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”.

example use case
How the error report looks like

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.

Shalom.

Repository: https://github.com/joshuaetim/error_handler

Defer, Panics, and Recover: https://go.dev/blog/defer-panic-and-recover

Debug package: https://pkg.go.dev/runtime/debug

--

--

Backend Developer with Go and PHP. I am passionate about building and improving reliable business software.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Joshua Etim

Backend Developer with Go and PHP. I am passionate about building and improving reliable business software.