reCoreD-UI/server/response.go

39 lines
753 B
Go
Raw Normal View History

2024-04-03 14:37:15 +00:00
package server
import (
2024-04-09 08:28:18 +00:00
"errors"
2024-04-03 14:37:15 +00:00
"net/http"
2024-04-09 13:53:12 +00:00
"reCoreD-UI/models"
2024-04-03 14:37:15 +00:00
"github.com/gin-gonic/gin"
2024-04-07 02:08:02 +00:00
"github.com/sirupsen/logrus"
2024-04-09 08:28:18 +00:00
"gorm.io/gorm"
2024-04-03 14:37:15 +00:00
)
type Response struct {
Succeed bool `json:"succeed"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
func errorHandler(c *gin.Context, err error) {
2024-04-07 02:08:02 +00:00
logrus.Error(err)
2024-04-09 13:53:12 +00:00
switch {
case errors.Is(err, gorm.ErrRecordNotFound):
2024-04-09 08:28:18 +00:00
c.JSON(http.StatusNotFound, Response{
Succeed: false,
Message: err.Error(),
})
2024-04-09 13:53:12 +00:00
case errors.Is(err, models.ErrorZoneNotEndWithDot):
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
Message: err.Error(),
})
default:
2024-04-09 08:28:18 +00:00
c.JSON(http.StatusInternalServerError, Response{
Succeed: false,
Message: err.Error(),
})
}
2024-04-03 14:37:15 +00:00
}