2022-10-30 02:15:35 +00:00
|
|
|
package mid
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2022-10-30 04:05:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent"
|
2022-10-30 02:15:35 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/sys/validate"
|
2023-04-09 18:39:43 +00:00
|
|
|
"github.com/hay-kot/httpkit/errchain"
|
|
|
|
"github.com/hay-kot/httpkit/server"
|
2022-10-30 02:15:35 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
)
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
type ErrorResponse struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
Fields map[string]string `json:"fields,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-03-02 00:07:03 +00:00
|
|
|
func Errors(log zerolog.Logger) errchain.ErrorHandler {
|
2023-03-21 04:32:10 +00:00
|
|
|
return func(h errchain.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2022-10-30 02:15:35 +00:00
|
|
|
err := h.ServeHTTP(w, r)
|
|
|
|
if err != nil {
|
2023-03-21 04:32:10 +00:00
|
|
|
var resp ErrorResponse
|
2022-10-30 02:15:35 +00:00
|
|
|
var code int
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
traceID := r.Context().Value(middleware.RequestIDKey).(string)
|
2022-10-30 02:15:35 +00:00
|
|
|
log.Err(err).
|
2023-03-21 04:32:10 +00:00
|
|
|
Stack().
|
|
|
|
Str("req_id", traceID).
|
2022-10-30 02:15:35 +00:00
|
|
|
Msg("ERROR occurred")
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case validate.IsUnauthorizedError(err):
|
|
|
|
code = http.StatusUnauthorized
|
2023-03-21 04:32:10 +00:00
|
|
|
resp = ErrorResponse{
|
2022-10-30 02:15:35 +00:00
|
|
|
Error: "unauthorized",
|
|
|
|
}
|
|
|
|
case validate.IsInvalidRouteKeyError(err):
|
|
|
|
code = http.StatusBadRequest
|
2023-03-21 04:32:10 +00:00
|
|
|
resp = ErrorResponse{
|
2022-10-30 02:15:35 +00:00
|
|
|
Error: err.Error(),
|
|
|
|
}
|
|
|
|
case validate.IsFieldError(err):
|
2023-03-07 06:18:58 +00:00
|
|
|
code = http.StatusUnprocessableEntity
|
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
fieldErrors := err.(validate.FieldErrors) // nolint
|
2022-10-30 02:15:35 +00:00
|
|
|
resp.Error = "Validation Error"
|
|
|
|
resp.Fields = map[string]string{}
|
|
|
|
|
|
|
|
for _, fieldError := range fieldErrors {
|
|
|
|
resp.Fields[fieldError.Field] = fieldError.Error
|
|
|
|
}
|
|
|
|
case validate.IsRequestError(err):
|
2024-01-04 17:55:26 +00:00
|
|
|
requestError := err.(*validate.RequestError) // nolint
|
2022-10-30 02:15:35 +00:00
|
|
|
resp.Error = requestError.Error()
|
2023-03-07 06:18:58 +00:00
|
|
|
|
|
|
|
if requestError.Status == 0 {
|
|
|
|
code = http.StatusBadRequest
|
|
|
|
} else {
|
|
|
|
code = requestError.Status
|
|
|
|
}
|
2022-10-30 02:15:35 +00:00
|
|
|
case ent.IsNotFound(err):
|
|
|
|
resp.Error = "Not Found"
|
|
|
|
code = http.StatusNotFound
|
|
|
|
default:
|
|
|
|
resp.Error = "Unknown Error"
|
|
|
|
code = http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
if err := server.JSON(w, code, resp); err != nil {
|
|
|
|
log.Err(err).Msg("failed to write response")
|
2022-10-30 02:15:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|