July 2017
Beginner to intermediate
358 pages
10h 54m
English
The recover function allows an application to manage the behavior of a panicking goroutine. When called inside a deferred function, recover stops the execution of panic and returns the error passed to the call of panic:
func recover() interface{}
If our handler panics for some reason, the HTTP server will recover this panic and write the output to the std error. While this is fine if we are running the application locally, it does not allow us to manage the errors when our application is distributed across many remote servers. Since we have already logged into an ELK stack setup, we can write a simple handler that will wrap our main handler, allow the capture of any panic, and forward it to the logger:
18 func (p *panicHandler) ServeHTTP(rw ...
Read now
Unlock full access