Przeglądaj źródła

added API call error message retrieval (not tested)

master
Patrick Peng Sun 8 lat temu
rodzic
commit
86496a8046
2 zmienionych plików z 41 dodań i 0 usunięć
  1. +2
    -0
      todo.tasks
  2. +39
    -0
      wechatApiError.go

+ 2
- 0
todo.tasks Wyświetl plik

@@ -0,0 +1,2 @@
wechatApiError:
☐ test get JSON error message when api call happened

+ 39
- 0
wechatApiError.go Wyświetl plik

@@ -0,0 +1,39 @@
package main

import (
"encoding/json"
"net/http"
"strconv"
)

//WechatError when API call goes wrong, it shows error message
type WechatError struct {
ErrCode int64 `json:"errcode"`
ErrMsg string `json:"errmsg"`
}

//getWechatAPIError given an http body, try to decode json error
// caller needs to makesure content type is json
//return
// the error strucutre, on success
// err: if json decode encounters problem
func getWechatAPIError(respBody string) (result WechatError, err error) {
err = json.Unmarshal([]byte(respBody), &result)
return
}

//see if a http response is json response
func isJSON(r *http.Response) bool {
contentType := r.Header.Get("Content-Type")
contentLen := r.Header.Get("Content-Length")
len, _ := strconv.Atoi(contentLen)

if contentType == "text/plain" || contentType == "text/json" {
if len < 10240 { //less than 10K
return true
}
}
return false
}

//To Do: test error message @todo

Ładowanie…
Anuluj
Zapisz