error.go 760 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package context
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. )
  7. //ErrUnmarshall err when unmarshall
  8. var ErrUnmarshall = errors.New("json Unmarshal Error")
  9. //WxError 微信返回的错误信息
  10. type WxError struct {
  11. Code int64 `json:"errcode"`
  12. Msg string `json:"errmsg"`
  13. }
  14. //NewWxError new NewWxError
  15. func NewWxError(code int64, msg string) *WxError {
  16. return &WxError{Code: code, Msg: msg}
  17. }
  18. func (e *WxError) Error() string {
  19. return e.Msg
  20. }
  21. //CheckCommonError check CommonError
  22. func CheckCommonError(jsonData []byte) error {
  23. var errmsg WxError
  24. if err := json.Unmarshal(jsonData, &errmsg); err != nil {
  25. return ErrUnmarshall
  26. }
  27. if errmsg.Code != 0 {
  28. return fmt.Errorf("Error , errcode=%d , errmsg=%s", errmsg.Code, errmsg.Msg)
  29. }
  30. return nil
  31. }