Before the change

When developing an interface using GIN, the returned interface data is written like this.

type response struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data"` } // always return Http.statusok c.json (http.statusok, response{Code: 20101, Msg: "invalid ", Data: nil,})Copy the code

In this way, code and MSG are defined where to return, without unified management.

After the change

/ / for instance, Return to the "user mobile phone number is not legitimate" error in SAN Antonio SON (HTTP. StatusOK, errno. ErrUserPhone. WithID be sad etString chtistina georgina rossetti.british poetess ((" trace - id "))) / / return to SAN Antonio right SON (HTTP. StatusOK, errno.OK.WithData(data).WithID(c.GetString("trace-id")))Copy the code

Errno.ErrUserPhone and errno.OK indicate user-defined error codes. You will see the definitions below.

.withid () sets the unique ID of the current request, also known as the link ID, or can be ignored.

.withData () Data returned on success.

The following share prepared errno package source code, very simple, I hope you don’t mind.

Errno package source

// errno/errno.go package errno import ( "encoding/json" ) var _ Error = (*err)(nil) type Error interface { // i WithData(data interface{}) Error // WithID Sets the unique ID of the current request WithID(ID string) Error // ToString() String} Type Err struct {Code int 'JSON :" Code "' // Business Code Msg string 'JSON :" Msg "' // Error Description Data interface{} 'json:" Data "' // Data ID returned on success string 'json:" ID, omitEmpty "' // Unique ID of the current request, easy to locate the problem, } func NewError(code int, MSG string) Error {return &err{code: code, MSG: MSG, Data: nil, } } func (e *err) i() {} func (e *err) WithData(data interface{}) Error { e.Data = data return e } func (e *err) WithID(id string) Error {e.id = ID return e} // ToString Returns details about errors in JSON format func (e *err) ToString() string {err := &struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data"` ID string `json:"id,omitempty"` }{ Code: e.Code, Msg: e.Msg, Data: e.Data, ID: e.ID, } raw, _ := json.Marshal(err) return string(raw) }Copy the code
Go package errno var (// OK OK = NewError(0, "OK") // Service level error code ErrServer = NewError(10001, "Service error, Please contact the administrator. ") ErrParam = NewError(10002, "parameter error ") ErrSignParam = NewError(10003, ErrUserPhone = NewError(20101, "user phone number is invalid ") ErrUserCaptcha = NewError(20102, "User verification code error ") //...)Copy the code

Error code rule

  • The error code must be incode.goFile defined.
  • The error code must be greater than 0. Otherwise, the error code is correct.

The error code is 5 digits

1 01 01
Service level error code Module level error code Specific error code
  • Service level error code: 1 digits. For example, 1 indicates a system-level error. 2 is a common error, usually caused by illegal operations.
  • Module-level error code: 2 digits, for example, 01 is the user module. 02 is the order module.
  • The error code is 2-digit. For example, 01 indicates that the mobile phone number is invalid. 02 indicates that the verification code is incorrect.

The above code is in the Go-Gin-API project at github.com/xinliangnot…