handle 402 http status

This commit is contained in:
Alex Lushpai 2018-10-18 18:21:07 +03:00
parent 15efacb4f4
commit 1a3a116b07
4 changed files with 51 additions and 6 deletions

View File

@ -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
}

45
errs/error_test.go Normal file
View File

@ -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)
}
}

View File

@ -4,5 +4,5 @@ package errs
type Error interface {
error
ApiError() string
ApiErrors() map[string]string
ApiErrors() interface{}
}

View File

@ -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"`
}