mirror of
https://github.com/retailcrm/api-client-go.git
synced 2025-04-03 13:13:37 +03:00
change struct for errors
This commit is contained in:
parent
3f2e573b11
commit
65dcf5ab13
4 changed files with 29 additions and 61 deletions
26
v5/client.go
26
v5/client.go
|
@ -5,7 +5,6 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/retailcrm/api-client-go/errs"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
@ -83,7 +82,7 @@ func (c *Client) PostRequest(uri string, postData interface{}, contType ...strin
|
|||
)
|
||||
|
||||
prefix := "/api/v5"
|
||||
failure := &errs.Failure{}
|
||||
apiError := &ApiError{}
|
||||
|
||||
if len(contType) > 0 {
|
||||
contentType = contType[0]
|
||||
|
@ -98,15 +97,14 @@ func (c *Client) PostRequest(uri string, postData interface{}, contType ...strin
|
|||
if i, ok := postData.(io.Reader); ok {
|
||||
reader = i
|
||||
} else {
|
||||
failure.SetRuntimeError(errors.New("postData should be url.Values or implement io.Reader"))
|
||||
return []byte{}, 0, failure
|
||||
err := errors.New("postData should be url.Values or implement io.Reader")
|
||||
return []byte{}, 0, err
|
||||
}
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s%s", c.URL, prefix, uri), reader)
|
||||
if err != nil {
|
||||
failure.SetRuntimeError(err)
|
||||
return res, 0, failure
|
||||
return res, 0, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", contentType)
|
||||
|
@ -118,26 +116,24 @@ func (c *Client) PostRequest(uri string, postData interface{}, contType ...strin
|
|||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
failure.SetRuntimeError(err)
|
||||
return res, 0, failure
|
||||
return res, 0, err
|
||||
}
|
||||
|
||||
if resp.StatusCode >= http.StatusInternalServerError {
|
||||
failure.SetApiError(fmt.Sprintf("HTTP request error. Status code: %d.\n", resp.StatusCode))
|
||||
return res, resp.StatusCode, failure
|
||||
fmt.Println(resp)
|
||||
if resp.StatusCode >= http.StatusBadRequest {
|
||||
apiError.ErrorMsg = fmt.Sprintf("HTTP request error. Status code: %d.\n", resp.StatusCode)
|
||||
return res, resp.StatusCode, apiError
|
||||
}
|
||||
|
||||
res, err = buildRawResponse(resp)
|
||||
if err != nil {
|
||||
failure.SetRuntimeError(err)
|
||||
return res, 0, failure
|
||||
return res, 0, err
|
||||
}
|
||||
|
||||
if c.Debug {
|
||||
log.Printf("API Response: %s", res)
|
||||
}
|
||||
|
||||
return res, resp.StatusCode, failure
|
||||
return res, resp.StatusCode, nil
|
||||
}
|
||||
|
||||
func buildRawResponse(resp *http.Response) ([]byte, error) {
|
||||
|
|
17
v5/error.go
17
v5/error.go
|
@ -1,31 +1,32 @@
|
|||
package v5
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// ApiErrorsList struct
|
||||
type ApiErrorsList struct {
|
||||
Success bool `json:"success"`
|
||||
ErrorsMsg string `json:"errorMsg,omitempty"`
|
||||
Errors interface{} `json:"errors,omitempty"`
|
||||
}
|
||||
type ApiErrorsList map[string]string
|
||||
|
||||
// ApiError struct
|
||||
type ApiError struct {
|
||||
SuccessfulResponse
|
||||
ErrorMsg string
|
||||
Errors map[string]string
|
||||
ErrorMsg string `json:"errorMsg,omitempty"`
|
||||
Errors ApiErrorsList `json:"errors,omitempty"`
|
||||
}
|
||||
|
||||
func (e *ApiError) Error() string {
|
||||
return e.ErrorMsg
|
||||
}
|
||||
|
||||
func (e *ApiError) GetApiErrors() map[string]string {
|
||||
return e.Errors
|
||||
}
|
||||
|
||||
func NewApiError (dataResponse []byte) error {
|
||||
a := &ApiError{}
|
||||
|
||||
if err := a.UnmarshalJSON(dataResponse); err != nil {
|
||||
if err := json.Unmarshal(dataResponse, a); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -1,50 +1,29 @@
|
|||
package v5
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFailure_ApiErrorsSlice(t *testing.T) {
|
||||
var err = Failure{}
|
||||
b := []byte(`{"success": false, "errorMsg": "Failed to activate module", "errors": ["Your account has insufficient funds to activate integration module"]}`)
|
||||
expected := map[string]string{
|
||||
"0": "Your account has insufficient funds to activate integration module",
|
||||
}
|
||||
|
||||
resp, e := ErrorResponse(b)
|
||||
err.SetRuntimeError(e)
|
||||
err.SetApiError(resp.ErrorMsg)
|
||||
|
||||
if resp.Errors != nil {
|
||||
err.SetApiErrors(ErrorsHandler(resp.Errors))
|
||||
}
|
||||
|
||||
eq := reflect.DeepEqual(expected, err.ApiErrors())
|
||||
|
||||
if eq != true {
|
||||
e := NewApiError(b)
|
||||
if eq := e.(*ApiError).Errors["0"] == expected["0"]; eq != true {
|
||||
t.Errorf("%+v", eq)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFailure_ApiErrorsMap(t *testing.T) {
|
||||
var err = Failure{}
|
||||
b := []byte(`{"success": false, "errorMsg": "Failed to activate module", "errors": {"id": "ID must be an integer"}}`)
|
||||
expected := map[string]string{
|
||||
"id": "ID must be an integer",
|
||||
}
|
||||
|
||||
resp, e := ErrorResponse(b)
|
||||
err.SetRuntimeError(e)
|
||||
err.SetApiError(resp.ErrorMsg)
|
||||
|
||||
if resp.Errors != nil {
|
||||
err.SetApiErrors(ErrorsHandler(resp.Errors))
|
||||
}
|
||||
|
||||
eq := reflect.DeepEqual(expected, err.ApiErrors())
|
||||
|
||||
if eq != true {
|
||||
e := NewApiError(b)
|
||||
if eq := expected["id"] == e.(*ApiError).Errors["id"]; eq != true {
|
||||
t.Errorf("%+v", eq)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,19 +8,11 @@ func (t Tag) MarshalJSON() ([]byte, error) {
|
|||
return json.Marshal(t.Name)
|
||||
}
|
||||
|
||||
func (a *ApiError) UnmarshalJSON(data []byte) error {
|
||||
var e ApiErrorsList
|
||||
|
||||
if err := json.Unmarshal(data, &e); err != nil {
|
||||
func (a *ApiErrorsList) UnmarshalJSON (data []byte) error {
|
||||
var i interface {}
|
||||
if err := json.Unmarshal(data, &i); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
a.SuccessfulResponse = SuccessfulResponse{e.Success}
|
||||
a.ErrorMsg = e.ErrorsMsg
|
||||
|
||||
if e.Errors != nil {
|
||||
a.Errors = ErrorsHandler(e.Errors)
|
||||
}
|
||||
|
||||
*a = ErrorsHandler(i)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue