diff --git a/errs/error.go b/errs/error.go new file mode 100644 index 0000000..ca3b44e --- /dev/null +++ b/errs/error.go @@ -0,0 +1,35 @@ +package errs + +import ( + "encoding/json" + "fmt" +) + +// Error returns the string representation of the error and satisfies the error interface. +func (f *Failure) Error() string { + return f.RuntimeErr.Error() +} + +// ApiError returns formatted string representation of the API error +func (f *Failure) ApiError() string { + return fmt.Sprintf("%v", f.ApiErr) +} + +// ApiErrors returns array of formatted strings that represents API errors +func (f *Failure) ApiErrors() []string { + var errors []string + + for i := 0; i < len(f.ApiErrs); i++ { + errors = append(errors, fmt.Sprintf("%v", f.ApiErrs[i])) + } + + return errors +} + +// ErrorResponse method +func ErrorResponse(data []byte) (FailureResponse, error) { + var resp FailureResponse + err := json.Unmarshal(data, &resp) + + return resp, err +} diff --git a/errs/interfaces.go b/errs/interfaces.go new file mode 100644 index 0000000..497b6c1 --- /dev/null +++ b/errs/interfaces.go @@ -0,0 +1,8 @@ +package errs + +// Error implements generic error interface +type Error interface { + error + ApiError() string + ApiErrors() []string +} diff --git a/errs/types.go b/errs/types.go new file mode 100644 index 0000000..e390d92 --- /dev/null +++ b/errs/types.go @@ -0,0 +1,14 @@ +package errs + +// Failure struct implode runtime & api errors +type Failure struct { + RuntimeErr error + ApiErr string + ApiErrs []string +} + +// FailureResponse convert json error response into object +type FailureResponse struct { + ErrorMsg string `json:"errorMsg,omitempty"` + Errors []string `json:"errors,omitempty"` +} diff --git a/v5/client.go b/v5/client.go index 5bc5504..5f34ae0 100644 --- a/v5/client.go +++ b/v5/client.go @@ -11,6 +11,7 @@ import ( "time" "github.com/google/go-querystring/query" + "github.com/retailcrm/api-client-go/errs" ) // New initalize client @@ -23,9 +24,9 @@ func New(url string, key string) *Client { } // GetRequest implements GET Request -func (c *Client) GetRequest(urlWithParameters string, versioned ...bool) ([]byte, int, ErrorResponse) { +func (c *Client) GetRequest(urlWithParameters string, versioned ...bool) ([]byte, int, errs.Failure) { var res []byte - var bug ErrorResponse + var cerr errs.Failure var prefix = "/api/v5" if len(versioned) > 0 { @@ -38,50 +39,41 @@ func (c *Client) GetRequest(urlWithParameters string, versioned ...bool) ([]byte req, err := http.NewRequest("GET", fmt.Sprintf("%s%s%s", c.URL, prefix, urlWithParameters), nil) if err != nil { - bug.ErrorMsg = err.Error() - return res, 0, bug + cerr.RuntimeErr = err + return res, 0, cerr } req.Header.Set("X-API-KEY", c.Key) resp, err := c.httpClient.Do(req) if err != nil { - bug.ErrorMsg = err.Error() - return res, 0, bug + cerr.RuntimeErr = err + return res, 0, cerr } if resp.StatusCode >= http.StatusInternalServerError { - bug.ErrorMsg = fmt.Sprintf("HTTP request error. Status code: %d.\n", resp.StatusCode) - return res, resp.StatusCode, bug + cerr.ApiErr = fmt.Sprintf("HTTP request error. Status code: %d.\n", resp.StatusCode) + return res, resp.StatusCode, cerr } res, err = buildRawResponse(resp) if err != nil { - bug.ErrorMsg = err.Error() + cerr.RuntimeErr = err } - eresp, _ := c.ErrorResponse(res) - if eresp.ErrorMsg != "" { - return res, resp.StatusCode, eresp - } - - return res, resp.StatusCode, bug + return res, resp.StatusCode, cerr } // PostRequest implements POST Request -func (c *Client) PostRequest(url string, postParams url.Values) ([]byte, int, ErrorResponse) { +func (c *Client) PostRequest(url string, postParams url.Values) ([]byte, int, errs.Failure) { var res []byte - var bug ErrorResponse + var cerr errs.Failure var prefix = "/api/v5" - req, err := http.NewRequest( - "POST", - fmt.Sprintf("%s%s%s", c.URL, prefix, url), - strings.NewReader(postParams.Encode()), - ) + req, err := http.NewRequest("POST", fmt.Sprintf("%s%s%s", c.URL, prefix, url), strings.NewReader(postParams.Encode())) if err != nil { - bug.ErrorMsg = err.Error() - return res, 0, bug + cerr.RuntimeErr = err + return res, 0, cerr } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") @@ -89,27 +81,22 @@ func (c *Client) PostRequest(url string, postParams url.Values) ([]byte, int, Er resp, err := c.httpClient.Do(req) if err != nil { - bug.ErrorMsg = err.Error() - return res, 0, bug + cerr.RuntimeErr = err + return res, 0, cerr } if resp.StatusCode >= http.StatusInternalServerError { - bug.ErrorMsg = fmt.Sprintf("HTTP request error. Status code: %d.\n", resp.StatusCode) - return res, resp.StatusCode, bug + cerr.ApiErr = fmt.Sprintf("HTTP request error. Status code: %d.\n", resp.StatusCode) + return res, resp.StatusCode, cerr } res, err = buildRawResponse(resp) if err != nil { - bug.ErrorMsg = err.Error() - return res, 0, bug + cerr.RuntimeErr = err + return res, 0, cerr } - eresp, _ := c.ErrorResponse(res) - if eresp.ErrorMsg != "" { - return res, resp.StatusCode, eresp - } - - return res, resp.StatusCode, bug + return res, resp.StatusCode, cerr } func buildRawResponse(resp *http.Response) ([]byte, error) { @@ -123,6 +110,20 @@ func buildRawResponse(resp *http.Response) ([]byte, error) { return res, nil } +func buildErr(data []byte) errs.Failure { + var err = errs.Failure{} + + eresp, errr := errs.ErrorResponse(data) + err.RuntimeErr = errr + err.ApiErr = eresp.ErrorMsg + + if eresp.Errors != nil { + err.ApiErrs = eresp.Errors + } + + return err +} + // checkBy select identifier type func checkBy(by string) string { var context = "id" @@ -153,7 +154,7 @@ func fillSite(p *url.Values, site []string) { // // data, status, err := client.APIVersions() // -// if err.ErrorMsg != "" { +// if err.RuntimeErr != nil { // fmt.Printf("%v", err.ErrorMsg) // } // @@ -164,16 +165,20 @@ func fillSite(p *url.Values, site []string) { // for _, value := range data.versions { // fmt.Printf("%v\n", value) // } -func (c *Client) APIVersions() (VersionResponse, int, ErrorResponse) { +func (c *Client) APIVersions() (VersionResponse, int, errs.Failure) { var resp VersionResponse data, status, err := c.GetRequest("/api-versions", false) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } @@ -185,7 +190,7 @@ func (c *Client) APIVersions() (VersionResponse, int, ErrorResponse) { // // data, status, err := client.APICredentials() // -// if err.ErrorMsg != "" { +// if err.RuntimeErr != nil { // fmt.Printf("%v", err.ErrorMsg) // } // @@ -196,37 +201,45 @@ func (c *Client) APIVersions() (VersionResponse, int, ErrorResponse) { // for _, value := range data.credentials { // fmt.Printf("%v\n", value) // } -func (c *Client) APICredentials() (CredentialResponse, int, ErrorResponse) { +func (c *Client) APICredentials() (CredentialResponse, int, errs.Failure) { var resp CredentialResponse data, status, err := c.GetRequest("/credentials", false) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // Customers list method -func (c *Client) Customers(parameters CustomersRequest) (CustomersResponse, int, ErrorResponse) { +func (c *Client) Customers(parameters CustomersRequest) (CustomersResponse, int, errs.Failure) { var resp CustomersResponse params, _ := query.Values(parameters) data, status, err := c.GetRequest(fmt.Sprintf("/customers?%s", params.Encode())) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // CustomersCombine method -func (c *Client) CustomersCombine(customers []Customer, resultCustomer Customer) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) CustomersCombine(customers []Customer, resultCustomer Customer) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse combineJSONIn, _ := json.Marshal(&customers) @@ -238,17 +251,21 @@ func (c *Client) CustomersCombine(customers []Customer, resultCustomer Customer) } data, status, err := c.PostRequest("/customers/combine", p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // CustomerCreate method -func (c *Client) CustomerCreate(customer Customer, site ...string) (CustomerChangeResponse, int, ErrorResponse) { +func (c *Client) CustomerCreate(customer Customer, site ...string) (CustomerChangeResponse, int, errs.Failure) { var resp CustomerChangeResponse customerJSON, _ := json.Marshal(&customer) @@ -260,17 +277,21 @@ func (c *Client) CustomerCreate(customer Customer, site ...string) (CustomerChan fillSite(&p, site) data, status, err := c.PostRequest("/customers/create", p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // CustomersFixExternalIds method -func (c *Client) CustomersFixExternalIds(customers []IdentifiersPair) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) CustomersFixExternalIds(customers []IdentifiersPair) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse customersJSON, _ := json.Marshal(&customers) @@ -280,49 +301,61 @@ func (c *Client) CustomersFixExternalIds(customers []IdentifiersPair) (Successfu } data, status, err := c.PostRequest("/customers/fix-external-ids", p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // CustomersHistory method -func (c *Client) CustomersHistory(parameters CustomersHistoryRequest) (CustomersHistoryResponse, int, ErrorResponse) { +func (c *Client) CustomersHistory(parameters CustomersHistoryRequest) (CustomersHistoryResponse, int, errs.Failure) { var resp CustomersHistoryResponse params, _ := query.Values(parameters) data, status, err := c.GetRequest(fmt.Sprintf("/customers/history?%s", params.Encode())) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // CustomerNotes list method -func (c *Client) CustomerNotes(parameters NotesRequest) (NotesResponse, int, ErrorResponse) { +func (c *Client) CustomerNotes(parameters NotesRequest) (NotesResponse, int, errs.Failure) { var resp NotesResponse params, _ := query.Values(parameters) data, status, err := c.GetRequest(fmt.Sprintf("/customers/notes?%s", params.Encode())) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // CustomerNoteCreate method -func (c *Client) CustomerNoteCreate(note Note, site ...string) (CreateResponse, int, ErrorResponse) { +func (c *Client) CustomerNoteCreate(note Note, site ...string) (CreateResponse, int, errs.Failure) { var resp CreateResponse noteJSON, _ := json.Marshal(¬e) @@ -334,17 +367,21 @@ func (c *Client) CustomerNoteCreate(note Note, site ...string) (CreateResponse, fillSite(&p, site) data, status, err := c.PostRequest("/customers/notes/create", p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // CustomerNoteDelete method -func (c *Client) CustomerNoteDelete(id int) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) CustomerNoteDelete(id int) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse p := url.Values{ @@ -352,17 +389,21 @@ func (c *Client) CustomerNoteDelete(id int) (SuccessfulResponse, int, ErrorRespo } data, status, err := c.PostRequest(fmt.Sprintf("/customers/notes/%d/delete", id), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // CustomersUpload method -func (c *Client) CustomersUpload(customers []Customer, site ...string) (CustomersUploadResponse, int, ErrorResponse) { +func (c *Client) CustomersUpload(customers []Customer, site ...string) (CustomersUploadResponse, int, errs.Failure) { var resp CustomersUploadResponse uploadJSON, _ := json.Marshal(&customers) @@ -374,17 +415,21 @@ func (c *Client) CustomersUpload(customers []Customer, site ...string) (Customer fillSite(&p, site) data, status, err := c.PostRequest("/customers/upload", p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // Customer get method -func (c *Client) Customer(id, by, site string) (CustomerResponse, int, ErrorResponse) { +func (c *Client) Customer(id, by, site string) (CustomerResponse, int, errs.Failure) { var resp CustomerResponse var context = checkBy(by) @@ -392,17 +437,21 @@ func (c *Client) Customer(id, by, site string) (CustomerResponse, int, ErrorResp params, _ := query.Values(fw) data, status, err := c.GetRequest(fmt.Sprintf("/customers/%s?%s", id, params.Encode())) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // CustomerEdit method -func (c *Client) CustomerEdit(customer Customer, by string, site ...string) (CustomerChangeResponse, int, ErrorResponse) { +func (c *Client) CustomerEdit(customer Customer, by string, site ...string) (CustomerChangeResponse, int, errs.Failure) { var resp CustomerChangeResponse var uid = strconv.Itoa(customer.ID) var context = checkBy(by) @@ -421,17 +470,21 @@ func (c *Client) CustomerEdit(customer Customer, by string, site ...string) (Cus fillSite(&p, site) data, status, err := c.PostRequest(fmt.Sprintf("/customers/%s/edit", uid), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // DeliveryTracking method -func (c *Client) DeliveryTracking(parameters DeliveryTrackingRequest, subcode string) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) DeliveryTracking(parameters DeliveryTrackingRequest, subcode string) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse updateJSON, _ := json.Marshal(¶meters) @@ -441,33 +494,41 @@ func (c *Client) DeliveryTracking(parameters DeliveryTrackingRequest, subcode st } data, status, err := c.PostRequest(fmt.Sprintf("/delivery/generic/%s/tracking", subcode), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // DeliveryShipments method -func (c *Client) DeliveryShipments(parameters DeliveryShipmentsRequest) (DeliveryShipmentsResponse, int, ErrorResponse) { +func (c *Client) DeliveryShipments(parameters DeliveryShipmentsRequest) (DeliveryShipmentsResponse, int, errs.Failure) { var resp DeliveryShipmentsResponse params, _ := query.Values(parameters) data, status, err := c.GetRequest(fmt.Sprintf("/delivery/shipments?%s", params.Encode())) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // DeliveryShipmentCreate method -func (c *Client) DeliveryShipmentCreate(shipment DeliveryShipment, deliveryType string, site ...string) (DeliveryShipmentUpdateResponse, int, ErrorResponse) { +func (c *Client) DeliveryShipmentCreate(shipment DeliveryShipment, deliveryType string, site ...string) (DeliveryShipmentUpdateResponse, int, errs.Failure) { var resp DeliveryShipmentUpdateResponse updateJSON, _ := json.Marshal(&shipment) @@ -479,31 +540,39 @@ func (c *Client) DeliveryShipmentCreate(shipment DeliveryShipment, deliveryType fillSite(&p, site) data, status, err := c.PostRequest("/delivery/shipments/create", p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // DeliveryShipment method -func (c *Client) DeliveryShipment(id int) (DeliveryShipmentResponse, int, ErrorResponse) { +func (c *Client) DeliveryShipment(id int) (DeliveryShipmentResponse, int, errs.Failure) { var resp DeliveryShipmentResponse data, status, err := c.GetRequest(fmt.Sprintf("/delivery/shipments/%d", id)) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // DeliveryShipmentEdit method -func (c *Client) DeliveryShipmentEdit(shipment DeliveryShipment, site ...string) (DeliveryShipmentUpdateResponse, int, ErrorResponse) { +func (c *Client) DeliveryShipmentEdit(shipment DeliveryShipment, site ...string) (DeliveryShipmentUpdateResponse, int, errs.Failure) { var resp DeliveryShipmentUpdateResponse updateJSON, _ := json.Marshal(&shipment) @@ -514,64 +583,80 @@ func (c *Client) DeliveryShipmentEdit(shipment DeliveryShipment, site ...string) fillSite(&p, site) data, status, err := c.PostRequest(fmt.Sprintf("/delivery/shipments/%s/edit", strconv.Itoa(shipment.ID)), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // IntegrationModule method -func (c *Client) IntegrationModule(code string) (IntegrationModuleResponse, int, ErrorResponse) { +func (c *Client) IntegrationModule(code string) (IntegrationModuleResponse, int, errs.Failure) { var resp IntegrationModuleResponse data, status, err := c.GetRequest(fmt.Sprintf("/integration-modules/%s", code)) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // IntegrationModuleEdit method -func (c *Client) IntegrationModuleEdit(integrationModule IntegrationModule) (IntegrationModuleEditResponse, int, ErrorResponse) { +func (c *Client) IntegrationModuleEdit(integrationModule IntegrationModule) (IntegrationModuleEditResponse, int, errs.Failure) { var resp IntegrationModuleEditResponse updateJSON, _ := json.Marshal(&integrationModule) p := url.Values{"integrationModule": {string(updateJSON[:])}} data, status, err := c.PostRequest(fmt.Sprintf("/integration-modules/%s/edit", integrationModule.Code), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // Orders list method -func (c *Client) Orders(parameters OrdersRequest) (OrdersResponse, int, ErrorResponse) { +func (c *Client) Orders(parameters OrdersRequest) (OrdersResponse, int, errs.Failure) { var resp OrdersResponse params, _ := query.Values(parameters) data, status, err := c.GetRequest(fmt.Sprintf("/orders?%s", params.Encode())) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // OrdersCombine method -func (c *Client) OrdersCombine(technique string, order, resultOrder Order) (OperationResponse, int, ErrorResponse) { +func (c *Client) OrdersCombine(technique string, order, resultOrder Order) (OperationResponse, int, errs.Failure) { var resp OperationResponse combineJSONIn, _ := json.Marshal(&order) @@ -584,17 +669,21 @@ func (c *Client) OrdersCombine(technique string, order, resultOrder Order) (Oper } data, status, err := c.PostRequest("/orders/combine", p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // OrderCreate method -func (c *Client) OrderCreate(order Order, site ...string) (CreateResponse, int, ErrorResponse) { +func (c *Client) OrderCreate(order Order, site ...string) (CreateResponse, int, errs.Failure) { var resp CreateResponse orderJSON, _ := json.Marshal(&order) @@ -605,17 +694,21 @@ func (c *Client) OrderCreate(order Order, site ...string) (CreateResponse, int, fillSite(&p, site) data, status, err := c.PostRequest("/orders/create", p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // OrdersFixExternalIds method -func (c *Client) OrdersFixExternalIds(orders []IdentifiersPair) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) OrdersFixExternalIds(orders []IdentifiersPair) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse ordersJSON, _ := json.Marshal(&orders) @@ -625,33 +718,41 @@ func (c *Client) OrdersFixExternalIds(orders []IdentifiersPair) (SuccessfulRespo } data, status, err := c.PostRequest("/orders/fix-external-ids", p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // OrdersHistory method -func (c *Client) OrdersHistory(parameters OrdersHistoryRequest) (CustomersHistoryResponse, int, ErrorResponse) { +func (c *Client) OrdersHistory(parameters OrdersHistoryRequest) (CustomersHistoryResponse, int, errs.Failure) { var resp CustomersHistoryResponse params, _ := query.Values(parameters) data, status, err := c.GetRequest(fmt.Sprintf("/orders/history?%s", params.Encode())) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // OrderPaymentCreate method -func (c *Client) OrderPaymentCreate(payment Payment, site ...string) (CreateResponse, int, ErrorResponse) { +func (c *Client) OrderPaymentCreate(payment Payment, site ...string) (CreateResponse, int, errs.Failure) { var resp CreateResponse paymentJSON, _ := json.Marshal(&payment) @@ -663,17 +764,21 @@ func (c *Client) OrderPaymentCreate(payment Payment, site ...string) (CreateResp fillSite(&p, site) data, status, err := c.PostRequest("/orders/payments/create", p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // OrderPaymentDelete method -func (c *Client) OrderPaymentDelete(id int) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) OrderPaymentDelete(id int) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse p := url.Values{ @@ -681,17 +786,21 @@ func (c *Client) OrderPaymentDelete(id int) (SuccessfulResponse, int, ErrorRespo } data, status, err := c.PostRequest(fmt.Sprintf("/orders/payments/%d/delete", id), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // OrderPaymentEdit method -func (c *Client) OrderPaymentEdit(payment Payment, by string, site ...string) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) OrderPaymentEdit(payment Payment, by string, site ...string) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse var uid = strconv.Itoa(payment.ID) var context = checkBy(by) @@ -709,17 +818,21 @@ func (c *Client) OrderPaymentEdit(payment Payment, by string, site ...string) (S fillSite(&p, site) data, status, err := c.PostRequest(fmt.Sprintf("/orders/payments/%s/edit", uid), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // OrdersUpload method -func (c *Client) OrdersUpload(orders []Order, site ...string) (OrdersUploadResponse, int, ErrorResponse) { +func (c *Client) OrdersUpload(orders []Order, site ...string) (OrdersUploadResponse, int, errs.Failure) { var resp OrdersUploadResponse uploadJSON, _ := json.Marshal(&orders) @@ -731,17 +844,21 @@ func (c *Client) OrdersUpload(orders []Order, site ...string) (OrdersUploadRespo fillSite(&p, site) data, status, err := c.PostRequest("/orders/upload", p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // Order get method -func (c *Client) Order(id, by, site string) (OrderResponse, int, ErrorResponse) { +func (c *Client) Order(id, by, site string) (OrderResponse, int, errs.Failure) { var resp OrderResponse var context = checkBy(by) @@ -749,17 +866,21 @@ func (c *Client) Order(id, by, site string) (OrderResponse, int, ErrorResponse) params, _ := query.Values(fw) data, status, err := c.GetRequest(fmt.Sprintf("/orders/%s?%s", id, params.Encode())) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // OrderEdit method -func (c *Client) OrderEdit(order Order, by string, site ...string) (CreateResponse, int, ErrorResponse) { +func (c *Client) OrderEdit(order Order, by string, site ...string) (CreateResponse, int, errs.Failure) { var resp CreateResponse var uid = strconv.Itoa(order.ID) var context = checkBy(by) @@ -778,33 +899,41 @@ func (c *Client) OrderEdit(order Order, by string, site ...string) (CreateRespon fillSite(&p, site) data, status, err := c.PostRequest(fmt.Sprintf("/orders/%s/edit", uid), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // Packs list method -func (c *Client) Packs(parameters PacksRequest) (PacksResponse, int, ErrorResponse) { +func (c *Client) Packs(parameters PacksRequest) (PacksResponse, int, errs.Failure) { var resp PacksResponse params, _ := query.Values(parameters) data, status, err := c.GetRequest(fmt.Sprintf("/orders/packs?%s", params.Encode())) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // PackCreate method -func (c *Client) PackCreate(pack Pack) (CreateResponse, int, ErrorResponse) { +func (c *Client) PackCreate(pack Pack) (CreateResponse, int, errs.Failure) { var resp CreateResponse packJSON, _ := json.Marshal(&pack) @@ -813,61 +942,77 @@ func (c *Client) PackCreate(pack Pack) (CreateResponse, int, ErrorResponse) { } data, status, err := c.PostRequest("/orders/packs/create", p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // PacksHistory method -func (c *Client) PacksHistory(parameters PacksHistoryRequest) (PacksHistoryResponse, int, ErrorResponse) { +func (c *Client) PacksHistory(parameters PacksHistoryRequest) (PacksHistoryResponse, int, errs.Failure) { var resp PacksHistoryResponse params, _ := query.Values(parameters) data, status, err := c.GetRequest(fmt.Sprintf("/orders/packs/history?%s", params.Encode())) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // Pack get method -func (c *Client) Pack(id int) (PackResponse, int, ErrorResponse) { +func (c *Client) Pack(id int) (PackResponse, int, errs.Failure) { var resp PackResponse data, status, err := c.GetRequest(fmt.Sprintf("/orders/packs/%d", id)) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // PackDelete method -func (c *Client) PackDelete(id int) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) PackDelete(id int) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse data, status, err := c.PostRequest(fmt.Sprintf("/orders/packs/%d/delete", id), url.Values{}) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // PackEdit method -func (c *Client) PackEdit(pack Pack) (CreateResponse, int, ErrorResponse) { +func (c *Client) PackEdit(pack Pack) (CreateResponse, int, errs.Failure) { var resp CreateResponse packJSON, _ := json.Marshal(&pack) @@ -877,45 +1022,57 @@ func (c *Client) PackEdit(pack Pack) (CreateResponse, int, ErrorResponse) { } data, status, err := c.PostRequest(fmt.Sprintf("/orders/packs/%d/edit", pack.ID), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // Countries method -func (c *Client) Countries() (CountriesResponse, int, ErrorResponse) { +func (c *Client) Countries() (CountriesResponse, int, errs.Failure) { var resp CountriesResponse data, status, err := c.GetRequest("/reference/countries") - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // CostGroups method -func (c *Client) CostGroups() (CostGroupsResponse, int, ErrorResponse) { +func (c *Client) CostGroups() (CostGroupsResponse, int, errs.Failure) { var resp CostGroupsResponse data, status, err := c.GetRequest("/reference/cost-groups") - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // CostGroupEdit method -func (c *Client) CostGroupEdit(costGroup CostGroup) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) CostGroupEdit(costGroup CostGroup) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse objJSON, _ := json.Marshal(&costGroup) @@ -925,31 +1082,39 @@ func (c *Client) CostGroupEdit(costGroup CostGroup) (SuccessfulResponse, int, Er } data, status, err := c.PostRequest(fmt.Sprintf("/reference/cost-groups/%s/edit", costGroup.Code), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // CostItems method -func (c *Client) CostItems() (CostItemsResponse, int, ErrorResponse) { +func (c *Client) CostItems() (CostItemsResponse, int, errs.Failure) { var resp CostItemsResponse data, status, err := c.GetRequest("/reference/cost-items") - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // CostItemEdit method -func (c *Client) CostItemEdit(costItem CostItem) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) CostItemEdit(costItem CostItem) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse objJSON, _ := json.Marshal(&costItem) @@ -959,31 +1124,39 @@ func (c *Client) CostItemEdit(costItem CostItem) (SuccessfulResponse, int, Error } data, status, err := c.PostRequest(fmt.Sprintf("/reference/cost-items/%s/edit", costItem.Code), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // Couriers method -func (c *Client) Couriers() (CouriersResponse, int, ErrorResponse) { +func (c *Client) Couriers() (CouriersResponse, int, errs.Failure) { var resp CouriersResponse data, status, err := c.GetRequest("/reference/couriers") - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // CourierCreate method -func (c *Client) CourierCreate(courier Courier) (CreateResponse, int, ErrorResponse) { +func (c *Client) CourierCreate(courier Courier) (CreateResponse, int, errs.Failure) { var resp CreateResponse objJSON, _ := json.Marshal(&courier) @@ -993,17 +1166,21 @@ func (c *Client) CourierCreate(courier Courier) (CreateResponse, int, ErrorRespo } data, status, err := c.PostRequest("/reference/couriers/create", p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // CourierEdit method -func (c *Client) CourierEdit(courier Courier) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) CourierEdit(courier Courier) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse objJSON, _ := json.Marshal(&courier) @@ -1013,31 +1190,39 @@ func (c *Client) CourierEdit(courier Courier) (SuccessfulResponse, int, ErrorRes } data, status, err := c.PostRequest(fmt.Sprintf("/reference/couriers/%d/edit", courier.ID), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // DeliveryServices method -func (c *Client) DeliveryServices() (DeliveryServiceResponse, int, ErrorResponse) { +func (c *Client) DeliveryServices() (DeliveryServiceResponse, int, errs.Failure) { var resp DeliveryServiceResponse data, status, err := c.GetRequest("/reference/delivery-services") - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // DeliveryServiceEdit method -func (c *Client) DeliveryServiceEdit(deliveryService DeliveryService) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) DeliveryServiceEdit(deliveryService DeliveryService) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse objJSON, _ := json.Marshal(&deliveryService) @@ -1047,31 +1232,39 @@ func (c *Client) DeliveryServiceEdit(deliveryService DeliveryService) (Successfu } data, status, err := c.PostRequest(fmt.Sprintf("/reference/delivery-services/%s/edit", deliveryService.Code), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // DeliveryTypes method -func (c *Client) DeliveryTypes() (DeliveryTypesResponse, int, ErrorResponse) { +func (c *Client) DeliveryTypes() (DeliveryTypesResponse, int, errs.Failure) { var resp DeliveryTypesResponse data, status, err := c.GetRequest("/reference/delivery-types") - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // DeliveryTypeEdit method -func (c *Client) DeliveryTypeEdit(deliveryType DeliveryType) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) DeliveryTypeEdit(deliveryType DeliveryType) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse objJSON, _ := json.Marshal(&deliveryType) @@ -1081,31 +1274,39 @@ func (c *Client) DeliveryTypeEdit(deliveryType DeliveryType) (SuccessfulResponse } data, status, err := c.PostRequest(fmt.Sprintf("/reference/delivery-types/%s/edit", deliveryType.Code), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // LegalEntities method -func (c *Client) LegalEntities() (LegalEntitiesResponse, int, ErrorResponse) { +func (c *Client) LegalEntities() (LegalEntitiesResponse, int, errs.Failure) { var resp LegalEntitiesResponse data, status, err := c.GetRequest("/reference/legal-entities") - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // LegalEntityEdit method -func (c *Client) LegalEntityEdit(legalEntity LegalEntity) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) LegalEntityEdit(legalEntity LegalEntity) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse objJSON, _ := json.Marshal(&legalEntity) @@ -1115,31 +1316,39 @@ func (c *Client) LegalEntityEdit(legalEntity LegalEntity) (SuccessfulResponse, i } data, status, err := c.PostRequest(fmt.Sprintf("/reference/legal-entities/%s/edit", legalEntity.Code), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // OrderMethods method -func (c *Client) OrderMethods() (OrderMethodsResponse, int, ErrorResponse) { +func (c *Client) OrderMethods() (OrderMethodsResponse, int, errs.Failure) { var resp OrderMethodsResponse data, status, err := c.GetRequest("/reference/order-methods") - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // OrderMethodEdit method -func (c *Client) OrderMethodEdit(orderMethod OrderMethod) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) OrderMethodEdit(orderMethod OrderMethod) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse objJSON, _ := json.Marshal(&orderMethod) @@ -1149,31 +1358,39 @@ func (c *Client) OrderMethodEdit(orderMethod OrderMethod) (SuccessfulResponse, i } data, status, err := c.PostRequest(fmt.Sprintf("/reference/order-methods/%s/edit", orderMethod.Code), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // OrderTypes method -func (c *Client) OrderTypes() (OrderTypesResponse, int, ErrorResponse) { +func (c *Client) OrderTypes() (OrderTypesResponse, int, errs.Failure) { var resp OrderTypesResponse data, status, err := c.GetRequest("/reference/order-types") - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // OrderTypeEdit method -func (c *Client) OrderTypeEdit(orderType OrderType) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) OrderTypeEdit(orderType OrderType) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse objJSON, _ := json.Marshal(&orderType) @@ -1183,31 +1400,39 @@ func (c *Client) OrderTypeEdit(orderType OrderType) (SuccessfulResponse, int, Er } data, status, err := c.PostRequest(fmt.Sprintf("/reference/order-types/%s/edit", orderType.Code), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // PaymentStatuses method -func (c *Client) PaymentStatuses() (PaymentStatusesResponse, int, ErrorResponse) { +func (c *Client) PaymentStatuses() (PaymentStatusesResponse, int, errs.Failure) { var resp PaymentStatusesResponse data, status, err := c.GetRequest("/reference/payment-statuses") - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // PaymentStatusEdit method -func (c *Client) PaymentStatusEdit(paymentStatus PaymentStatus) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) PaymentStatusEdit(paymentStatus PaymentStatus) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse objJSON, _ := json.Marshal(&paymentStatus) @@ -1217,31 +1442,39 @@ func (c *Client) PaymentStatusEdit(paymentStatus PaymentStatus) (SuccessfulRespo } data, status, err := c.PostRequest(fmt.Sprintf("/reference/payment-statuses/%s/edit", paymentStatus.Code), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // PaymentTypes method -func (c *Client) PaymentTypes() (PaymentTypesResponse, int, ErrorResponse) { +func (c *Client) PaymentTypes() (PaymentTypesResponse, int, errs.Failure) { var resp PaymentTypesResponse data, status, err := c.GetRequest("/reference/payment-types") - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // PaymentTypeEdit method -func (c *Client) PaymentTypeEdit(paymentType PaymentType) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) PaymentTypeEdit(paymentType PaymentType) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse objJSON, _ := json.Marshal(&paymentType) @@ -1251,31 +1484,39 @@ func (c *Client) PaymentTypeEdit(paymentType PaymentType) (SuccessfulResponse, i } data, status, err := c.PostRequest(fmt.Sprintf("/reference/payment-types/%s/edit", paymentType.Code), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // PriceTypes method -func (c *Client) PriceTypes() (PriceTypesResponse, int, ErrorResponse) { +func (c *Client) PriceTypes() (PriceTypesResponse, int, errs.Failure) { var resp PriceTypesResponse data, status, err := c.GetRequest("/reference/price-types") - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // PriceTypeEdit method -func (c *Client) PriceTypeEdit(priceType PriceType) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) PriceTypeEdit(priceType PriceType) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse objJSON, _ := json.Marshal(&priceType) @@ -1285,31 +1526,39 @@ func (c *Client) PriceTypeEdit(priceType PriceType) (SuccessfulResponse, int, Er } data, status, err := c.PostRequest(fmt.Sprintf("/reference/price-types/%s/edit", priceType.Code), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // ProductStatuses method -func (c *Client) ProductStatuses() (ProductStatusesResponse, int, ErrorResponse) { +func (c *Client) ProductStatuses() (ProductStatusesResponse, int, errs.Failure) { var resp ProductStatusesResponse data, status, err := c.GetRequest("/reference/product-statuses") - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // ProductStatusEdit method -func (c *Client) ProductStatusEdit(productStatus ProductStatus) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) ProductStatusEdit(productStatus ProductStatus) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse objJSON, _ := json.Marshal(&productStatus) @@ -1319,31 +1568,39 @@ func (c *Client) ProductStatusEdit(productStatus ProductStatus) (SuccessfulRespo } data, status, err := c.PostRequest(fmt.Sprintf("/reference/product-statuses/%s/edit", productStatus.Code), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // Sites method -func (c *Client) Sites() (SitesResponse, int, ErrorResponse) { +func (c *Client) Sites() (SitesResponse, int, errs.Failure) { var resp SitesResponse data, status, err := c.GetRequest("/reference/sites") - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // SiteEdit method -func (c *Client) SiteEdit(site Site) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) SiteEdit(site Site) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse objJSON, _ := json.Marshal(&site) @@ -1353,45 +1610,57 @@ func (c *Client) SiteEdit(site Site) (SuccessfulResponse, int, ErrorResponse) { } data, status, err := c.PostRequest(fmt.Sprintf("/reference/sites/%s/edit", site.Code), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // StatusGroups method -func (c *Client) StatusGroups() (StatusGroupsResponse, int, ErrorResponse) { +func (c *Client) StatusGroups() (StatusGroupsResponse, int, errs.Failure) { var resp StatusGroupsResponse data, status, err := c.GetRequest("/reference/status-groups") - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // Statuses method -func (c *Client) Statuses() (StatusesResponse, int, ErrorResponse) { +func (c *Client) Statuses() (StatusesResponse, int, errs.Failure) { var resp StatusesResponse data, status, err := c.GetRequest("/reference/statuses") - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // StatusEdit method -func (c *Client) StatusEdit(st Status) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) StatusEdit(st Status) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse objJSON, _ := json.Marshal(&st) @@ -1401,31 +1670,39 @@ func (c *Client) StatusEdit(st Status) (SuccessfulResponse, int, ErrorResponse) } data, status, err := c.PostRequest(fmt.Sprintf("/reference/statuses/%s/edit", st.Code), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // Stores method -func (c *Client) Stores() (StoresResponse, int, ErrorResponse) { +func (c *Client) Stores() (StoresResponse, int, errs.Failure) { var resp StoresResponse data, status, err := c.GetRequest("/reference/stores") - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // StoreEdit method -func (c *Client) StoreEdit(store Store) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) StoreEdit(store Store) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse objJSON, _ := json.Marshal(&store) @@ -1435,49 +1712,61 @@ func (c *Client) StoreEdit(store Store) (SuccessfulResponse, int, ErrorResponse) } data, status, err := c.PostRequest(fmt.Sprintf("/reference/stores/%s/edit", store.Code), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // Segments get segments -func (c *Client) Segments(parameters SegmentsRequest) (SegmentsResponse, int, ErrorResponse) { +func (c *Client) Segments(parameters SegmentsRequest) (SegmentsResponse, int, errs.Failure) { var resp SegmentsResponse params, _ := query.Values(parameters) data, status, err := c.GetRequest(fmt.Sprintf("/segments?%s", params.Encode())) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // Inventories method -func (c *Client) Inventories(parameters InventoriesRequest) (InventoriesResponse, int, ErrorResponse) { +func (c *Client) Inventories(parameters InventoriesRequest) (InventoriesResponse, int, errs.Failure) { var resp InventoriesResponse params, _ := query.Values(parameters) data, status, err := c.GetRequest(fmt.Sprintf("/store/inventories?%s", params.Encode())) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // InventoriesUpload method -func (c *Client) InventoriesUpload(inventories []InventoryUpload, site ...string) (StoreUploadResponse, int, ErrorResponse) { +func (c *Client) InventoriesUpload(inventories []InventoryUpload, site ...string) (StoreUploadResponse, int, errs.Failure) { var resp StoreUploadResponse uploadJSON, _ := json.Marshal(&inventories) @@ -1489,17 +1778,21 @@ func (c *Client) InventoriesUpload(inventories []InventoryUpload, site ...string fillSite(&p, site) data, status, err := c.PostRequest("/store/inventories/upload", p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // PricesUpload method -func (c *Client) PricesUpload(prices []OfferPriceUpload) (StoreUploadResponse, int, ErrorResponse) { +func (c *Client) PricesUpload(prices []OfferPriceUpload) (StoreUploadResponse, int, errs.Failure) { var resp StoreUploadResponse uploadJSON, _ := json.Marshal(&prices) @@ -1509,81 +1802,101 @@ func (c *Client) PricesUpload(prices []OfferPriceUpload) (StoreUploadResponse, i } data, status, err := c.PostRequest("/store/prices/upload", p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // ProductsGroup method -func (c *Client) ProductsGroup(parameters ProductsGroupsRequest) (ProductsGroupsResponse, int, ErrorResponse) { +func (c *Client) ProductsGroup(parameters ProductsGroupsRequest) (ProductsGroupsResponse, int, errs.Failure) { var resp ProductsGroupsResponse params, _ := query.Values(parameters) data, status, err := c.GetRequest(fmt.Sprintf("/store/product-groups?%s", params.Encode())) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // Products method -func (c *Client) Products(parameters ProductsRequest) (ProductsResponse, int, ErrorResponse) { +func (c *Client) Products(parameters ProductsRequest) (ProductsResponse, int, errs.Failure) { var resp ProductsResponse params, _ := query.Values(parameters) data, status, err := c.GetRequest(fmt.Sprintf("/store/products?%s", params.Encode())) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // ProductsProperties method -func (c *Client) ProductsProperties(parameters ProductsPropertiesRequest) (ProductsPropertiesResponse, int, ErrorResponse) { +func (c *Client) ProductsProperties(parameters ProductsPropertiesRequest) (ProductsPropertiesResponse, int, errs.Failure) { var resp ProductsPropertiesResponse params, _ := query.Values(parameters) data, status, err := c.GetRequest(fmt.Sprintf("/store/products/properties?%s", params.Encode())) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // Tasks list method -func (c *Client) Tasks(parameters TasksRequest) (TasksResponse, int, ErrorResponse) { +func (c *Client) Tasks(parameters TasksRequest) (TasksResponse, int, errs.Failure) { var resp TasksResponse params, _ := query.Values(parameters) data, status, err := c.GetRequest(fmt.Sprintf("/tasks?%s", params.Encode())) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // TaskCreate method -func (c *Client) TaskCreate(task Task, site ...string) (CreateResponse, int, ErrorResponse) { +func (c *Client) TaskCreate(task Task, site ...string) (CreateResponse, int, errs.Failure) { var resp CreateResponse taskJSON, _ := json.Marshal(&task) @@ -1594,31 +1907,39 @@ func (c *Client) TaskCreate(task Task, site ...string) (CreateResponse, int, Err fillSite(&p, site) data, status, err := c.PostRequest("/tasks/create", p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // Task get method -func (c *Client) Task(id int) (TaskResponse, int, ErrorResponse) { +func (c *Client) Task(id int) (TaskResponse, int, errs.Failure) { var resp TaskResponse data, status, err := c.GetRequest(fmt.Sprintf("/tasks/%d", id)) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // TaskEdit method -func (c *Client) TaskEdit(task Task, site ...string) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) TaskEdit(task Task, site ...string) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse var uid = strconv.Itoa(task.ID) @@ -1631,63 +1952,79 @@ func (c *Client) TaskEdit(task Task, site ...string) (SuccessfulResponse, int, E fillSite(&p, site) data, status, err := c.PostRequest(fmt.Sprintf("/tasks/%s/edit", uid), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // UserGroups list method -func (c *Client) UserGroups(parameters UserGroupsRequest) (UserGroupsResponse, int, ErrorResponse) { +func (c *Client) UserGroups(parameters UserGroupsRequest) (UserGroupsResponse, int, errs.Failure) { var resp UserGroupsResponse params, _ := query.Values(parameters) data, status, err := c.GetRequest(fmt.Sprintf("/user-groups?%s", params.Encode())) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // Users list method -func (c *Client) Users(parameters UsersRequest) (UsersResponse, int, ErrorResponse) { +func (c *Client) Users(parameters UsersRequest) (UsersResponse, int, errs.Failure) { var resp UsersResponse params, _ := query.Values(parameters) data, status, err := c.GetRequest(fmt.Sprintf("/users?%s", params.Encode())) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // User get method -func (c *Client) User(id int) (UserResponse, int, ErrorResponse) { +func (c *Client) User(id int) (UserResponse, int, errs.Failure) { var resp UserResponse data, status, err := c.GetRequest(fmt.Sprintf("/users/%d", id)) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } // UserStatus update method -func (c *Client) UserStatus(id int, status string) (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) UserStatus(id int, status string) (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse p := url.Values{ @@ -1695,25 +2032,33 @@ func (c *Client) UserStatus(id int, status string) (SuccessfulResponse, int, Err } data, st, err := c.PostRequest(fmt.Sprintf("/users/%d/status", id), p) - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, st, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, st, buildErr(data) + } + return resp, st, err } // StaticticsUpdate update statistic -func (c *Client) StaticticsUpdate() (SuccessfulResponse, int, ErrorResponse) { +func (c *Client) StaticticsUpdate() (SuccessfulResponse, int, errs.Failure) { var resp SuccessfulResponse data, status, err := c.GetRequest("/statistic/update") - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { return resp, status, err } json.Unmarshal(data, &resp) + if resp.Success == false { + return resp, status, buildErr(data) + } + return resp, status, err } diff --git a/v5/client_test.go b/v5/client_test.go index e304ebd..f234ebf 100644 --- a/v5/client_test.go +++ b/v5/client_test.go @@ -34,6 +34,14 @@ func client() *Client { return New(os.Getenv("RETAILCRM_URL"), os.Getenv("RETAILCRM_KEY")) } +func badurlclient() *Client { + return New("https://qwertypoiu.retailcrm.ru", os.Getenv("RETAILCRM_KEY")) +} + +func badkeyclient() *Client { + return New(os.Getenv("RETAILCRM_URL"), "1234567890") +} + func TestGetRequest(t *testing.T) { c := client() _, status, _ := c.GetRequest("/fake-method") @@ -56,19 +64,50 @@ func TestClient_ApiVersionsVersions(t *testing.T) { c := client() data, status, err := c.APIVersions() - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) + } +} + +func TestClient_ApiVersionsVersionsBadUrl(t *testing.T) { + c := badurlclient() + + data, status, err := c.APIVersions() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) + } + + if status >= http.StatusBadRequest { + t.Errorf("%v", err.ApiError()) + } + + if data.Success != true { + t.Logf("%v", err.ApiError()) + } +} + +func TestClient_ApiVersionsVersionsBadKey(t *testing.T) { + c := badkeyclient() + + data, status, err := c.APIVersions() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) + } + + if status >= http.StatusBadRequest { + t.Logf("%v", err.ApiError()) + } + + if data.Success != true { + t.Logf("%v", err.ApiError()) } } @@ -76,16 +115,16 @@ func TestClient_ApiCredentialsCredentials(t *testing.T) { c := client() data, status, err := c.APICredentials() - if err.ErrorMsg != "" { + if err.RuntimeErr != nil { t.Fail() } if status >= http.StatusBadRequest { - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -99,19 +138,16 @@ func TestClient_CustomersCustomers(t *testing.T) { Page: 3, }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -132,59 +168,49 @@ func TestClient_CustomerChange(t *testing.T) { } cr, sc, err := c.CustomerCreate(f) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if sc != http.StatusCreated { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if cr.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } f.ID = cr.ID f.Vip = true ed, se, err := c.CustomerEdit(f, "id") - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if se != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if ed.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } data, status, err := c.Customer(f.ExternalID, "externalId", "") - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Customer.ExternalID != f.ExternalID { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -202,19 +228,16 @@ func TestClient_CustomersUpload(t *testing.T) { } data, status, err := c.CustomersUpload(customers) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -227,19 +250,16 @@ func TestClient_CustomersCombine(t *testing.T) { ExternalID: RandomString(8), Email: fmt.Sprintf("%s@example.com", RandomString(8)), }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if dataFirst.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } dataSecond, status, err := c.CustomerCreate(Customer{ @@ -248,19 +268,16 @@ func TestClient_CustomersCombine(t *testing.T) { ExternalID: RandomString(8), Email: fmt.Sprintf("%s@example.com", RandomString(8)), }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if dataSecond.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } dataThird, status, err := c.CustomerCreate(Customer{ @@ -269,35 +286,29 @@ func TestClient_CustomersCombine(t *testing.T) { ExternalID: RandomString(8), Email: fmt.Sprintf("%s@example.com", RandomString(8)), }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if dataThird.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } data, status, err := c.CustomersCombine([]Customer{{ID: dataFirst.ID}, {ID: dataSecond.ID}}, Customer{ID: dataThird.ID}) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -311,19 +322,16 @@ func TestClient_CustomersFixExternalIds(t *testing.T) { } cr, sc, err := c.CustomerCreate(f) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if sc != http.StatusCreated { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if cr.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } customers := []IdentifiersPair{{ @@ -332,19 +340,16 @@ func TestClient_CustomersFixExternalIds(t *testing.T) { }} fx, fe, err := c.CustomersFixExternalIds(customers) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if fe != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if fx.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -357,24 +362,20 @@ func TestClient_CustomersHistory(t *testing.T) { } data, status, err := c.CustomersHistory(f) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if len(data.History) == 0 { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", "Empty history") } } @@ -382,19 +383,16 @@ func TestClient_NotesNotes(t *testing.T) { c := client() data, status, err := c.CustomerNotes(NotesRequest{Page: 1}) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -408,19 +406,16 @@ func TestClient_NotesCreateDelete(t *testing.T) { ExternalID: RandomString(8), Email: fmt.Sprintf("%s@example.com", RandomString(8)), }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if createCustomerStatus != http.StatusCreated { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if createCustomerResponse.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } noteCreateResponse, noteCreateStatus, err := c.CustomerNoteCreate(Note{ @@ -430,35 +425,29 @@ func TestClient_NotesCreateDelete(t *testing.T) { ID: createCustomerResponse.ID, }, }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if noteCreateStatus != http.StatusCreated { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if noteCreateResponse.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } noteDeleteResponse, noteDeleteStatus, err := c.CustomerNoteDelete(noteCreateResponse.ID) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if noteDeleteStatus != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if noteDeleteResponse.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -466,19 +455,16 @@ func TestClient_OrdersOrders(t *testing.T) { c := client() data, status, err := c.Orders(OrdersRequest{Filter: OrdersFilter{City: "Москва"}, Page: 1}) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -496,59 +482,49 @@ func TestClient_OrderChange(t *testing.T) { } cr, sc, err := c.OrderCreate(f) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if sc != http.StatusCreated { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if cr.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } f.ID = cr.ID f.CustomerComment = "test comment" ed, se, err := c.OrderEdit(f, "id") - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if se != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if ed.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } data, status, err := c.Order(f.ExternalID, "externalId", "") - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Order.ExternalID != f.ExternalID { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -566,19 +542,16 @@ func TestClient_OrdersUpload(t *testing.T) { } data, status, err := c.OrdersUpload(orders) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -591,19 +564,16 @@ func TestClient_OrdersCombine(t *testing.T) { ExternalID: RandomString(8), Email: fmt.Sprintf("%s@example.com", RandomString(8)), }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if dataFirst.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } dataSecond, status, err := c.OrderCreate(Order{ @@ -612,35 +582,29 @@ func TestClient_OrdersCombine(t *testing.T) { ExternalID: RandomString(8), Email: fmt.Sprintf("%s@example.com", RandomString(8)), }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if dataSecond.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } data, status, err := c.OrdersCombine("ours", Order{ID: dataFirst.ID}, Order{ID: dataSecond.ID}) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -654,19 +618,16 @@ func TestClient_OrdersFixExternalIds(t *testing.T) { } cr, sc, err := c.OrderCreate(f) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if sc != http.StatusCreated { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if cr.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } orders := []IdentifiersPair{{ @@ -675,19 +636,16 @@ func TestClient_OrdersFixExternalIds(t *testing.T) { }} fx, fe, err := c.OrdersFixExternalIds(orders) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if fe != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if fx.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -695,24 +653,20 @@ func TestClient_OrdersHistory(t *testing.T) { c := client() data, status, err := c.OrdersHistory(OrdersHistoryRequest{Filter: OrdersHistoryFilter{SinceID: 20}}) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if len(data.History) == 0 { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -728,19 +682,16 @@ func TestClient_PaymentCreateEditDelete(t *testing.T) { } createOrderResponse, status, err := c.OrderCreate(order) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status != http.StatusCreated { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if createOrderResponse.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } f := Payment{ @@ -752,19 +703,16 @@ func TestClient_PaymentCreateEditDelete(t *testing.T) { } paymentCreateResponse, status, err := c.OrderPaymentCreate(f) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status != http.StatusCreated { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if paymentCreateResponse.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } k := Payment{ @@ -773,35 +721,29 @@ func TestClient_PaymentCreateEditDelete(t *testing.T) { } paymentEditResponse, status, err := c.OrderPaymentEdit(k, "id") - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if paymentEditResponse.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } paymentDeleteResponse, status, err := c.OrderPaymentDelete(paymentCreateResponse.ID) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if paymentDeleteResponse.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -816,19 +758,16 @@ func TestClient_TasksTasks(t *testing.T) { } data, status, err := c.Tasks(f) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -841,54 +780,45 @@ func TestClient_TaskChange(t *testing.T) { } cr, sc, err := c.TaskCreate(f) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if sc != http.StatusCreated { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if cr.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } f.ID = cr.ID f.Commentary = RandomString(20) gt, sg, err := c.Task(f.ID) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if sg != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if gt.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } data, status, err := c.TaskEdit(f) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -896,19 +826,16 @@ func TestClient_UsersUsers(t *testing.T) { c := client() data, status, err := c.Users(UsersRequest{Filter: UsersFilter{Active: 1}, Page: 1}) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -916,19 +843,16 @@ func TestClient_UsersUser(t *testing.T) { c := client() data, st, err := c.User(user) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -936,19 +860,16 @@ func TestClient_UsersGroups(t *testing.T) { c := client() data, status, err := c.UserGroups(UserGroupsRequest{Page: 1}) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -956,19 +877,16 @@ func TestClient_UsersUpdate(t *testing.T) { c := client() data, st, err := c.UserStatus(user, "busy") - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -976,19 +894,16 @@ func TestClient_StaticticsUpdate(t *testing.T) { c := client() data, st, err := c.StaticticsUpdate() - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -996,19 +911,16 @@ func TestClient_Countries(t *testing.T) { c := client() data, st, err := c.Couriers() - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1016,19 +928,16 @@ func TestClient_CostGroups(t *testing.T) { c := client() data, st, err := c.CostGroups() - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1036,19 +945,16 @@ func TestClient_CostItems(t *testing.T) { c := client() data, st, err := c.CostItems() - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1056,19 +962,16 @@ func TestClient_Couriers(t *testing.T) { c := client() data, st, err := c.Couriers() - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1076,19 +979,16 @@ func TestClient_DeliveryService(t *testing.T) { c := client() data, st, err := c.DeliveryServices() - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1096,19 +996,16 @@ func TestClient_DeliveryTypes(t *testing.T) { c := client() data, st, err := c.DeliveryTypes() - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1116,19 +1013,16 @@ func TestClient_LegalEntities(t *testing.T) { c := client() data, st, err := c.LegalEntities() - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1136,19 +1030,16 @@ func TestClient_OrderMethods(t *testing.T) { c := client() data, st, err := c.OrderMethods() - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1156,19 +1047,16 @@ func TestClient_OrderTypes(t *testing.T) { c := client() data, st, err := c.OrderTypes() - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1176,19 +1064,16 @@ func TestClient_PaymentStatuses(t *testing.T) { c := client() data, st, err := c.PaymentStatuses() - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1196,19 +1081,16 @@ func TestClient_PaymentTypes(t *testing.T) { c := client() data, st, err := c.PaymentTypes() - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1216,19 +1098,16 @@ func TestClient_PriceTypes(t *testing.T) { c := client() data, st, err := c.PriceTypes() - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1236,19 +1115,16 @@ func TestClient_ProductStatuses(t *testing.T) { c := client() data, st, err := c.ProductStatuses() - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1256,19 +1132,16 @@ func TestClient_Statuses(t *testing.T) { c := client() data, st, err := c.Statuses() - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1276,19 +1149,16 @@ func TestClient_StatusGroups(t *testing.T) { c := client() data, st, err := c.StatusGroups() - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1296,19 +1166,16 @@ func TestClient_Sites(t *testing.T) { c := client() data, st, err := c.Sites() - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1316,19 +1183,16 @@ func TestClient_Stores(t *testing.T) { c := client() data, st, err := c.Stores() - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if st != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1343,19 +1207,16 @@ func TestClient_CostGroupItemEdit(t *testing.T) { Color: "#da5c98", Name: fmt.Sprintf("CostGroup-%s", uid), }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if !statuses[st] { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } idata, st, err := c.CostItemEdit(CostItem{ @@ -1366,19 +1227,16 @@ func TestClient_CostGroupItemEdit(t *testing.T) { AppliesToOrders: true, Active: false, }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if !statuses[st] { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if idata.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1393,38 +1251,32 @@ func TestClient_Courier(t *testing.T) { } data, st, err := c.CourierCreate(cur) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if !statuses[st] { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } cur.ID = data.ID cur.Patronymic = fmt.Sprintf("%s", RandomString(5)) idata, st, err := c.CourierEdit(cur) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if !statuses[st] { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if idata.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1436,19 +1288,16 @@ func TestClient_DeliveryServiceEdit(t *testing.T) { Code: RandomString(5), Name: RandomString(5), }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if !statuses[st] { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1465,19 +1314,16 @@ func TestClient_DeliveryTypeEdit(t *testing.T) { PaymentTypes: x, DefaultForCrm: false, }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if !statuses[st] { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1490,19 +1336,16 @@ func TestClient_OrderMethodEdit(t *testing.T) { Active: false, DefaultForCRM: false, }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if !statuses[st] { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1515,19 +1358,16 @@ func TestClient_OrderTypeEdit(t *testing.T) { Active: false, DefaultForCRM: false, }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if !statuses[st] { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1542,19 +1382,16 @@ func TestClient_PaymentStatusEdit(t *testing.T) { PaymentTypes: []string{"cash"}, PaymentComplete: false, }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if !statuses[st] { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1567,19 +1404,16 @@ func TestClient_PaymentTypeEdit(t *testing.T) { Active: false, DefaultForCRM: false, }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if !statuses[st] { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1591,19 +1425,16 @@ func TestClient_PriceTypeEdit(t *testing.T) { Name: RandomString(5), Active: false, }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if !statuses[st] { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1616,19 +1447,16 @@ func TestClient_ProductStatusEdit(t *testing.T) { Active: false, CancelStatus: false, }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if !statuses[st] { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1641,19 +1469,16 @@ func TestClient_StatusEdit(t *testing.T) { Active: false, Group: "new", }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if !statuses[st] { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1666,14 +1491,12 @@ func TestClient_SiteEdit(t *testing.T) { URL: fmt.Sprintf("https://%s.example.com", RandomString(4)), LoadFromYml: false, }) - if err.ErrorMsg == "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if data.Success != false { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1687,19 +1510,16 @@ func TestClient_StoreEdit(t *testing.T) { Type: "store-type-warehouse", InventoryType: "integer", }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if !statuses[st] { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1714,35 +1534,29 @@ func TestClient_PackChange(t *testing.T) { Email: fmt.Sprintf("%s@example.com", RandomString(8)), Items: []OrderItem{{Offer: Offer{ID: 1609}, Quantity: 5}}, }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status != http.StatusCreated { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if o.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } g, status, err := c.Order(strconv.Itoa(o.ID), "id", "") - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if o.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } p, status, err := c.PackCreate(Pack{ @@ -1750,67 +1564,55 @@ func TestClient_PackChange(t *testing.T) { ItemID: g.Order.Items[0].ID, Quantity: 1, }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status != http.StatusCreated { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if p.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } s, status, err := c.Pack(p.ID) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if s.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } e, status, err := c.PackEdit(Pack{ID: p.ID, Quantity: 2}) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if e.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } d, status, err := c.PackDelete(p.ID) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if d.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1818,24 +1620,20 @@ func TestClient_PacksHistory(t *testing.T) { c := client() data, status, err := c.PacksHistory(PacksHistoryRequest{Filter: OrdersHistoryFilter{SinceID: 5}}) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if len(data.History) == 0 { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1843,19 +1641,16 @@ func TestClient_Packs(t *testing.T) { c := client() data, status, err := c.Packs(PacksRequest{Filter: PacksFilter{}, Page: 1}) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1863,19 +1658,16 @@ func TestClient_Inventories(t *testing.T) { c := client() data, status, err := c.Inventories(InventoriesRequest{Filter: InventoriesFilter{Details: 1}, Page: 1}) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1883,19 +1675,16 @@ func TestClient_Segments(t *testing.T) { c := client() data, status, err := c.Segments(SegmentsRequest{}) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status >= http.StatusBadRequest { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if data.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1915,35 +1704,29 @@ func TestClient_IntegrationModule(t *testing.T) { ClientID: RandomString(10), Logo: "https://cdn.worldvectorlogo.com/logos/github-icon.svg", }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status != http.StatusCreated { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if m.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } g, status, err := c.IntegrationModule(code) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if g.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -1954,7 +1737,7 @@ func TestClient_IntegrationModuleFail(t *testing.T) { m, status, err := c.IntegrationModuleEdit(IntegrationModule{ Code: code, }) - if err.ErrorMsg == "" { + if err.RuntimeErr == nil { t.Fail() } @@ -1967,7 +1750,7 @@ func TestClient_IntegrationModuleFail(t *testing.T) { } g, status, err := c.IntegrationModule(RandomString(12)) - if err.ErrorMsg == "" { + if err.RuntimeErr == nil { t.Fail() } @@ -1988,19 +1771,16 @@ func TestClient_ProductsGroup(t *testing.T) { Active: 1, }, }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if g.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -2013,19 +1793,16 @@ func TestClient_Products(t *testing.T) { MinPrice: 1, }, }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if g.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -2040,19 +1817,16 @@ func TestClient_ProductsProperties(t *testing.T) { Sites: sites, }, }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if g.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } @@ -2064,18 +1838,15 @@ func TestClient_DeliveryShipments(t *testing.T) { DateFrom: "2017-10-10", }, }) - if err.ErrorMsg != "" { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + if err.RuntimeErr != nil { + t.Errorf("%v", err.Error()) } if status != http.StatusOK { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } if g.Success != true { - t.Errorf("%v", err.ErrorMsg) - t.Fail() + t.Errorf("%v", err.ApiError()) } } diff --git a/v5/response.go b/v5/response.go index e33b9a3..84d4fd0 100644 --- a/v5/response.go +++ b/v5/response.go @@ -1,21 +1,5 @@ package v5 -import "encoding/json" - -// ErrorResponse type -type ErrorResponse struct { - ErrorMsg string `json:"errorMsg,omitempty"` - Errors map[string]string `json:"errors,omitempty"` -} - -// ErrorResponse method -func (c *Client) ErrorResponse(data []byte) (ErrorResponse, error) { - var resp ErrorResponse - err := json.Unmarshal(data, &resp) - - return resp, err -} - // SuccessfulResponse type type SuccessfulResponse struct { Success bool `json:"success,omitempty"`