1
0
mirror of synced 2024-11-28 16:06:04 +03:00

fix review

This commit is contained in:
Ruslan Efanov 2022-10-26 18:07:04 +03:00
parent 8765026f7e
commit 5e3e7cd03d
5 changed files with 54 additions and 53 deletions

View File

@ -71,7 +71,7 @@ func (c *MgClient) TransportTemplates() ([]Template, int, error) {
} }
if status > http.StatusCreated || status < http.StatusOK { if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data) return resp, status, NewAPIClientError(data)
} }
return resp, status, err return resp, status, err
@ -116,7 +116,7 @@ func (c *MgClient) ActivateTemplate(channelID uint64, request ActivateTemplateRe
} }
if status > http.StatusCreated || status < http.StatusOK { if status > http.StatusCreated || status < http.StatusOK {
return status, c.Error(data) return status, NewAPIClientError(data)
} }
return status, err return status, err
@ -165,7 +165,7 @@ func (c *MgClient) UpdateTemplate(request Template) (int, error) {
} }
if status != http.StatusOK { if status != http.StatusOK {
return status, c.Error(data) return status, NewAPIClientError(data)
} }
return status, err return status, err
@ -190,7 +190,7 @@ func (c *MgClient) DeactivateTemplate(channelID uint64, templateCode string) (in
} }
if status > http.StatusCreated || status < http.StatusOK { if status > http.StatusCreated || status < http.StatusOK {
return status, c.Error(data) return status, NewAPIClientError(data)
} }
return status, err return status, err
@ -224,7 +224,7 @@ func (c *MgClient) TransportChannels(request Channels) ([]ChannelListItem, int,
} }
if status > http.StatusCreated || status < http.StatusOK { if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data) return resp, status, NewAPIClientError(data)
} }
return resp, status, err return resp, status, err
@ -283,7 +283,7 @@ func (c *MgClient) ActivateTransportChannel(request Channel) (ActivateResponse,
} }
if status > http.StatusCreated || status < http.StatusOK { if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data) return resp, status, NewAPIClientError(data)
} }
return resp, status, err return resp, status, err
@ -342,7 +342,7 @@ func (c *MgClient) UpdateTransportChannel(request Channel) (UpdateResponse, int,
} }
if status != http.StatusOK { if status != http.StatusOK {
return resp, status, c.Error(data) return resp, status, NewAPIClientError(data)
} }
return resp, status, err return resp, status, err
@ -378,7 +378,7 @@ func (c *MgClient) DeactivateTransportChannel(id uint64) (DeleteResponse, int, e
} }
if status != http.StatusOK { if status != http.StatusOK {
return resp, status, c.Error(data) return resp, status, NewAPIClientError(data)
} }
return resp, status, err return resp, status, err
@ -427,7 +427,7 @@ func (c *MgClient) Messages(request SendData) (MessagesResponse, int, error) {
} }
if status != http.StatusOK { if status != http.StatusOK {
return resp, status, c.Error(data) return resp, status, NewAPIClientError(data)
} }
return resp, status, err return resp, status, err
@ -471,7 +471,7 @@ func (c *MgClient) UpdateMessages(request EditMessageRequest) (MessagesResponse,
} }
if status != http.StatusOK { if status != http.StatusOK {
return resp, status, c.Error(data) return resp, status, NewAPIClientError(data)
} }
return resp, status, err return resp, status, err
@ -510,7 +510,7 @@ func (c *MgClient) MarkMessageRead(request MarkMessageReadRequest) (MarkMessageR
} }
if status != http.StatusOK { if status != http.StatusOK {
return resp, status, c.Error(data) return resp, status, NewAPIClientError(data)
} }
return resp, status, err return resp, status, err
@ -541,7 +541,7 @@ func (c *MgClient) AckMessage(request AckMessageRequest) (int, error) {
} }
if status != http.StatusOK { if status != http.StatusOK {
return status, c.Error(data) return status, NewAPIClientError(data)
} }
return status, err return status, err
@ -579,7 +579,7 @@ func (c *MgClient) DeleteMessage(request DeleteData) (*MessagesResponse, int, er
return nil, status, err return nil, status, err
} }
if status != http.StatusOK { if status != http.StatusOK {
return nil, status, c.Error(data) return nil, status, NewAPIClientError(data)
} }
var previousChatMessage *MessagesResponse var previousChatMessage *MessagesResponse
@ -618,7 +618,7 @@ func (c *MgClient) GetFile(request string) (FullFileResponse, int, error) {
} }
if status != http.StatusOK { if status != http.StatusOK {
return resp, status, c.Error(data) return resp, status, NewAPIClientError(data)
} }
return resp, status, err return resp, status, err
@ -638,7 +638,7 @@ func (c *MgClient) UploadFile(request io.Reader) (UploadFileResponse, int, error
} }
if status != http.StatusOK { if status != http.StatusOK {
return resp, status, c.Error(data) return resp, status, NewAPIClientError(data)
} }
return resp, status, err return resp, status, err
@ -659,16 +659,12 @@ func (c *MgClient) UploadFileByURL(request UploadFileByUrlRequest) (UploadFileRe
} }
if status != http.StatusOK { if status != http.StatusOK {
return resp, status, c.Error(data) return resp, status, NewAPIClientError(data)
} }
return resp, status, err return resp, status, err
} }
func (c *MgClient) Error(info []byte) error {
return NewAPIClientError(info)
}
// MakeTimestamp returns current unix timestamp. // MakeTimestamp returns current unix timestamp.
func MakeTimestamp() int64 { func MakeTimestamp() int64 {
return time.Now().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond)) return time.Now().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))

