Merge pull request #21 from gwinn/master

handle 402 http status
This commit is contained in:
Ilyas Salikhov 2018-10-22 16:04:00 +03:00 committed by GitHub
commit 3e0076bf6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 6 deletions

View File

@ -3,6 +3,7 @@ go:
- '1.8'
- '1.9'
- '1.10'
- '1.11'
before_install:
- go get -v github.com/google/go-querystring/query
- go get -v github.com/h2non/gock

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