MCPcopy
hub / github.com/silenceper/wechat / DecodeWithError

Function DecodeWithError

util/error.go:44–70  ·  view source on GitHub ↗

DecodeWithError 将返回值按照解析

(response []byte, obj interface{}, apiName string)

Source from the content-addressed store, hash-verified

42
43// DecodeWithError 将返回值按照解析
44func DecodeWithError(response []byte, obj interface{}, apiName string) error {
45 err := json.Unmarshal(response, obj)
46 if err != nil {
47 return fmt.Errorf("json Unmarshal Error, err=%v", err)
48 }
49 responseObj := reflect.ValueOf(obj)
50 if !responseObj.IsValid() {
51 return fmt.Errorf("obj is invalid")
52 }
53 commonError := responseObj.Elem().FieldByName("CommonError")
54 if !commonError.IsValid() || commonError.Kind() != reflect.Struct {
55 return fmt.Errorf("commonError is invalid or not struct")
56 }
57 errCode := commonError.FieldByName("ErrCode")
58 errMsg := commonError.FieldByName("ErrMsg")
59 if !errCode.IsValid() || !errMsg.IsValid() {
60 return fmt.Errorf("errcode or errmsg is invalid")
61 }
62 if errCode.Int() != 0 {
63 return &CommonError{
64 apiName: apiName,
65 ErrCode: errCode.Int(),
66 ErrMsg: errMsg.String(),
67 }
68 }
69 return nil
70}
71
72// HandleFileResponse 通用处理微信等接口返回:有时 JSON 错误,有时文件内容
73func HandleFileResponse(response []byte, apiName string) ([]byte, error) {

Callers 15

AccountPagingMethod · 0.92
GetCorpStatisticMethod · 0.92
GetServicerStatisticMethod · 0.92
AddKnowledgeGroupMethod · 0.92
ListKnowledgeGroupMethod · 0.92
AddKnowledgeIntentMethod · 0.92
ListKnowledgeIntentMethod · 0.92
GetCheckinDataMethod · 0.92
GetDayDataMethod · 0.92
GetMonthDataMethod · 0.92
GetCorpOptionMethod · 0.92
GetOptionMethod · 0.92

Calls 1

StringMethod · 0.45

Tested by 1

TestDecodeWithErrorFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…