I have this piece of error handling middleware:
func Error(next http.HandlerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
log.Error("Caught error in defer/recover middleware: ", err)
originalError := err.(struct{ OriginalError error }).OriginalError
if originalError != nil {
log.Error("Original error in defer/recover middleware: ", originalError)
}
statusCode := err.(struct{ StatusCode int }).StatusCode
if statusCode != 0 {
w.WriteHeader(statusCode)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
message := err.(struct{ Message string }).Message
if message == "" {
message = "Unknown error message."
}
json.NewEncoder(w).Encode(struct {
ID string
}{
message,
})
}
}()
next.ServeHTTP(w, r)
})
}
it will run at least once per request since it's deferred, and I get this error:
ERRO[0009] Caught error in defer/recover middleware: runtime error: index out of range
2018/12/19 16:53:47 http: panic serving 127.0.0.1:50394: interface conversion: interface {} is runtime.errorString, not struct { OriginalError error }
goroutine 34 [running]:
net/http.(*conn).serve.func1(0xc420224000)
/usr/lib/go-1.10/src/net/http/server.go:1726 +0xd0
panic(0x700300, 0xc420232340)
/usr/lib/go-1.10/src/runtime/panic.go:502 +0x229
huru/mw.Error.func1.1(0x7a5b60, 0xc420244000)
/home/oleg/codes/huru/api/src/huru/mw/middleware.go:113 +0x309
panic(0x6fd380, 0x919c10)
/usr/lib/go-1.10/src/runtime/panic.go:502 +0x229
huru/mw.Auth.func1(0x7a5b60, 0xc420244000, 0xc42023a200)
/home/oleg/codes/huru/api/src/huru/mw/middleware.go:53 +0x4ae
net/http.HandlerFunc.ServeHTTP(0xc4201133c0, 0x7a5b60, 0xc420244000, 0xc42023a200)
/usr/lib/go-1.10/src/net/http/server.go:1947 +0x44
huru/mw.Logging.func1(0x7a5b60, 0xc420244000, 0xc42023a200)
/home/oleg/codes/huru/api/src/huru/mw/middleware.go:21 +0xcb
net/http.HandlerFunc.ServeHTTP(0xc4200117c0, 0x7a5b60, 0xc420244000, 0xc42023a200)
/usr/lib/go-1.10/src/net/http/server.go:1947 +0x44
huru/mw.Error.func1(0x7a5b60, 0xc420244000, 0xc42023a200)
/home/oleg/codes/huru/api/src/huru/mw/middleware.go:140 +0x86
net/http.HandlerFunc.ServeHTTP(0xc4200117d0, 0x7a5b60, 0xc420244000, 0xc42023a200)
/usr/lib/go-1.10/src/net/http/server.go:1947 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc42013e000, 0x7a5b60, 0xc420244000, 0xc42023a200)
/home/oleg/codes/huru/api/src/github.com/gorilla/mux/mux.go:162 +0xed
net/http.serverHandler.ServeHTTP(0xc4200a5110, 0x7a5b60, 0xc420244000, 0xc42023a000)
/usr/lib/go-1.10/src/net/http/server.go:2697 +0xbc
I can't figure out how to fix it.