当前位置:网站首页>Go - unified definition API error code

Go - unified definition API error code

2021-01-23 16:57:58 Xinliang

Before the reform

In the use of gin When developing interfaces , The return 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:  " The user's mobile phone number is illegal ",
    Data: nil,
})

This kind of writing codemsg Where are all the definitions that need to be returned , There is no unified management .

After the reform

//  such as , return “ The user's mobile phone number is illegal ” error 
c.JSON(http.StatusOK, errno.ErrUserPhone.WithID(c.GetString("trace-id")))

//  Correct return 
c.JSON(http.StatusOK, errno.OK.WithData(data).WithID(c.GetString("trace-id")))

errno.ErrUserPhoneerrno.OK Represents a custom error code , You'll see the definition below .

.WithID() Set the unique name of the current request ID, It can also be understood as a link ID, It's OK to ignore .

.WithData() Set the data returned when successful .

Let's share the following errno Package source , It's 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  To avoid being implemented by other packages 
    i()
    // WithData  Set the data returned when successful 
    WithData(data interface{}) Error
    // WithID  Set the unique name of the current request ID
    WithID(id string) Error
    // ToString  return  JSON  Format error details 
    ToString() string
}

type err struct {
    Code int         `json:"code"`         //  Business coding 
    Msg  string      `json:"msg"`          //  Error description 
    Data interface{} `json:"data"`         //  Data returned on success 
    ID   string      `json:"id,omitempty"` //  The only one currently requested ID, It's easy to locate the problem , It's OK to ignore 
}

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  return  JSON  Format error details 
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)
}
// errno/code.go

package errno

var (
    // OK
    OK = NewError(0, "OK")

    //  Service level error code 
    ErrServer    = NewError(10001, " Service exception , Please contact the Administrator ")
    ErrParam     = NewError(10002, " Wrong parameter ")
    ErrSignParam = NewError(10003, " Wrong signature parameter ")

    //  Module level error code  -  User module 
    ErrUserPhone   = NewError(20101, " The user's mobile phone number is illegal ")
    ErrUserCaptcha = NewError(20102, " The user verification code is wrong ")

    // ...
)

Error code rule

  • Error code should be in code.go The document defines .
  • The error code should be > 0 Number of numbers , On the contrary, it means right .

The error code is 5 digit

1 01 01
Service level error code Module level error code Specific error code
  • Service level error code :1 Number of digits , such as 1 Is a system level error ;2 For common mistakes , It is usually caused by illegal operation of users .
  • Module level error code :2 Number of digits , such as 01 For the user module ;02 For the order module .
  • Specific error code :2 Number of digits , such as 01 The mobile phone number is illegal ;02 Input error for captcha .

The above code is go-gin-api In the project , Address :https://github.com/xinliangno...

版权声明
本文为[Xinliang]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/01/20210123165713218c.html