Error Handling
Error handling is an indispensable part of service. In normal business development, we can think that the http status code is not in the series, it can be regarded as an http request error.
It is accompanied by error messages in response, but these error messages are all returned in plain text. In addition, I will define some business errors in the business, and the common practice is to pass
The two fields code
and msg
are used to describe the business processing results, and it is hoped that the response can be made with the json response body.
Business processing is normal
-
{
"code": 10001,
"msg": "something wrong"
}
Previously, when we handled the login logic when the username did not exist, an error was directly returned. Let’s log in and pass a username that does not exist to see the effect.
curl -X POST \
http://127.0.0.1:8888/user/login \
-H 'content-type: application/json' \
-d '{
"username":"1",
"password":"123456"
}'
Next we will return it in json format
-
$ cd common
$ mkdir errorx&&cd errorx
$ vim baseerror.go
package errorx
const defaultCode = 1001
type CodeError struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
Code int `json:"code"`
Msg string `json:"msg"`
}
func NewCodeError(code int, msg string) error {
return &CodeError{Code: code, Msg: msg}
}
func NewDefaultError(msg string) error {
return NewCodeError(defaultCode, msg)
}
func (e *CodeError) Error() string {
return e.Msg
}
func (e *CodeError) Data() *CodeErrorResponse {
return &CodeErrorResponse{
Code: e.Code,
Msg: e.Msg,
}
}
Replace errors in login logic with CodeError custom errors
Use custom errors
$ vim service/user/api/user.go
func main() {
flag.Parse()
conf.MustLoad(*configFile, &c)
ctx := svc.NewServiceContext(c)
server := rest.MustNewServer(c.RestConf)
defer server.Stop()
handler.RegisterHandlers(server, ctx)
// Custom error
httpx.SetErrorHandler(func(err error) (int, interface{}) {
switch e := err.(type) {
case *errorx.CodeError:
return http.StatusOK, e.Data()
default:
return http.StatusInternalServerError, nil
}
})
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}
-
HTTP/1.1 200 OK
Content-Type: application/json
Date: Tue, 09 Feb 2021 06:47:29 GMT
Content-Length: 40