forked from chenhg5/morningo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle.go
45 lines (41 loc) · 892 Bytes
/
handle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"github.com/gin-gonic/gin"
"net/http"
"github.com/go-sql-driver/mysql"
"morningo/module/logger"
)
func handleErrors() gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
logger.Error(err)
var (
errMsg string
mysqlError *mysql.MySQLError
ok bool
)
if errMsg, ok = err.(string); ok {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"msg": "system error, " + errMsg,
})
return
} else if mysqlError, ok = err.(*mysql.MySQLError); ok {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"msg": "system error, " + mysqlError.Error(),
})
return
} else {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"msg": "system error",
})
return
}
}
}()
c.Next()
}
}