Skip to content

Commit

Permalink
Added better error handling when mirror node returns non json respons…
Browse files Browse the repository at this point in the history
…es (#898)

Signed-off-by: Antonio Mindov <[email protected]>
  • Loading branch information
rokn authored Jun 5, 2023
1 parent 8c6b157 commit 9101ccb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/clients/hedera/mirror-node/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
39 changes: 39 additions & 0 deletions app/clients/hedera/mirror-node/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}

0 comments on commit 9101ccb

Please sign in to comment.