updated error handling

This commit is contained in:
Akolzin Dmitry 2019-02-21 11:51:50 +03:00
parent 129372638a
commit 62ee2077a0
6 changed files with 925 additions and 696 deletions

View File

@ -3,21 +3,67 @@ package errs
import (
"encoding/json"
"fmt"
"strconv"
)
// Error returns the string representation of the error and satisfies the error interface.
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
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
func (f *Failure) ApiErrors() interface{} {
return f.ApiErrs
func (f *Failure) ApiErrors() map[string]string {
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

View File

@ -1,45 +1,50 @@
package errs
import (
"reflect"
"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"]}`)
expected := map[string]string{
"0": "Your account has insufficient funds to activate integration module",
}
resp, e := ErrorResponse(b)
err.RuntimeErr = e
err.ApiErr = resp.ErrorMsg
err.SetRuntimeError(e)
err.SetApiError(resp.ErrorMsg)
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 {
t.Errorf("%+v", f)
if eq != true {
t.Errorf("%+v", eq)
}
}
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"}}`)
expected := map[string]string{
"id": "ID must be an integer",
}
resp, e := ErrorResponse(b)
err.RuntimeErr = e
err.ApiErr = resp.ErrorMsg
err.SetRuntimeError(e)
err.SetApiError(resp.ErrorMsg)
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 {
t.Errorf("%+v", f)
if eq != true {
t.Errorf("%+v", eq)
}
}

View File

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

View File

@ -2,9 +2,9 @@ package errs
// Failure struct implode runtime & api errors
type Failure struct {
RuntimeErr error
ApiErr string
ApiErrs interface{}
runtimeErr error
apiErr string
apiErrs map[string]string
}
// FailureResponse convert json error response into object

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff