Merge pull request #24 from iyzoer/master

error handling
This commit is contained in:
Alex Lushpai 2019-02-25 13:08:45 +03:00 committed by GitHub
commit 04f87e1fbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 927 additions and 698 deletions

View File

@ -34,7 +34,7 @@ func main() {
Limit: 20,
Page: 1,
},)
if err.RuntimeErr != nil {
if err != nil {
fmt.Printf("%v", err.Error())
}
@ -92,7 +92,7 @@ func main() {
},
},
)
if err.RuntimeErr != nil {
if err != nil {
fmt.Printf("%v", err.Error())
}

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