mirror of
https://github.com/retailcrm/api-client-go.git
synced 2024-11-22 04:46:03 +03:00
handle 402 http status
This commit is contained in:
parent
15efacb4f4
commit
1a3a116b07
@ -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
45
errs/error_test.go
Normal 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)
|
||||
}
|
||||
|
||||
}
|
@ -4,5 +4,5 @@ package errs
|
||||
type Error interface {
|
||||
error
|
||||
ApiError() string
|
||||
ApiErrors() map[string]string
|
||||
ApiErrors() interface{}
|
||||
}
|
||||
|
@ -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"`
|
||||
Errors interface{} `json:"errors,omitempty"`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user