errors.go79 lines, 2.6 KB
| 1 | package httpx |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "log/slog" |
| 8 | "net/http" |
| 9 | |
| 10 | "github.com/fiston-user/payments/internal/platform/apperr" |
| 11 | ) |
| 12 | |
| 13 | // errorBody is the shape of every error response, from every service. |
| 14 | type errorBody struct { |
| 15 | Error errorDetail `json:"error"` |
| 16 | } |
| 17 | |
| 18 | type errorDetail struct { |
| 19 | Code string `json:"code"` |
| 20 | Message string `json:"message"` |
| 21 | RequestID string `json:"request_id,omitempty"` |
| 22 | Fields map[string]string `json:"fields,omitempty"` |
| 23 | } |
| 24 | |
| 25 | // classify maps an error to an HTTP status and a stable error code. It's the |
| 26 | // only place that knows how our errors become HTTP. Clients should branch on |
| 27 | // the code, never on the message. |
| 28 | func classify(err error) (int, string) { |
| 29 | var reqErr *requestError |
| 30 | var valErr *apperr.ValidationError |
| 31 | var ruleErr *apperr.RuleError |
| 32 | switch { |
| 33 | case errors.As(err, &reqErr): |
| 34 | return reqErr.status, reqErr.code |
| 35 | case errors.As(err, &valErr): |
| 36 | return http.StatusUnprocessableEntity, "invalid_input" |
| 37 | case errors.As(err, &ruleErr): |
| 38 | return http.StatusUnprocessableEntity, ruleErr.Code |
| 39 | case errors.Is(err, apperr.ErrNotFound): |
| 40 | return http.StatusNotFound, "not_found" |
| 41 | case errors.Is(err, apperr.ErrConflict): |
| 42 | return http.StatusConflict, "conflict" |
| 43 | case errors.Is(err, apperr.ErrUnauthenticated): |
| 44 | return http.StatusUnauthorized, "unauthenticated" |
| 45 | case errors.Is(err, apperr.ErrForbidden): |
| 46 | return http.StatusForbidden, "forbidden" |
| 47 | default: |
| 48 | return http.StatusInternalServerError, "internal" |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Error sends err to the client as a JSON error response. Client errors show |
| 53 | // the error's message, so wrap them with words a client can read. Anything |
| 54 | // else is our fault: it gets logged, and the client sees a generic message. |
| 55 | func Error(w http.ResponseWriter, r *http.Request, err error) { |
| 56 | status, code := classify(err) |
| 57 | d := errorDetail{Code: code, Message: err.Error()} |
| 58 | if valErr, ok := errors.AsType[*apperr.ValidationError](err); ok { |
| 59 | d.Fields = valErr.Fields |
| 60 | } |
| 61 | if status >= 500 { |
| 62 | slog.ErrorContext(r.Context(), "request failed", "err", err) |
| 63 | d.Message = "internal server error" |
| 64 | } |
| 65 | writeError(w, r, status, d) |
| 66 | } |
| 67 | |
| 68 | func writeError(w http.ResponseWriter, r *http.Request, status int, d errorDetail) { |
| 69 | d.RequestID = RequestIDFrom(r.Context()) |
| 70 | // A struct of strings always encodes, so there's no error to handle. |
| 71 | body, _ := json.Marshal(errorBody{Error: d}) |
| 72 | writeBody(w, status, body) |
| 73 | } |
| 74 | |
| 75 | // NotFound answers requests that match no route with a JSON 404, instead of |
| 76 | // net/http's plain-text one. |
| 77 | func NotFound(w http.ResponseWriter, r *http.Request) { |
| 78 | Error(w, r, fmt.Errorf("route %w", apperr.ErrNotFound)) |
| 79 | } |