mirror of
https://github.com/retailcrm/api-client-go.git
synced 2024-11-24 13:56:04 +03:00
updated error handling
This commit is contained in:
parent
129372638a
commit
62ee2077a0
@ -3,21 +3,67 @@ package errs
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Error returns the string representation of the error and satisfies the error interface.
|
// Error returns the string representation of the error and satisfies the error interface.
|
||||||
func (f *Failure) Error() string {
|
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
|
// ApiError returns formatted string representation of the API error
|
||||||
func (f *Failure) ApiError() string {
|
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
|
// ApiErrors returns array of formatted strings that represents API errors
|
||||||
func (f *Failure) ApiErrors() interface{} {
|
func (f *Failure) ApiErrors() map[string]string {
|
||||||
return f.ApiErrs
|
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
|
// ErrorResponse method
|
||||||
|
@ -1,45 +1,50 @@
|
|||||||
package errs
|
package errs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFailure_ApiErrorsSlice(t *testing.T) {
|
func TestFailure_ApiErrorsSlice(t *testing.T) {
|
||||||
var err = Failure{}
|
var err = Failure{}
|
||||||
b := []byte(`{"success": false, "errorMsg": "Failed to activate module", "errors": ["Your account has insufficient funds to activate integration module"]}`)
|
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)
|
resp, e := ErrorResponse(b)
|
||||||
err.RuntimeErr = e
|
err.SetRuntimeError(e)
|
||||||
err.ApiErr = resp.ErrorMsg
|
err.SetApiError(resp.ErrorMsg)
|
||||||
|
|
||||||
if resp.Errors != nil {
|
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 {
|
if eq != true {
|
||||||
t.Errorf("%+v", f)
|
t.Errorf("%+v", eq)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFailure_ApiErrorsMap(t *testing.T) {
|
func TestFailure_ApiErrorsMap(t *testing.T) {
|
||||||
var err = Failure{}
|
var err = Failure{}
|
||||||
b := []byte(`{"success": false, "errorMsg": "Failed to activate module", "errors": {"id": "ID must be an integer"}}`)
|
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)
|
resp, e := ErrorResponse(b)
|
||||||
err.RuntimeErr = e
|
err.SetRuntimeError(e)
|
||||||
err.ApiErr = resp.ErrorMsg
|
err.SetApiError(resp.ErrorMsg)
|
||||||
|
|
||||||
if resp.Errors != nil {
|
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 {
|
if eq != true {
|
||||||
t.Errorf("%+v", f)
|
t.Errorf("%+v", eq)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,5 +4,5 @@ package errs
|
|||||||
type Error interface {
|
type Error interface {
|
||||||
error
|
error
|
||||||
ApiError() string
|
ApiError() string
|
||||||
ApiErrors() interface{}
|
ApiErrors() map[string]string
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,9 @@ package errs
|
|||||||
|
|
||||||
// Failure struct implode runtime & api errors
|
// Failure struct implode runtime & api errors
|
||||||
type Failure struct {
|
type Failure struct {
|
||||||
RuntimeErr error
|
runtimeErr error
|
||||||
ApiErr string
|
apiErr string
|
||||||
ApiErrs interface{}
|
apiErrs map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
// FailureResponse convert json error response into object
|
// 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