MSP-437 implement error handler
This commit is contained in:
1 parent
a73b9c119e
commit
f9f127f6b9
1 file changed
+35
@@ -0,0 +1,35 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/integration-system/isp-kit/log"
|
||||
"isp-gate-service/httperrors"
|
||||
"isp-gate-service/request"
|
||||
)
|
||||
|
||||
type HttpError interface {
|
||||
WriteError(w http.ResponseWriter) error
|
||||
}
|
||||
|
||||
func ErrorHandler(logger log.Logger) Middleware {
|
||||
return func(next Handler) Handler {
|
||||
return HandlerFunc(func(ctx *request.Context) error {
|
||||
err := next.Handle(ctx)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
logger.Error(ctx.Context(), err)
|
||||
|
||||
httpErr, ok := err.(HttpError)
|
||||
if ok {
|
||||
return httpErr.WriteError(ctx.ResponseWriter())
|
||||
}
|
||||
|
||||
return httperrors.
|
||||
New(http.StatusInternalServerError, "internal service error", err).
|
||||
WriteError(ctx.ResponseWriter())
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user