mirror of
https://github.com/retailcrm/api-client-go.git
synced 2024-11-22 04:46:03 +03:00
commit
04f87e1fbc
@ -34,7 +34,7 @@ func main() {
|
||||
Limit: 20,
|
||||
Page: 1,
|
||||
},)
|
||||
if err.RuntimeErr != nil {
|
||||
if err != nil {
|
||||
fmt.Printf("%v", err.Error())
|
||||
}
|
||||
|
||||
@ -92,7 +92,7 @@ func main() {
|
||||
},
|
||||
},
|
||||
)
|
||||
if err.RuntimeErr != nil {
|
||||
if err != nil {
|
||||
fmt.Printf("%v", err.Error())
|
||||
}
|
||||
|
||||
|
@ -3,21 +3,67 @@ package errs
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Error returns the string representation of the error and satisfies the error interface.
|
||||
func (f *Failure) Error() string {
|
||||
return f.RuntimeErr.Error()
|
||||
if f != nil && f.runtimeErr != nil {
|
||||
return f.runtimeErr.Error()
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// ApiError returns formatted string representation of the API error
|
||||
func (f *Failure) ApiError() string {
|
||||
return fmt.Sprintf("%+v", f.ApiErr)
|
||||
if f != nil && f.apiErr != "" {
|
||||
return fmt.Sprintf("%+v", f.apiErr)
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// ApiErrors returns array of formatted strings that represents API errors
|
||||
func (f *Failure) ApiErrors() interface{} {
|
||||
return f.ApiErrs
|
||||
func (f *Failure) ApiErrors() map[string]string {
|
||||
if len(f.apiErrs) > 0 {
|
||||
return f.apiErrs
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetRuntimeError set runtime error value
|
||||
func (f *Failure) SetRuntimeError(e error) {
|
||||
f.runtimeErr = e
|
||||
}
|
||||
|
||||
// SetApiError set api error value
|
||||
func (f *Failure) SetApiError(e string) {
|
||||
f.apiErr = e
|
||||
}
|
||||
|
||||
// SetApiErrors set api errors value
|
||||
func (f *Failure) SetApiErrors(e map[string]string) {
|
||||
f.apiErrs = e
|
||||
}
|
||||
|
||||
// ErrorsHandler returns map
|
||||
func ErrorsHandler(errs interface{}) map[string]string {
|
||||
m := make(map[string]string)
|
||||
|
||||
switch errs.(type) {
|
||||
case map[string]interface{}:
|
||||
for idx, val := range errs.(map[string]interface{}) {
|
||||
m[idx] = val.(string)
|
||||
}
|
||||
case []interface{}:
|
||||
for idx, val := range errs.([]interface{}) {
|
||||
m[strconv.Itoa(idx)] = val.(string)
|
||||
}
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// ErrorResponse method
|
||||
|
@ -1,45 +1,50 @@
|
||||
package errs
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFailure_ApiErrorsSlice(t *testing.T) {
|
||||
var err = Failure{}
|
||||
b := []byte(`{"success": false, "errorMsg": "Failed to activate module", "errors": ["Your account has insufficient funds to activate integration module"]}`)
|
||||
expected := map[string]string{
|
||||
"0": "Your account has insufficient funds to activate integration module",
|
||||
}
|
||||
|
||||
resp, e := ErrorResponse(b)
|
||||
err.RuntimeErr = e
|
||||
err.ApiErr = resp.ErrorMsg
|
||||
err.SetRuntimeError(e)
|
||||
err.SetApiError(resp.ErrorMsg)
|
||||
|
||||
if resp.Errors != nil {
|
||||
err.ApiErrs = resp.Errors
|
||||
err.SetApiErrors(ErrorsHandler(resp.Errors))
|
||||
}
|
||||
|
||||
f, ok := resp.Errors.([]interface{})
|
||||
eq := reflect.DeepEqual(expected, err.ApiErrors())
|
||||
|
||||
if !ok {
|
||||
t.Errorf("%+v", f)
|
||||
if eq != true {
|
||||
t.Errorf("%+v", eq)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestFailure_ApiErrorsMap(t *testing.T) {
|
||||
var err = Failure{}
|
||||
b := []byte(`{"success": false, "errorMsg": "Failed to activate module", "errors": {"id": "ID must be an integer"}}`)
|
||||
expected := map[string]string{
|
||||
"id": "ID must be an integer",
|
||||
}
|
||||
|
||||
resp, e := ErrorResponse(b)
|
||||
err.RuntimeErr = e
|
||||
err.ApiErr = resp.ErrorMsg
|
||||
err.SetRuntimeError(e)
|
||||
err.SetApiError(resp.ErrorMsg)
|
||||
|
||||
if resp.Errors != nil {
|
||||
err.ApiErrs = resp.Errors
|
||||
err.SetApiErrors(ErrorsHandler(resp.Errors))
|
||||
}
|
||||
|
||||
f, ok := resp.Errors.(map[string]interface{})
|
||||
eq := reflect.DeepEqual(expected, err.ApiErrors())
|
||||
|
||||
if !ok {
|
||||
t.Errorf("%+v", f)
|
||||
if eq != true {
|
||||
t.Errorf("%+v", eq)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,5 +4,5 @@ package errs
|
||||
type Error interface {
|
||||
error
|
||||
ApiError() string
|
||||
ApiErrors() interface{}
|
||||
ApiErrors() map[string]string
|
||||
}
|
||||
|
@ -2,9 +2,9 @@ package errs
|
||||
|
||||
// Failure struct implode runtime & api errors
|
||||
type Failure struct {
|
||||
RuntimeErr error
|
||||
ApiErr string
|
||||
ApiErrs interface{}
|
||||
runtimeErr error
|
||||
apiErr string
|
||||
apiErrs map[string]string
|
||||
}
|
||||
|
||||
// FailureResponse convert json error response into object
|
||||
|
1145
v5/client.go
1145
v5/client.go
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user