DecodeWithError 将返回值按照解析
(response []byte, obj interface{}, apiName string)
| 42 | |
| 43 | // DecodeWithError 将返回值按照解析 |
| 44 | func 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 错误,有时文件内容 |
| 73 | func HandleFileResponse(response []byte, apiName string) ([]byte, error) { |
searching dependent graphs…