1
0
mirror of synced 2024-11-21 20:46:05 +03:00

fix export model and Error method

This commit is contained in:
Ruslan Efanov 2022-10-27 09:28:07 +03:00
parent 5e3e7cd03d
commit bb57d2c2e8
3 changed files with 16 additions and 18 deletions

View File

@ -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())
}

View File

@ -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)

View File

@ -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) {