From 0823d7e5914625069a86c0fb4e7353e6bdd9943a Mon Sep 17 00:00:00 2001 From: Ruslan Efanov Date: Tue, 25 Oct 2022 12:38:51 +0300 Subject: [PATCH] add ClientError model, fix using APIError --- v1/client_test.go | 13 ++++++++++++- v1/request.go | 4 ++-- v1/types.go | 8 ++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/v1/client_test.go b/v1/client_test.go index 679bb08..b4ab042 100644 --- a/v1/client_test.go +++ b/v1/client_test.go @@ -679,10 +679,21 @@ func (t *MGClientTest) Test_UploadFile() { t.Assert().Equal(resp, data) } -func (t *MGClientTest) Test_SuccessHandleError() { +func (t *MGClientTest) Test_SuccessHandleAPIError() { client := t.client() handleError := client.Error([]byte(`{"errors": ["Channel not found"]}`)) t.Assert().IsType(APIError(""), handleError) t.Assert().Equal(handleError.Error(), "Channel not found") + + defer gock.Off() + t.gock(). + Delete(t.transportURL("channels/123")). + Reply(http.StatusInternalServerError) + + _, statusCode, err := client.DeactivateTransportChannel(123) + + t.Assert().Equal(http.StatusInternalServerError, statusCode) + t.Assert().IsType(APIError(""), err) + t.Assert().Equal("http request error. status code: 500", err.Error()) } diff --git a/v1/request.go b/v1/request.go index 82fc545..9941291 100644 --- a/v1/request.go +++ b/v1/request.go @@ -71,11 +71,11 @@ func makeRequest(reqType, url string, buf io.Reader, c *MgClient) ([]byte, int, resp, err := c.httpClient.Do(req) if err != nil { - return res, 0, err + return res, 0, ClientError(err.Error()) } if resp.StatusCode >= http.StatusInternalServerError { - err = fmt.Errorf("http request error. status code: %d", resp.StatusCode) + err = APIError(fmt.Sprintf("http request error. status code: %d", resp.StatusCode)) return res, resp.StatusCode, err } diff --git a/v1/types.go b/v1/types.go index 7492361..9504b23 100644 --- a/v1/types.go +++ b/v1/types.go @@ -66,6 +66,14 @@ const ( OriginatorChannel ) +// ClientError is error of http-client or network. +type ClientError string + +func (err ClientError) Error() string { + return string(err) +} + +// APIError is error from MG. type APIError string func (err APIError) Error() string {