From 1a3a116b07353fdab2033578301d47b87bc8828b Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Thu, 18 Oct 2018 18:21:07 +0300 Subject: [PATCH] handle 402 http status --- errs/error.go | 4 ++-- errs/error_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ errs/interfaces.go | 2 +- errs/types.go | 6 +++--- 4 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 errs/error_test.go diff --git a/errs/error.go b/errs/error.go index 2582574..2730943 100644 --- a/errs/error.go +++ b/errs/error.go @@ -12,11 +12,11 @@ func (f *Failure) Error() string { // ApiError returns formatted string representation of the API error func (f *Failure) ApiError() string { - return fmt.Sprintf("%v", f.ApiErr) + return fmt.Sprintf("%+v", f.ApiErr) } // ApiErrors returns array of formatted strings that represents API errors -func (f *Failure) ApiErrors() map[string]string { +func (f *Failure) ApiErrors() interface{} { return f.ApiErrs } diff --git a/errs/error_test.go b/errs/error_test.go new file mode 100644 index 0000000..1527c36 --- /dev/null +++ b/errs/error_test.go @@ -0,0 +1,45 @@ +package errs + +import ( + "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"]}`) + + resp, e := ErrorResponse(b) + err.RuntimeErr = e + err.ApiErr = resp.ErrorMsg + + if resp.Errors != nil { + err.ApiErrs = resp.Errors + } + + f, ok := resp.Errors.([]interface{}) + + if !ok { + t.Errorf("%+v", f) + } + +} + +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"}}`) + + resp, e := ErrorResponse(b) + err.RuntimeErr = e + err.ApiErr = resp.ErrorMsg + + if resp.Errors != nil { + err.ApiErrs = resp.Errors + } + + f, ok := resp.Errors.(map[string]interface{}) + + if !ok { + t.Errorf("%+v", f) + } + +} diff --git a/errs/interfaces.go b/errs/interfaces.go index 371eec4..d8bedf1 100644 --- a/errs/interfaces.go +++ b/errs/interfaces.go @@ -4,5 +4,5 @@ package errs type Error interface { error ApiError() string - ApiErrors() map[string]string + ApiErrors() interface{} } diff --git a/errs/types.go b/errs/types.go index 1029675..a2705f2 100644 --- a/errs/types.go +++ b/errs/types.go @@ -4,11 +4,11 @@ package errs type Failure struct { RuntimeErr error ApiErr string - ApiErrs map[string]string + ApiErrs interface{} } // FailureResponse convert json error response into object type FailureResponse struct { - ErrorMsg string `json:"errorMsg,omitempty"` - Errors map[string]string `json:"errors,omitempty"` + ErrorMsg string `json:"errorMsg,omitempty"` + Errors interface{} `json:"errors,omitempty"` }