diff --git a/internal/photos/api/account.go b/internal/photos/api/account.go index 97b94e6..1daeaa9 100644 --- a/internal/photos/api/account.go +++ b/internal/photos/api/account.go @@ -76,9 +76,7 @@ func (s *Service) Signup(c *gin.Context) { return } - c.JSON(http.StatusOK, gin.H{ - "status": "success", - }) + c.Status(http.StatusNoContent) } func (s *Service) Login(c *gin.Context) { @@ -99,8 +97,7 @@ func (s *Service) Login(c *gin.Context) { } c.JSON(http.StatusOK, gin.H{ - "status": "success", - "token": session.Token, + "token": session.Token, }) } @@ -114,9 +111,7 @@ func (s *Service) Logout(c *gin.Context) { c.AbortWithError(http.StatusNotFound, ErrSessionNotFound) return } - c.JSON(http.StatusOK, gin.H{ - "status": "success", - }) + c.Status(http.StatusNoContent) } func (s *Service) AccountInit() { diff --git a/internal/photos/api/errors.go b/internal/photos/api/errors.go index 2522e47..f2d07b6 100644 --- a/internal/photos/api/errors.go +++ b/internal/photos/api/errors.go @@ -1,31 +1,37 @@ package photosapi import ( + "errors" "strings" "github.com/gin-gonic/gin" ) var ( - StatusSuccess = "success" - StatusFailed = "failed" + ErrReqMissingBody = errors.New("missing body") ) func (s *Service) HandleError(c *gin.Context) { c.Next() + err := c.Errors.Last() + if err == nil { + return + } - if err := c.Errors.Last(); err != nil { - if err.IsType(gin.ErrorTypeBind) { - c.JSON(-1, gin.H{ - "status": StatusFailed, - "error": "binding error", - "details": strings.Split(err.Error(), "\n"), - }) - } else { - c.JSON(-1, gin.H{ - "status": StatusFailed, - "error": err.Error(), - }) - } + details := err.Error() + if details == "EOF" { + details = "missing body" + } + + switch err.Type { + case gin.ErrorTypeBind: + c.JSON(-1, gin.H{ + "error": "binding error", + "details": strings.Split(details, "\n"), + }) + default: + c.JSON(-1, gin.H{ + "error": details, + }) } } diff --git a/internal/photos/api/file.go b/internal/photos/api/file.go index def5110..5be71d7 100644 --- a/internal/photos/api/file.go +++ b/internal/photos/api/file.go @@ -141,7 +141,6 @@ func (s *Service) FileCreate(c *gin.Context) { } c.JSON(http.StatusOK, gin.H{ - "status": "success", "sum": file.Checksum, "nbChunks": len(file.Chunks), "size": rs, @@ -163,7 +162,6 @@ func (s *Service) FileCreateChunk(c *gin.Context) { if err := chunk.Save(sess.Account.Login); err != nil { if errors.Is(err, ErrStoreChunkAlreadyExists) { c.JSON(http.StatusOK, gin.H{ - "status": "success", "checksum": chunk.Sum, }) } else { @@ -173,7 +171,6 @@ func (s *Service) FileCreateChunk(c *gin.Context) { } c.JSON(http.StatusOK, gin.H{ - "status": "success", "checksum": chunk.Sum, }) } diff --git a/internal/photos/api/me.go b/internal/photos/api/me.go index 774d8d5..4fece2f 100644 --- a/internal/photos/api/me.go +++ b/internal/photos/api/me.go @@ -8,8 +8,7 @@ import ( func (s *Service) Me(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ - "status": "success", - "user": s.CurrentSession(c).Account.Login, + "user": s.CurrentSession(c).Account.Login, }) } diff --git a/internal/photos/api/recovery.go b/internal/photos/api/recovery.go index 99f3382..f9bb596 100644 --- a/internal/photos/api/recovery.go +++ b/internal/photos/api/recovery.go @@ -18,7 +18,6 @@ func (s *Service) Recovery(c *gin.Context) { if err := recover(); err != nil { s.LogErr.Print("PANIC", err) c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{ - "status": StatusFailed, "error": ErrUnexpected.Error(), "details": err, })