Add additional project-level error handler to separate runtime & api errors in one struct

This commit is contained in:
Alex Lushpai 2018-04-22 20:39:22 +03:00
parent 8e5a2da89a
commit 4eaf2fd9be
6 changed files with 1019 additions and 862 deletions

35
errs/error.go Normal file
View File

@ -0,0 +1,35 @@
package errs
import (
"encoding/json"
"fmt"
)
// Error returns the string representation of the error and satisfies the error interface.
func (f *Failure) Error() string {
return f.RuntimeErr.Error()
}
// ApiError returns formatted string representation of the API error
func (f *Failure) ApiError() string {
return fmt.Sprintf("%v", f.ApiErr)
}
// ApiErrors returns array of formatted strings that represents API errors
func (f *Failure) ApiErrors() []string {
var errors []string
for i := 0; i < len(f.ApiErrs); i++ {
errors = append(errors, fmt.Sprintf("%v", f.ApiErrs[i]))
}
return errors
}
// ErrorResponse method
func ErrorResponse(data []byte) (FailureResponse, error) {
var resp FailureResponse
err := json.Unmarshal(data, &resp)
return resp, err
}

8
errs/interfaces.go Normal file
View File

@ -0,0 +1,8 @@
package errs
// Error implements generic error interface
type Error interface {
error
ApiError() string
ApiErrors() []string
}

14
errs/types.go Normal file
View File

@ -0,0 +1,14 @@
package errs
// Failure struct implode runtime & api errors
type Failure struct {
RuntimeErr error
ApiErr string
ApiErrs []string
}
// FailureResponse convert json error response into object
type FailureResponse struct {
ErrorMsg string `json:"errorMsg,omitempty"`
Errors []string `json:"errors,omitempty"`
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,21 +1,5 @@
package v5 package v5
import "encoding/json"
// ErrorResponse type
type ErrorResponse struct {
ErrorMsg string `json:"errorMsg,omitempty"`
Errors map[string]string `json:"errors,omitempty"`
}
// ErrorResponse method
func (c *Client) ErrorResponse(data []byte) (ErrorResponse, error) {
var resp ErrorResponse
err := json.Unmarshal(data, &resp)
return resp, err
}
// SuccessfulResponse type // SuccessfulResponse type
type SuccessfulResponse struct { type SuccessfulResponse struct {
Success bool `json:"success,omitempty"` Success bool `json:"success,omitempty"`