-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_middleware.go
38 lines (31 loc) · 1010 Bytes
/
error_middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package gomiddlewares
import (
"encoding/json"
"net/http"
)
// ErrorHandlerMiddleware is a middleware function that wraps an HTTP handler
// and handles panics that occur during the execution of the wrapped handler.
// It captures and recovers from panics, then sends an appropriate HTTP
// response with status code and error message in JSON format.
func ErrorHandlerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if r := recover(); r != nil {
statusCode := http.StatusInternalServerError
errorMessage := "Internal Server Error"
if err, ok := r.(ErrorWithStatus); ok {
errorMessage = err.Error()
statusCode = err.StatusCode()
}
response := errorResponse{
Status: statusCode,
Message: errorMessage,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
json.NewEncoder(w).Encode(response)
}
}()
next.ServeHTTP(w, r)
})
}