forked from Qihoo360/wayne
/
resulthandler.go
120 lines (102 loc) · 3.46 KB
/
resulthandler.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package base
import (
"encoding/json"
"fmt"
"net/http"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
"github.com/go-sql-driver/mysql"
"k8s.io/apimachinery/pkg/api/errors"
erroresult "github.com/Qihoo360/wayne/src/backend/models/response/errors"
"github.com/Qihoo360/wayne/src/backend/util/hack"
"github.com/Qihoo360/wayne/src/backend/util/logs"
)
type ResultHandlerController struct {
beego.Controller
}
type Result struct {
Data interface{} `json:"data"`
}
func (c *ResultHandlerController) Success(data interface{}) {
c.Ctx.Output.SetStatus(http.StatusOK)
c.Data["json"] = Result{Data: data}
c.ServeJSON()
}
// Abort stops controller handler and show the error data, e.g. Prepare
func (c *ResultHandlerController) AbortForbidden(msg string) {
logs.Info("Abort Forbidden error. %s", msg)
c.CustomAbort(http.StatusForbidden, hack.String(c.errorResult(http.StatusForbidden, msg)))
}
func (c *ResultHandlerController) AbortInternalServerError(msg string) {
logs.Error("Abort InternalServerError error. %s", msg)
c.CustomAbort(http.StatusInternalServerError, hack.String(c.errorResult(http.StatusInternalServerError, msg)))
}
func (c *ResultHandlerController) AbortBadRequest(msg string) {
logs.Info("Abort BadRequest error. %s", msg)
c.CustomAbort(http.StatusBadRequest, hack.String(c.errorResult(http.StatusBadRequest, msg)))
}
// format BadRequest with param name.
func (c *ResultHandlerController) AbortBadRequestFormat(paramName string) {
msg := fmt.Sprintf("Invalid param %s !", paramName)
c.AbortBadRequest(msg)
}
func (c *ResultHandlerController) AbortUnauthorized(msg string) {
logs.Info("Abort Unauthorized error. %s", msg)
c.CustomAbort(http.StatusUnauthorized, hack.String(c.errorResult(http.StatusUnauthorized, msg)))
}
// Handle return http code and body normally, need return
func (c *ResultHandlerController) HandleError(err error) int {
errorResult := &erroresult.ErrorResult{
Code: http.StatusInternalServerError,
}
switch e := err.(type) {
// deal with kubernetes errors
case errors.APIStatus:
errorResult.Code = int(e.Status().Code)
errorResult.SubCode = errorResult.Code
errorResult.Msg = e.Status().Message
case *mysql.MySQLError:
errorResult.Code = http.StatusBadRequest
errorResult.SubCode = int(e.Number)
// MySQL error Duplicate entry;
// refer https://dev.mysql.com/doc/refman/5.6/en/error-messages-server.html
if e.Number == 1062 {
errorResult.Msg = "Resources already exist! "
} else {
errorResult.Msg = e.Message
}
case *erroresult.ErrorResult:
errorResult = e
default:
if err == orm.ErrNoRows {
errorResult.Code = http.StatusNotFound
}
errorResult.SubCode = errorResult.Code
errorResult.Msg = err.Error()
}
if errorResult.Code >= http.StatusInternalServerError {
logs.Error("System error. %v", err)
} else {
logs.Info("Info error. %v", err)
}
c.Ctx.Output.SetStatus(errorResult.Code)
body, err := json.Marshal(errorResult)
if err != nil {
logs.Error("Json Marshal error. %v", err)
c.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
c.Ctx.Output.Body(body)
return errorResult.Code
}
func (c *ResultHandlerController) errorResult(code int, msg string) []byte {
errorResult := erroresult.ErrorResult{
Code: code,
Msg: msg,
}
body, err := json.Marshal(errorResult)
if err != nil {
logs.Error("Json Marshal error. %v", err)
c.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
return body
}