1
0
mirror of synced 2024-11-22 04:56:06 +03:00

add ClientError model, fix using APIError

This commit is contained in:
Ruslan Efanov 2022-10-25 12:38:51 +03:00
parent 987d9cd6de
commit 0823d7e591
3 changed files with 22 additions and 3 deletions

View File

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

View File

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

View File

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