onasty/internal/transport/http/apiv1/response.go (view raw)
Olexandr Smirnov
Olexandr Smirnov
olexsmir@gmail.com refactor(api): split `usersrv` responsibilities (#195)..., 9 months ago
olexsmir@gmail.com refactor(api): split `usersrv` responsibilities (#195)..., 9 months ago
| 1 | package apiv1 |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "log/slog" |
| 6 | "net/http" |
| 7 | |
| 8 | "github.com/gin-gonic/gin" |
| 9 | "github.com/olexsmir/onasty/internal/jwtutil" |
| 10 | "github.com/olexsmir/onasty/internal/models" |
| 11 | "github.com/olexsmir/onasty/internal/service/authsrv" |
| 12 | "github.com/olexsmir/onasty/internal/service/notesrv" |
| 13 | ) |
| 14 | |
| 15 | var ErrUnauthorized = errors.New("unauthorized") |
| 16 | |
| 17 | type response struct { |
| 18 | Message string `json:"message"` |
| 19 | } |
| 20 | |
| 21 | func errorResponse(c *gin.Context, err error) { |
| 22 | if errors.Is(err, authsrv.ErrProviderNotSupported) || |
| 23 | errors.Is(err, models.ErrResetPasswordTokenAlreadyUsed) || |
| 24 | errors.Is(err, models.ErrResetPasswordTokenExpired) || |
| 25 | errors.Is(err, models.ErrUserEmailIsAlreadyInUse) || |
| 26 | errors.Is(err, models.ErrUserIsAlreadyVerified) || |
| 27 | errors.Is(err, models.ErrUserIsNotActivated) || |
| 28 | errors.Is(err, models.ErrUserInvalidEmail) || |
| 29 | errors.Is(err, models.ErrUserInvalidPassword) || |
| 30 | errors.Is(err, models.ErrUserNotFound) || |
| 31 | errors.Is(err, models.ErrUserWrongCredentials) || |
| 32 | // notes |
| 33 | errors.Is(err, notesrv.ErrNotePasswordNotProvided) || |
| 34 | errors.Is(err, models.ErrNoteContentIsEmpty) || |
| 35 | errors.Is(err, models.ErrNoteSlugIsAlreadyInUse) || |
| 36 | errors.Is(err, models.ErrNoteSlugIsInvalid) { |
| 37 | newError(c, http.StatusBadRequest, err.Error()) |
| 38 | return |
| 39 | } |
| 40 | |
| 41 | if errors.Is(err, models.ErrNoteExpired) { |
| 42 | newError(c, http.StatusGone, err.Error()) |
| 43 | return |
| 44 | } |
| 45 | |
| 46 | if errors.Is(err, models.ErrNoteNotFound) || |
| 47 | errors.Is(err, models.ErrVerificationTokenNotFound) { |
| 48 | newErrorStatus(c, http.StatusNotFound, err.Error()) |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | if errors.Is(err, ErrUnauthorized) || |
| 53 | errors.Is(err, jwtutil.ErrTokenExpired) || |
| 54 | errors.Is(err, jwtutil.ErrTokenSignatureInvalid) { |
| 55 | newErrorStatus(c, http.StatusUnauthorized, err.Error()) |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | newInternalError(c, err) |
| 60 | } |
| 61 | |
| 62 | func newError(c *gin.Context, status int, msg string) { |
| 63 | slog.ErrorContext(c.Request.Context(), msg, "status", status) |
| 64 | c.AbortWithStatusJSON(status, response{msg}) |
| 65 | } |
| 66 | |
| 67 | func newErrorStatus(c *gin.Context, status int, msg string) { |
| 68 | slog.ErrorContext(c.Request.Context(), msg, "status", status) |
| 69 | c.AbortWithStatus(status) |
| 70 | } |
| 71 | |
| 72 | func newInternalError(c *gin.Context, err error, msg ...string) { |
| 73 | slog.ErrorContext(c.Request.Context(), err.Error(), "status", "internal error") |
| 74 | |
| 75 | if len(msg) != 0 { |
| 76 | c.AbortWithStatusJSON(http.StatusInternalServerError, response{ |
| 77 | Message: msg[0], |
| 78 | }) |
| 79 | return |
| 80 | } |
| 81 | |
| 82 | c.AbortWithStatusJSON(http.StatusInternalServerError, response{ |
| 83 | Message: "internal error", |
| 84 | }) |
| 85 | } |