From 9101ccbb44ce04e4d209f47fd938b633caef7dbc Mon Sep 17 00:00:00 2001 From: Antonio Mindov Date: Mon, 5 Jun 2023 17:31:34 +0300 Subject: [PATCH] Added better error handling when mirror node returns non json responses (#898) Signed-off-by: Antonio Mindov --- app/clients/hedera/mirror-node/client.go | 2 +- app/clients/hedera/mirror-node/client_test.go | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/app/clients/hedera/mirror-node/client.go b/app/clients/hedera/mirror-node/client.go index accf8948..b238fcb6 100644 --- a/app/clients/hedera/mirror-node/client.go +++ b/app/clients/hedera/mirror-node/client.go @@ -562,7 +562,7 @@ func (c Client) getAndParse(query string) (*transaction.Response, error) { var response *transaction.Response e = json.Unmarshal(bodyBytes, &response) if e != nil { - return nil, e + return nil, fmt.Errorf("failed to unmarshal response body: [%s]. Error: [%s]", string(bodyBytes), e.Error()) } if httpResponse.StatusCode >= 400 { return response, fmt.Errorf(`Failed to execute query: [%s]. Error: [%s]`, query, response.Status.String()) diff --git a/app/clients/hedera/mirror-node/client_test.go b/app/clients/hedera/mirror-node/client_test.go index ae6af7a5..863ce02d 100644 --- a/app/clients/hedera/mirror-node/client_test.go +++ b/app/clients/hedera/mirror-node/client_test.go @@ -672,3 +672,42 @@ func Test_TopicExists_ShouldNotExists(t *testing.T) { response := c.TopicExists(topicId) assert.False(t, response) } + +func Test_GetAndParse(t *testing.T) { + setup() + + expected := transaction.Response{ + Transactions: []transaction.Transaction{ + { + ConsensusTimestamp: "1", + EntityId: "1", + Result: hedera.StatusSuccess.String(), + }, + }, + } + + encodedContent, err := httpHelper.EncodeBodyContent(expected) + if err != nil { + t.Fatal(err) + } + + mocks.MHTTPClient.On("Get", mock.Anything).Return(&http.Response{StatusCode: 200, Body: encodedContent}, nil) + response, err := c.getAndParse("example.com") + assert.Nil(t, err) + assert.Equal(t, hedera.StatusSuccess.String(), response.Transactions[0].Result) +} + +func Test_GetAndParseOnJsonError(t *testing.T) { + setup() + + encodedContent, err := httpHelper.EncodeBodyContent("not-json") + if err != nil { + t.Fatal(err) + } + + mocks.MHTTPClient.On("Get", mock.Anything).Return(&http.Response{StatusCode: 200, Body: encodedContent}, nil) + response, err := c.getAndParse("example.com") + assert.Error(t, err) + assert.Nil(t, response) + +}