reCoreD-UI/server/response.go

45 lines
841 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
)
2024-04-19 04:47:00 +00:00
// Response common http response
2024-04-03 14:37:15 +00:00
type Response struct {
2024-04-19 04:47:00 +00:00
// `true` for 2xx, else `false`
Succeed bool `json:"succeed"`
// error message
Message string `json:"message"`
// payload here
Data interface{} `json:"data"`
2024-04-03 14:37:15 +00:00
}
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
}