improve errors management

This commit is contained in:
celogeek 2022-05-07 15:38:44 +02:00
parent 656440c4b9
commit 99e55d4d12
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
8 changed files with 80 additions and 66 deletions

View File

@ -59,26 +59,25 @@ type SignupOrLoginRequest struct {
func (s *Service) Signup(c *gin.Context) { func (s *Service) Signup(c *gin.Context) {
var account *SignupOrLoginRequest var account *SignupOrLoginRequest
if err := c.ShouldBindJSON(&account); err != nil { if c.BindJSON(&account) != nil {
s.Error(c, http.StatusBadRequest, err)
return return
} }
if err := validator.Validate(account); err != nil { if err := validator.Validate(account); err != nil {
s.Error(c, http.StatusExpectationFailed, err) c.AbortWithError(http.StatusExpectationFailed, err)
return return
} }
var accountExists int64 var accountExists int64
if err := s.DB.Model(&Account{}).Where("login = ?", account.Login).Count(&accountExists).Error; err != nil { if err := s.DB.Model(&Account{}).Where("login = ?", account.Login).Count(&accountExists).Error; err != nil {
s.Error(c, http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
return return
} }
if accountExists > 0 { if accountExists > 0 {
s.Error(c, http.StatusConflict, ErrAccountExists) c.AbortWithError(http.StatusConflict, ErrAccountExists)
return return
} }
if err := s.DB.Create(NewAccount(account.Login, account.Password)).Error; err != nil { if err := s.DB.Create(NewAccount(account.Login, account.Password)).Error; err != nil {
s.Error(c, http.StatusConflict, err) c.AbortWithError(http.StatusConflict, err)
return return
} }
@ -90,17 +89,16 @@ func (s *Service) Signup(c *gin.Context) {
func (s *Service) Login(c *gin.Context) { func (s *Service) Login(c *gin.Context) {
var account *SignupOrLoginRequest var account *SignupOrLoginRequest
if err := c.ShouldBindJSON(&account); err != nil { if c.BindJSON(&account) != nil {
s.Error(c, http.StatusBadRequest, err)
return return
} }
session, err := NewSession(s.DB, account.Login, account.Password) session, err := NewSession(s.DB, account.Login, account.Password)
if err != nil { if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
s.Error(c, http.StatusNotFound, ErrAccountAuth) c.AbortWithError(http.StatusNotFound, ErrAccountAuth)
} else { } else {
s.Error(c, http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
} }
return return
} }
@ -114,11 +112,11 @@ func (s *Service) Login(c *gin.Context) {
func (s *Service) Logout(c *gin.Context) { func (s *Service) Logout(c *gin.Context) {
res := s.DB.Where("token = ?", c.GetString("token")).Delete(&Session{}) res := s.DB.Where("token = ?", c.GetString("token")).Delete(&Session{})
if res.Error != nil { if res.Error != nil {
s.Error(c, http.StatusInternalServerError, res.Error) c.AbortWithError(http.StatusInternalServerError, res.Error)
return return
} }
if res.RowsAffected == 0 { if res.RowsAffected == 0 {
s.Error(c, http.StatusNotFound, ErrSessionNotFound) c.AbortWithError(http.StatusNotFound, ErrSessionNotFound)
return return
} }
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{

View File

@ -13,7 +13,7 @@ var (
func (s *Service) RequireBody(c *gin.Context) { func (s *Service) RequireBody(c *gin.Context) {
if c.Request.Method == "POST" && c.Request.ContentLength == 0 { if c.Request.Method == "POST" && c.Request.ContentLength == 0 {
s.Error(c, http.StatusBadRequest, ErrReqMissingBody) c.AbortWithError(http.StatusBadRequest, ErrReqMissingBody)
return return
} }
} }

View File

@ -4,16 +4,18 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func (s *Service) Error(c *gin.Context, code int, err error) { var (
var status string StatusSuccess = "success"
if code >= 200 && code < 400 { StatusFailed = "failed"
status = "success" )
} else {
status = "failed"
}
c.AbortWithStatusJSON(code, gin.H{ func (s *Service) HandleError(c *gin.Context) {
"status": status, c.Next()
"error": err.Error(),
if err := c.Errors.Last(); err != nil {
c.JSON(-1, gin.H{
"status": StatusFailed,
"error": err.Err.Error(),
}) })
} }
}

View File

@ -63,28 +63,27 @@ type FileRequest struct {
func (s *Service) FileCreate(c *gin.Context) { func (s *Service) FileCreate(c *gin.Context) {
file := &FileRequest{} file := &FileRequest{}
if err := c.ShouldBindJSON(file); err != nil { if c.BindJSON(file) != nil {
s.Error(c, http.StatusInternalServerError, err)
return return
} }
if len(file.Name) < 1 { if len(file.Name) < 1 {
s.Error(c, http.StatusBadRequest, ErrStoreMissingName) c.AbortWithError(http.StatusBadRequest, ErrStoreMissingName)
return return
} }
if len(file.Checksum) != 40 { if len(file.Checksum) != 40 {
s.Error(c, http.StatusBadRequest, ErrStoreBadChecksum) c.AbortWithError(http.StatusBadRequest, ErrStoreBadChecksum)
return return
} }
if len(file.Chunks) == 0 { if len(file.Chunks) == 0 {
s.Error(c, http.StatusBadRequest, ErrStoreMissingChunks) c.AbortWithError(http.StatusBadRequest, ErrStoreMissingChunks)
return return
} }
for _, chunk := range file.Chunks { for _, chunk := range file.Chunks {
if len(chunk) != 40 { if len(chunk) != 40 {
s.Error(c, http.StatusBadRequest, ErrStoreBadChecksum) c.AbortWithError(http.StatusBadRequest, ErrStoreBadChecksum)
return return
} }
} }
@ -92,15 +91,15 @@ func (s *Service) FileCreate(c *gin.Context) {
r, rs, err := s.Store.Combine(file.Chunks) r, rs, err := s.Store.Combine(file.Chunks)
if err != nil { if err != nil {
if strings.HasPrefix(err.Error(), "chunk") && strings.HasSuffix(err.Error(), "doesn't exists") { if strings.HasPrefix(err.Error(), "chunk") && strings.HasSuffix(err.Error(), "doesn't exists") {
s.Error(c, http.StatusBadRequest, err) c.AbortWithError(http.StatusBadRequest, err)
} else { } else {
s.Error(c, http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
} }
return return
} }
if r != file.Checksum { if r != file.Checksum {
s.Error(c, http.StatusExpectationFailed, ErrStoreMismatchChecksum) c.AbortWithError(http.StatusExpectationFailed, ErrStoreMismatchChecksum)
return return
} }
@ -137,7 +136,7 @@ func (s *Service) FileCreate(c *gin.Context) {
} }
if err != nil { if err != nil {
s.Error(c, http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
return return
} }
@ -151,7 +150,7 @@ func (s *Service) FileCreate(c *gin.Context) {
func (s *Service) FileCreateChunk(c *gin.Context) { func (s *Service) FileCreateChunk(c *gin.Context) {
if c.Request.ContentLength > CHUNK_SIZE { if c.Request.ContentLength > CHUNK_SIZE {
s.Error(c, http.StatusBadRequest, ErrStoreBadChunkSize) c.AbortWithError(http.StatusBadRequest, ErrStoreBadChunkSize)
return return
} }
@ -169,7 +168,7 @@ func (s *Service) FileCreateChunk(c *gin.Context) {
"checksum": chunk.Sum, "checksum": chunk.Sum,
}) })
} else { } else {
s.Error(c, http.StatusBadRequest, err) c.AbortWithError(http.StatusBadRequest, err)
} }
return return
} }
@ -183,7 +182,7 @@ func (s *Service) FileCreateChunk(c *gin.Context) {
func (s *Service) FileChunkExists(c *gin.Context) { func (s *Service) FileChunkExists(c *gin.Context) {
checksum := c.Param("checksum") checksum := c.Param("checksum")
if len(checksum) != 40 { if len(checksum) != 40 {
s.Error(c, http.StatusBadRequest, ErrStoreBadChecksum) c.AbortWithError(http.StatusBadRequest, ErrStoreBadChecksum)
return return
} }
@ -197,13 +196,13 @@ func (s *Service) FileChunkExists(c *gin.Context) {
func (s *Service) FileExists(c *gin.Context) { func (s *Service) FileExists(c *gin.Context) {
checksum := c.Param("checksum") checksum := c.Param("checksum")
if len(checksum) != 40 { if len(checksum) != 40 {
s.Error(c, http.StatusBadRequest, ErrStoreBadChecksum) c.AbortWithError(http.StatusBadRequest, ErrStoreBadChecksum)
return return
} }
var fileExists int64 var fileExists int64
if err := s.DB.Model(&File{}).Where("checksum = ?", checksum).Count(&fileExists).Error; err != nil { if err := s.DB.Model(&File{}).Where("checksum = ?", checksum).Count(&fileExists).Error; err != nil {
s.Error(c, http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
return return
} }
@ -217,7 +216,7 @@ func (s *Service) FileExists(c *gin.Context) {
func (s *Service) FileGet(c *gin.Context) { func (s *Service) FileGet(c *gin.Context) {
checksum := c.Param("checksum") checksum := c.Param("checksum")
if len(checksum) != 40 { if len(checksum) != 40 {
s.Error(c, http.StatusBadRequest, ErrStoreBadChecksum) c.AbortWithError(http.StatusBadRequest, ErrStoreBadChecksum)
return return
} }
if checksum == c.GetHeader("If-None-Match") { if checksum == c.GetHeader("If-None-Match") {
@ -227,7 +226,7 @@ func (s *Service) FileGet(c *gin.Context) {
f := &File{} f := &File{}
if err := s.DB.Debug().Preload("Chunks").Where("checksum = ?", checksum).First(&f).Error; err != nil { if err := s.DB.Debug().Preload("Chunks").Where("checksum = ?", checksum).First(&f).Error; err != nil {
s.Error(c, http.StatusBadRequest, err) c.AbortWithError(http.StatusBadRequest, err)
return return
} }
@ -238,7 +237,7 @@ func (s *Service) FileGet(c *gin.Context) {
reader, err := s.Store.NewStoreReader(chunks) reader, err := s.Store.NewStoreReader(chunks)
if err != nil { if err != nil {
s.Error(c, http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
return return
} }
defer reader.Close() defer reader.Close()
@ -258,13 +257,13 @@ func (s *Service) FileGet(c *gin.Context) {
func (s *Service) FileAnalyze(c *gin.Context) { func (s *Service) FileAnalyze(c *gin.Context) {
checksum := c.Param("checksum") checksum := c.Param("checksum")
if len(checksum) != 40 { if len(checksum) != 40 {
s.Error(c, http.StatusBadRequest, ErrStoreBadChecksum) c.AbortWithError(http.StatusBadRequest, ErrStoreBadChecksum)
return return
} }
f := &File{} f := &File{}
if err := s.DB.Debug().Preload("Chunks").Where("checksum = ?", checksum).First(&f).Error; err != nil { if err := s.DB.Debug().Preload("Chunks").Where("checksum = ?", checksum).First(&f).Error; err != nil {
s.Error(c, http.StatusBadRequest, err) c.AbortWithError(http.StatusBadRequest, err)
return return
} }
@ -275,19 +274,19 @@ func (s *Service) FileAnalyze(c *gin.Context) {
reader, err := s.Store.NewStoreReader(chunks) reader, err := s.Store.NewStoreReader(chunks)
if err != nil { if err != nil {
s.Error(c, http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
return return
} }
defer reader.Close() defer reader.Close()
rawExif, err := exif.SearchAndExtractExifWithReader(reader) rawExif, err := exif.SearchAndExtractExifWithReader(reader)
if err != nil { if err != nil {
s.Error(c, http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
return return
} }
entries, _, err := exif.GetFlatExifDataUniversalSearch(rawExif, nil, true) entries, _, err := exif.GetFlatExifDataUniversalSearch(rawExif, nil, true)
if err != nil { if err != nil {
s.Error(c, http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
return return
} }

View File

@ -50,6 +50,7 @@ func (s *Service) SetupRoutes() {
s.Gin.Use( s.Gin.Use(
gin.Logger(), gin.Logger(),
s.Recovery, s.Recovery,
s.HandleError,
s.RequireBody, s.RequireBody,
) )
@ -59,7 +60,7 @@ func (s *Service) SetupRoutes() {
s.UploadInit() s.UploadInit()
s.Gin.NoRoute(func(c *gin.Context) { s.Gin.NoRoute(func(c *gin.Context) {
s.Error(c, http.StatusNotFound, ErrReqNotFound) c.AbortWithError(http.StatusNotFound, ErrReqNotFound)
}) })
} }

View File

@ -17,7 +17,11 @@ func (s *Service) Recovery(c *gin.Context) {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
s.LogErr.Print("PANIC", err) s.LogErr.Print("PANIC", err)
s.Error(c, http.StatusInternalServerError, ErrUnexpected) c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"status": StatusFailed,
"error": ErrUnexpected.Error(),
"details": err,
})
} }
}() }()
c.Next() c.Next()

View File

@ -61,14 +61,14 @@ func (s *Service) RequireAuthToken(c *gin.Context) {
if tokenAuth != "" { if tokenAuth != "" {
if !strings.HasPrefix(tokenAuth, "Private ") { if !strings.HasPrefix(tokenAuth, "Private ") {
s.Error(c, http.StatusForbidden, ErrTokenMissing) c.AbortWithError(http.StatusForbidden, ErrTokenMissing)
} else { } else {
c.Set("token", tokenAuth[8:]) c.Set("token", tokenAuth[8:])
} }
} else if tokenCookie != "" { } else if tokenCookie != "" {
c.Set("token", tokenCookie) c.Set("token", tokenCookie)
} else { } else {
s.Error(c, http.StatusForbidden, ErrTokenMissing) c.AbortWithError(http.StatusForbidden, ErrTokenMissing)
} }
} }
@ -81,14 +81,14 @@ func (s *Service) RequireSession(c *gin.Context) {
sess := &Session{} sess := &Session{}
if err := s.DB.Preload("Account").Where("token = ?", c.GetString("token")).First(sess).Error; err != nil { if err := s.DB.Preload("Account").Where("token = ?", c.GetString("token")).First(sess).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
s.Error(c, http.StatusForbidden, ErrSessionNotFound) c.AbortWithError(http.StatusForbidden, ErrSessionNotFound)
} else { } else {
s.Error(c, http.StatusForbidden, err) c.AbortWithError(http.StatusForbidden, err)
} }
return return
} }
if sess.Account == nil { if sess.Account == nil {
s.Error(c, http.StatusInternalServerError, ErrSessionInvalid) c.AbortWithError(http.StatusInternalServerError, ErrSessionInvalid)
return return
} }
s.DB.Select("updated_at").Save(sess) s.DB.Select("updated_at").Save(sess)

View File

@ -13,15 +13,19 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
) )
var (
ErrUploadNotExists = errors.New("upload id doesn't exists")
)
func (s *Service) UploadCreate(c *gin.Context) { func (s *Service) UploadCreate(c *gin.Context) {
sha, err := uuid.NewRandom() sha, err := uuid.NewRandom()
if err != nil { if err != nil {
s.Error(c, http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
return return
} }
if err := s.Storage.Create(StorageTmp, sha.String()); err != nil { if err := s.Storage.Create(StorageTmp, sha.String()); err != nil {
s.Error(c, http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
return return
} }
@ -35,16 +39,23 @@ type UploadPartRequest struct {
UploadId string `uri:"upload_id" binding:"required,uuid"` UploadId string `uri:"upload_id" binding:"required,uuid"`
Part uint `uri:"part" binding:"required"` Part uint `uri:"part" binding:"required"`
} }
type UploadPartHeaders struct {
PartSha256 string `header:"x-part-sha256" binding:"required,lowercase,alphanum,len=64"`
}
func (s *Service) UploadPart(c *gin.Context) { func (s *Service) UploadPart(c *gin.Context) {
var uploadPart UploadPartRequest var uploadPart UploadPartRequest
if err := c.ShouldBindUri(&uploadPart); err != nil { if c.BindUri(&uploadPart) != nil {
s.Error(c, http.StatusBadRequest, err) return
}
var uploadPartHeaders UploadPartHeaders
if c.BindHeader(&uploadPartHeaders) != nil {
return return
} }
if !s.Storage.Exists(StorageTmp, uploadPart.UploadId) { if !s.Storage.Exists(StorageTmp, uploadPart.UploadId) {
s.Error(c, http.StatusNotFound, errors.New("upload id doesn't exists")) c.AbortWithError(http.StatusNotFound, ErrUploadNotExists)
return return
} }
@ -52,7 +63,7 @@ func (s *Service) UploadPart(c *gin.Context) {
s.Storage.Join(StorageTmp, uploadPart.UploadId, fmt.Sprint(uploadPart.Part)), s.Storage.Join(StorageTmp, uploadPart.UploadId, fmt.Sprint(uploadPart.Part)),
) )
if err != nil { if err != nil {
s.Error(c, http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
return return
} }
@ -63,7 +74,7 @@ func (s *Service) UploadPart(c *gin.Context) {
t := io.TeeReader(c.Request.Body, sha) t := io.TeeReader(c.Request.Body, sha)
w, err := io.Copy(f, t) w, err := io.Copy(f, t)
if err != nil { if err != nil {
s.Error(c, http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
return return
} }
@ -80,7 +91,7 @@ func (s *Service) UploadCancel(c *gin.Context) {
upload_id := c.Param("upload_id") upload_id := c.Param("upload_id")
if err := s.Storage.Delete(StorageTmp, upload_id); err != nil { if err := s.Storage.Delete(StorageTmp, upload_id); err != nil {
s.Error(c, http.StatusNotFound, errors.New("upload id doesn't exists")) c.AbortWithError(http.StatusNotFound, ErrUploadNotExists)
return return
} }
@ -90,19 +101,18 @@ func (s *Service) UploadCancel(c *gin.Context) {
} }
type UploadCompleteRequest struct { type UploadCompleteRequest struct {
SHA256 string `json:"sha256" binding:"required,lowercase,alphanum,len=64"` Sha256 string `json:"sha256" binding:"required,lowercase,alphanum,len=64"`
Name string `json:"name" binding:"required"` Name string `json:"name" binding:"required"`
Parts uint `json:"parts" binding:"required"` Parts uint `json:"parts" binding:"required"`
} }
func (s *Service) UploadComplete(c *gin.Context) { func (s *Service) UploadComplete(c *gin.Context) {
var uploadCompleteRequest UploadCompleteRequest var uploadCompleteRequest UploadCompleteRequest
if err := c.ShouldBindJSON(&uploadCompleteRequest); err != nil { if c.BindJSON(&uploadCompleteRequest) != nil {
s.Error(c, http.StatusBadRequest, err)
return return
} }
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"sha256": uploadCompleteRequest.SHA256, "sha256": uploadCompleteRequest.Sha256,
"parts": uploadCompleteRequest.Parts, "parts": uploadCompleteRequest.Parts,
"name": uploadCompleteRequest.Name, "name": uploadCompleteRequest.Name,
}) })