mirror of
https://github.com/retailcrm/api-client-go.git
synced 2024-11-25 14:26:04 +03:00
Add additional project-level error handler to separate runtime & api errors in one struct
This commit is contained in:
parent
8e5a2da89a
commit
4eaf2fd9be
35
errs/error.go
Normal file
35
errs/error.go
Normal 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
8
errs/interfaces.go
Normal 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
14
errs/types.go
Normal 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"`
|
||||||
|
}
|
765
v5/client.go
765
v5/client.go
File diff suppressed because it is too large
Load Diff
1043
v5/client_test.go
1043
v5/client_test.go
File diff suppressed because it is too large
Load Diff
@ -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"`
|
||||||
|
Loading…
Reference in New Issue
Block a user