reCoreD-UI/server/response.go

33 lines
594 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"
"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 08:28:18 +00:00
if errors.Is(err, gorm.ErrRecordNotFound) {
c.JSON(http.StatusNotFound, Response{
Succeed: false,
Message: err.Error(),
})
} else {
c.JSON(http.StatusInternalServerError, Response{
Succeed: false,
Message: err.Error(),
Data: nil,
})
}
2024-04-03 14:37:15 +00:00
}