From bb57d2c2e813720b9b43704217b3e5cd668c89ac Mon Sep 17 00:00:00 2001 From: Ruslan Efanov Date: Thu, 27 Oct 2022 09:28:07 +0300 Subject: [PATCH] fix export model and Error method --- v1/client_test.go | 6 +++--- v1/errors.go | 16 +++++++--------- v1/errors_test.go | 12 ++++++------ 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/v1/client_test.go b/v1/client_test.go index a7dc299..c0f5c9b 100644 --- a/v1/client_test.go +++ b/v1/client_test.go @@ -737,9 +737,9 @@ func (t *MGClientTest) Test_SuccessHandleError() { _, statusCode, err := client.DeactivateTransportChannel(123) t.Assert().Equal(http.StatusInternalServerError, statusCode) - t.Assert().IsType(new(httpClientError), err) + t.Assert().IsType(new(HTTPClientError), err) t.Assert().Equal(internalServerError, err.Error()) - var serverErr *httpClientError + var serverErr *HTTPClientError if errors.As(err, &serverErr) { t.Assert().Nil(serverErr.Response) } else { @@ -748,6 +748,6 @@ func (t *MGClientTest) Test_SuccessHandleError() { _, statusCode, err = client.DeactivateTransportChannel(455) 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()) } diff --git a/v1/errors.go b/v1/errors.go index 08ca6ea..d8923fd 100644 --- a/v1/errors.go +++ b/v1/errors.go @@ -17,24 +17,22 @@ type MGErrors struct { Errors []string } -type httpClientError struct { +type HTTPClientError struct { ErrorMsg string BaseError error Response io.Reader } -func (err *httpClientError) Unwrap() error { +func (err *HTTPClientError) Unwrap() error { return err.BaseError } -func (err *httpClientError) Error() string { +func (err *HTTPClientError) Error() string { message := defaultErrorMessage if err.BaseError != nil { message = fmt.Sprintf("%s: %s", defaultErrorMessage, err.BaseError.Error()) - } - - if len(err.ErrorMsg) > 0 { + } else if len(err.ErrorMsg) > 0 { message = err.ErrorMsg } @@ -42,7 +40,7 @@ func (err *httpClientError) Error() string { } func NewCriticalHTTPError(err error) error { - return &httpClientError{BaseError: err} + return &HTTPClientError{BaseError: err} } 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 { - var serverError *httpClientError + var serverError *HTTPClientError body, _ := buildLimitedRawResponse(response) err := NewAPIClientError(body) diff --git a/v1/errors_test.go b/v1/errors_test.go index 924b469..cf3ddcc 100644 --- a/v1/errors_test.go +++ b/v1/errors_test.go @@ -16,7 +16,7 @@ func TestNewCriticalHTTPError(t *testing.T) { err := &url.Error{Op: "Get", URL: "http//example.com", Err: errors.New("EOF")} 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.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"]}`) httpErr := NewAPIClientError(body) - assert.IsType(t, new(httpClientError), httpErr) + assert.IsType(t, new(HTTPClientError), httpErr) assert.Equal(t, httpErr.Error(), "Channel not found") body = []byte{} httpErr = NewAPIClientError(body) - assert.IsType(t, new(httpClientError), httpErr) + assert.IsType(t, new(HTTPClientError), httpErr) assert.Equal(t, httpErr.Error(), internalServerError) } @@ -42,10 +42,10 @@ func TestNewServerError(t *testing.T) { response.Body = io.NopCloser(bytes.NewReader(body)) serverErr := NewServerError(response) - assert.IsType(t, new(httpClientError), serverErr) + assert.IsType(t, new(HTTPClientError), serverErr) assert.Equal(t, serverErr.Error(), "Something went wrong") - var err *httpClientError + var err *HTTPClientError if errors.As(serverErr, &err) { assert.NotNil(t, err.Response) } else { @@ -57,7 +57,7 @@ func TestNewServerError(t *testing.T) { response.Body = io.NopCloser(bytes.NewReader(body)) serverErr = NewServerError(response) - assert.IsType(t, new(httpClientError), serverErr) + assert.IsType(t, new(HTTPClientError), serverErr) assert.Equal(t, serverErr.Error(), marshalError) if errors.As(serverErr, &err) {