| 40 | } |
| 41 | |
| 42 | func Error() gin.HandlerFunc { |
| 43 | return func(c *gin.Context) { |
| 44 | c.Next() |
| 45 | err := c.Errors.Last() |
| 46 | if err == nil { |
| 47 | return |
| 48 | } |
| 49 | |
| 50 | // Redigo error handler |
| 51 | if errors.Is(err, redigo.ErrNil) { |
| 52 | c.JSON(http.StatusNotFound, ErrorResponse{ |
| 53 | Message: http.StatusText(http.StatusNotFound), |
| 54 | }) |
| 55 | c.Abort() |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | // RPC error handler |
| 60 | var dferr *dferrors.DfError |
| 61 | if errors.As(err.Err, &dferr) { |
| 62 | switch dferr.Code { |
| 63 | case commonv1.Code_InvalidResourceType: |
| 64 | c.JSON(http.StatusBadRequest, ErrorResponse{ |
| 65 | Message: http.StatusText(http.StatusBadRequest), |
| 66 | }) |
| 67 | c.Abort() |
| 68 | return |
| 69 | default: |
| 70 | c.JSON(http.StatusInternalServerError, ErrorResponse{ |
| 71 | Message: http.StatusText(http.StatusInternalServerError), |
| 72 | }) |
| 73 | c.Abort() |
| 74 | return |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Bcrypt error handler |
| 79 | if errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) { |
| 80 | c.JSON(http.StatusUnauthorized, ErrorResponse{ |
| 81 | Message: http.StatusText(http.StatusUnauthorized), |
| 82 | }) |
| 83 | c.Abort() |
| 84 | return |
| 85 | } |
| 86 | |
| 87 | // GORM error handler |
| 88 | if errors.Is(err.Err, gorm.ErrRecordNotFound) { |
| 89 | c.JSON(http.StatusNotFound, ErrorResponse{ |
| 90 | Message: err.Error(), |
| 91 | }) |
| 92 | c.Abort() |
| 93 | return |
| 94 | } |
| 95 | |
| 96 | // Mysql error handler |
| 97 | var merr *mysql.MySQLError |
| 98 | if errors.As(err.Err, &merr) { |
| 99 | switch merr.Number { |