improved api error reporting

This commit is contained in:
Michael 2019-11-21 22:37:26 +00:00
parent d910db375a
commit 1d95dbe6b4

6
api.go
View File

@ -2,6 +2,8 @@ package request
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings" "strings"
@ -44,7 +46,8 @@ func Get(r *Request, path string) ([]byte, *HTTPError) {
if resp.StatusCode == 200 { if resp.StatusCode == 200 {
return body, nil return body, nil
} }
return nil, &HTTPError{StatusCode: resp.StatusCode, Error: nil} e := fmt.Sprintf("api: %s/%s - %d %s", r.URL, path, resp.StatusCode, http.StatusText(resp.StatusCode))
return nil, &HTTPError{StatusCode: resp.StatusCode, Error: errors.New(e)}
} }
// JSONParse parses json data // JSONParse parses json data
@ -52,6 +55,7 @@ func JSONParse(r *Request, path string) (map[string]interface{}, *HTTPError) {
var result map[string]interface{} var result map[string]interface{}
resp, err := Get(r, path) resp, err := Get(r, path)
if err != nil { if err != nil {
fmt.Println(err.Error)
return nil, err return nil, err
} }
json.Unmarshal(resp, &result) json.Unmarshal(resp, &result)