mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
enhance(wip): error handle
This commit is contained in:
parent
3f95ae5d90
commit
650196d06a
93 changed files with 5228 additions and 3738 deletions
21
api/api.go
21
api/api.go
|
@ -1,9 +1,12 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/0xJacky/Nginx-UI/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/uozi-tech/cosy"
|
||||
"github.com/uozi-tech/cosy/logger"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
|
@ -13,9 +16,21 @@ func CurrentUser(c *gin.Context) *model.User {
|
|||
|
||||
func ErrHandler(c *gin.Context, err error) {
|
||||
logger.GetLogger().Errorln(err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
var cErr *cosy.Error
|
||||
switch {
|
||||
case errors.Is(err, gorm.ErrRecordNotFound):
|
||||
c.JSON(http.StatusNotFound, &cosy.Error{
|
||||
Code: http.StatusNotFound,
|
||||
Message: gorm.ErrRecordNotFound.Error(),
|
||||
})
|
||||
case errors.As(err, &cErr):
|
||||
c.JSON(http.StatusInternalServerError, cErr)
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, &cosy.Error{
|
||||
Code: http.StatusInternalServerError,
|
||||
Message: err.Error(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func SetSSEHeaders(c *gin.Context) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue