Better error handling in list API

This commit is contained in:
Wim Brand
2018-01-30 10:29:45 +01:00
parent 11ce21d281
commit a28b1ff7b0
3 changed files with 58 additions and 15 deletions

View File

@@ -1,8 +1,37 @@
package pixelapi
import "net/http"
import "io/ioutil"
import "io"
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
)
type ErrorResponse struct {
ReqError bool
Success bool `json:"success"`
Value string `json:"value"`
Message *string `json:"message"`
ID *string `json:"id"`
}
func errorResponseFromJSON(j string) *ErrorResponse {
var r = &ErrorResponse{}
var err = json.Unmarshal([]byte(j), r)
if err != nil {
r.Success = false
r.ReqError = true
r.Value = err.Error()
}
return r
}
func errorResponseFromError(e error) *ErrorResponse {
var r = &ErrorResponse{}
r.Success = false
r.ReqError = true
r.Value = e.Error()
return r
}
func getString(url string) (string, error) {
req, err := http.NewRequest("GET", url, nil)