github.com/chanxuehong/wechat@v0.0.0-20230222024006-36f0325263cd/mch/core/error.go (about)

     1  package core
     2  
     3  import (
     4  	"encoding/xml"
     5  	"errors"
     6  	"fmt"
     7  )
     8  
     9  var (
    10  	ErrNotFoundReturnCode = errors.New("not found return_code parameter")
    11  	ErrNotFoundResultCode = errors.New("not found result_code parameter")
    12  	ErrNotFoundSign       = errors.New("not found sign parameter")
    13  )
    14  
    15  var _ error = (*Error)(nil)
    16  
    17  // 协议错误, return_code 不为 SUCCESS 时有返回.
    18  type Error struct {
    19  	XMLName    struct{} `xml:"xml"                  json:"-"`
    20  	ReturnCode string   `xml:"return_code"          json:"return_code"`
    21  	ReturnMsg  string   `xml:"return_msg,omitempty" json:"return_msg,omitempty"`
    22  }
    23  
    24  func (e *Error) Error() string {
    25  	bs, err := xml.Marshal(e)
    26  	if err != nil {
    27  		return fmt.Sprintf("return_code: %q, return_msg: %q", e.ReturnCode, e.ReturnMsg)
    28  	}
    29  	return string(bs)
    30  }
    31  
    32  var _ error = (*BizError)(nil)
    33  
    34  // 业务错误, result_code 不为 SUCCESS 时有返回.
    35  type BizError struct {
    36  	XMLName     struct{} `xml:"xml"                    json:"-"`
    37  	ResultCode  string   `xml:"result_code"            json:"result_code"`
    38  	ErrCode     string   `xml:"err_code,omitempty"     json:"err_code,omitempty"`
    39  	ErrCodeDesc string   `xml:"err_code_des,omitempty" json:"err_code_des,omitempty"`
    40  }
    41  
    42  func (e *BizError) Error() string {
    43  	bs, err := xml.Marshal(e)
    44  	if err != nil {
    45  		return fmt.Sprintf("result_code: %q, err_code: %q, err_code_des: %q", e.ResultCode, e.ErrCode, e.ErrCodeDesc)
    46  	}
    47  	return string(bs)
    48  }