View File

@ -723,10 +723,6 @@ func (t *MGClientTest) Test_UploadFile() {
func (t *MGClientTest) Test_SuccessHandleError() { func (t *MGClientTest) Test_SuccessHandleError() {
client := t.client() client := t.client()
json := `{"errors": ["Channel not found"]}` json := `{"errors": ["Channel not found"]}`
handleError := client.Error([]byte(json))
t.Assert().IsType(new(httpClientError), handleError)
t.Assert().Equal(handleError.Error(), "Channel not found")
defer gock.Off() defer gock.Off()
t.gock(). t.gock().
@ -745,7 +741,7 @@ func (t *MGClientTest) Test_SuccessHandleError() {
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().NotNil(serverErr.LimitedResponse) t.Assert().Nil(serverErr.Response)
} else { } else {
t.Fail("Unexpected type of error") t.Fail("Unexpected type of error")
} }

View File

@ -9,28 +9,29 @@ import (
"net/http" "net/http"
) )
var defaultErrorMessage = "internal http client error" var defaultErrorMessage = "http client error"
var internalServerError = "internal server error" var internalServerError = "internal server error"
var marshalError = "cannot unmarshal response body"
type MGErrors struct {
Errors []string
}
type httpClientError struct { type httpClientError struct {
ErrorMsg string ErrorMsg string
BaseError error BaseError error
LimitedResponse io.Reader Response io.Reader
} }
func (err *httpClientError) Unwrap() error { func (err *httpClientError) Unwrap() error {
return err.BaseError return err.BaseError
} }
func (err *httpClientError) Is(target error) bool {
return errors.As(target, &err)
}
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())
} }
if len(err.ErrorMsg) > 0 { if len(err.ErrorMsg) > 0 {
@ -45,35 +46,32 @@ func NewCriticalHTTPError(err error) error {
} }
func NewAPIClientError(responseBody []byte) error { func NewAPIClientError(responseBody []byte) error {
var data map[string]interface{} var data MGErrors
var message string var message string
if len(responseBody) == 0 { if len(responseBody) == 0 {
message = internalServerError message = internalServerError
} else { } else {
if err := json.Unmarshal(responseBody, &data); err != nil { err := json.Unmarshal(responseBody, &data)
return err
}
values := data["errors"].([]interface{}) if err != nil {
message = values[0].(string) message = marshalError
} else if len(data.Errors) > 0 {
message = data.Errors[0]
}
} }
return &httpClientError{ErrorMsg: message} return &httpClientError{ErrorMsg: message}
} }
func NewServerError(response *http.Response) error { func NewServerError(response *http.Response) error {
var data []byte
body, err := buildLimitedRawResponse(response)
if err == nil {
data = body
}
err = NewAPIClientError(data)
var serverError *httpClientError var serverError *httpClientError
if errors.As(err, &serverError) { body, _ := buildLimitedRawResponse(response)
serverError.LimitedResponse = bytes.NewBuffer(body) err := NewAPIClientError(body)
if errors.As(err, &serverError) && len(body) > 0 {
serverError.Response = bytes.NewBuffer(body)
return serverError return serverError
} }

View File

@ -19,7 +19,7 @@ func TestNewCriticalHTTPError(t *testing.T) {
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()))
} }
func TestNewApiClientError(t *testing.T) { func TestNewApiClientError(t *testing.T) {
@ -47,8 +47,20 @@ func TestNewServerError(t *testing.T) {
var err *httpClientError var err *httpClientError
if errors.As(serverErr, &err) { if errors.As(serverErr, &err) {
assert.NotNil(t, err.LimitedResponse) assert.NotNil(t, err.Response)
} else { } else {
t.Fatal("Unexpected type of error") t.Fatal("Unexpected type of error")
} }
body = []byte(`{"invalid_json"`)
response = new(http.Response)
response.Body = io.NopCloser(bytes.NewReader(body))
serverErr = NewServerError(response)
assert.IsType(t, new(httpClientError), serverErr)
assert.Equal(t, serverErr.Error(), marshalError)
if errors.As(serverErr, &err) {
assert.NotNil(t, err.Response)
}
} }

View File

@ -6,12 +6,11 @@ import (
) )
const MB = 1 << 20 const MB = 1 << 20
const MaxSizeBody = MB * 0.5
func buildLimitedRawResponse(resp *http.Response) ([]byte, error) { func buildLimitedRawResponse(resp *http.Response) ([]byte, error) {
defer resp.Body.Close() defer resp.Body.Close()
limitReader := io.LimitReader(resp.Body, MaxSizeBody) limitReader := io.LimitReader(resp.Body, MB)
body, err := io.ReadAll(limitReader) body, err := io.ReadAll(limitReader)
if err != nil { if err != nil {