From 987d9cd6defdefdf968566c0638b085de5404002 Mon Sep 17 00:00:00 2001 From: Ruslan Efanov Date: Tue, 25 Oct 2022 10:47:02 +0300 Subject: [PATCH] add model of error for response --- v1/client.go | 2 +- v1/client_test.go | 8 ++++++++ v1/types.go | 6 ++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/v1/client.go b/v1/client.go index c8f3c90..58210ac 100644 --- a/v1/client.go +++ b/v1/client.go @@ -674,7 +674,7 @@ func (c *MgClient) Error(info []byte) error { values := data["errors"].([]interface{}) - return errors.New(values[0].(string)) + return APIError(values[0].(string)) } // MakeTimestamp returns current unix timestamp. diff --git a/v1/client_test.go b/v1/client_test.go index 4fb112e..679bb08 100644 --- a/v1/client_test.go +++ b/v1/client_test.go @@ -678,3 +678,11 @@ func (t *MGClientTest) Test_UploadFile() { resp.CreatedAt = data.CreatedAt t.Assert().Equal(resp, data) } + +func (t *MGClientTest) Test_SuccessHandleError() { + client := t.client() + handleError := client.Error([]byte(`{"errors": ["Channel not found"]}`)) + + t.Assert().IsType(APIError(""), handleError) + t.Assert().Equal(handleError.Error(), "Channel not found") +} diff --git a/v1/types.go b/v1/types.go index 239741c..7492361 100644 --- a/v1/types.go +++ b/v1/types.go @@ -66,6 +66,12 @@ const ( OriginatorChannel ) +type APIError string + +func (err APIError) Error() string { + return string(err) +} + type ErrorType string const (