add ClientError model, fix using APIError
This commit is contained in:
parent
987d9cd6de
commit
0823d7e591
@ -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())
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user