fix export model and Error method
This commit is contained in:
parent
5e3e7cd03d
commit
bb57d2c2e8
@ -737,9 +737,9 @@ func (t *MGClientTest) Test_SuccessHandleError() {
|
|||||||
_, statusCode, err := client.DeactivateTransportChannel(123)
|
_, statusCode, err := client.DeactivateTransportChannel(123)
|
||||||
|
|
||||||
t.Assert().Equal(http.StatusInternalServerError, statusCode)
|
t.Assert().Equal(http.StatusInternalServerError, statusCode)
|
||||||
t.Assert().IsType(new(httpClientError), err)
|
t.Assert().IsType(new(HTTPClientError), err)
|
||||||
t.Assert().Equal(internalServerError, err.Error())
|
t.Assert().Equal(internalServerError, err.Error())
|
||||||
var serverErr *httpClientError
|
var serverErr *HTTPClientError
|
||||||
if errors.As(err, &serverErr) {
|
if errors.As(err, &serverErr) {
|
||||||
t.Assert().Nil(serverErr.Response)
|
t.Assert().Nil(serverErr.Response)
|
||||||
} else {
|
} else {
|
||||||
@ -748,6 +748,6 @@ func (t *MGClientTest) Test_SuccessHandleError() {
|
|||||||
|
|
||||||
_, statusCode, err = client.DeactivateTransportChannel(455)
|
_, statusCode, err = client.DeactivateTransportChannel(455)
|
||||||
t.Assert().Equal(http.StatusBadRequest, statusCode)
|
t.Assert().Equal(http.StatusBadRequest, statusCode)
|
||||||
t.Assert().IsType(new(httpClientError), err)
|
t.Assert().IsType(new(HTTPClientError), err)
|
||||||
t.Assert().Equal("Channel not found", err.Error())
|
t.Assert().Equal("Channel not found", err.Error())
|
||||||
}
|
}
|
||||||
|
16
v1/errors.go
16
v1/errors.go
@ -17,24 +17,22 @@ type MGErrors struct {
|
|||||||
Errors []string
|
Errors []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type httpClientError struct {
|
type HTTPClientError struct {
|
||||||
ErrorMsg string
|
ErrorMsg string
|
||||||
BaseError error
|
BaseError error
|
||||||
Response io.Reader
|
Response io.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
func (err *httpClientError) Unwrap() error {
|
func (err *HTTPClientError) Unwrap() error {
|
||||||
return err.BaseError
|
return err.BaseError
|
||||||
}
|
}
|
||||||
|
|
||||||
func (err *httpClientError) Error() string {
|
func (err *HTTPClientError) Error() string {
|
||||||
message := defaultErrorMessage
|
message := defaultErrorMessage
|
||||||
|
|
||||||
if err.BaseError != nil {
|
if err.BaseError != nil {
|
||||||
message = fmt.Sprintf("%s: %s", defaultErrorMessage, err.BaseError.Error())
|
message = fmt.Sprintf("%s: %s", defaultErrorMessage, err.BaseError.Error())
|
||||||
}
|
} else if len(err.ErrorMsg) > 0 {
|
||||||
|
|
||||||
if len(err.ErrorMsg) > 0 {
|
|
||||||
message = err.ErrorMsg
|
message = err.ErrorMsg
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,7 +40,7 @@ func (err *httpClientError) Error() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewCriticalHTTPError(err error) error {
|
func NewCriticalHTTPError(err error) error {
|
||||||
return &httpClientError{BaseError: err}
|
return &HTTPClientError{BaseError: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAPIClientError(responseBody []byte) error {
|
func NewAPIClientError(responseBody []byte) error {
|
||||||
@ -61,11 +59,11 @@ func NewAPIClientError(responseBody []byte) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &httpClientError{ErrorMsg: message}
|
return &HTTPClientError{ErrorMsg: message}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServerError(response *http.Response) error {
|
func NewServerError(response *http.Response) error {
|
||||||
var serverError *httpClientError
|
var serverError *HTTPClientError
|
||||||
|
|
||||||
body, _ := buildLimitedRawResponse(response)
|
body, _ := buildLimitedRawResponse(response)
|
||||||
err := NewAPIClientError(body)
|
err := NewAPIClientError(body)
|
||||||
|
@ -16,7 +16,7 @@ func TestNewCriticalHTTPError(t *testing.T) {
|
|||||||
err := &url.Error{Op: "Get", URL: "http//example.com", Err: errors.New("EOF")}
|
err := &url.Error{Op: "Get", URL: "http//example.com", Err: errors.New("EOF")}
|
||||||
httpErr := NewCriticalHTTPError(err)
|
httpErr := NewCriticalHTTPError(err)
|
||||||
|
|
||||||
assert.IsType(t, new(httpClientError), httpErr)
|
assert.IsType(t, new(HTTPClientError), httpErr)
|
||||||
assert.IsType(t, new(url.Error), errors.Unwrap(httpErr))
|
assert.IsType(t, new(url.Error), errors.Unwrap(httpErr))
|
||||||
assert.IsType(t, new(url.Error), errors.Unwrap(httpErr))
|
assert.IsType(t, new(url.Error), errors.Unwrap(httpErr))
|
||||||
assert.Equal(t, httpErr.Error(), fmt.Sprintf("%s: %s", defaultErrorMessage, err.Error()))
|
assert.Equal(t, httpErr.Error(), fmt.Sprintf("%s: %s", defaultErrorMessage, err.Error()))
|
||||||
@ -26,13 +26,13 @@ func TestNewApiClientError(t *testing.T) {
|
|||||||
body := []byte(`{"errors" : ["Channel not found"]}`)
|
body := []byte(`{"errors" : ["Channel not found"]}`)
|
||||||
httpErr := NewAPIClientError(body)
|
httpErr := NewAPIClientError(body)
|
||||||
|
|
||||||
assert.IsType(t, new(httpClientError), httpErr)
|
assert.IsType(t, new(HTTPClientError), httpErr)
|
||||||
assert.Equal(t, httpErr.Error(), "Channel not found")
|
assert.Equal(t, httpErr.Error(), "Channel not found")
|
||||||
|
|
||||||
body = []byte{}
|
body = []byte{}
|
||||||
httpErr = NewAPIClientError(body)
|
httpErr = NewAPIClientError(body)
|
||||||
|
|
||||||
assert.IsType(t, new(httpClientError), httpErr)
|
assert.IsType(t, new(HTTPClientError), httpErr)
|
||||||
assert.Equal(t, httpErr.Error(), internalServerError)
|
assert.Equal(t, httpErr.Error(), internalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,10 +42,10 @@ func TestNewServerError(t *testing.T) {
|
|||||||
response.Body = io.NopCloser(bytes.NewReader(body))
|
response.Body = io.NopCloser(bytes.NewReader(body))
|
||||||
serverErr := NewServerError(response)
|
serverErr := NewServerError(response)
|
||||||
|
|
||||||
assert.IsType(t, new(httpClientError), serverErr)
|
assert.IsType(t, new(HTTPClientError), serverErr)
|
||||||
assert.Equal(t, serverErr.Error(), "Something went wrong")
|
assert.Equal(t, serverErr.Error(), "Something went wrong")
|
||||||
|
|
||||||
var err *httpClientError
|
var err *HTTPClientError
|
||||||
if errors.As(serverErr, &err) {
|
if errors.As(serverErr, &err) {
|
||||||
assert.NotNil(t, err.Response)
|
assert.NotNil(t, err.Response)
|
||||||
} else {
|
} else {
|
||||||
@ -57,7 +57,7 @@ func TestNewServerError(t *testing.T) {
|
|||||||
response.Body = io.NopCloser(bytes.NewReader(body))
|
response.Body = io.NopCloser(bytes.NewReader(body))
|
||||||
serverErr = NewServerError(response)
|
serverErr = NewServerError(response)
|
||||||
|
|
||||||
assert.IsType(t, new(httpClientError), serverErr)
|
assert.IsType(t, new(HTTPClientError), serverErr)
|
||||||
assert.Equal(t, serverErr.Error(), marshalError)
|
assert.Equal(t, serverErr.Error(), marshalError)
|
||||||
|
|
||||||
if errors.As(serverErr, &err) {
|
if errors.As(serverErr, &err) {
|
||||||
|
Loading…
Reference in New Issue
Block a user