From bcf5b2aa0e45a56da8284b6211be0414d45a8773 Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Sun, 17 Sep 2023 14:57:14 -0300 Subject: [PATCH] chore: returning errors --- messages/getAllMessagesByIndex.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/messages/getAllMessagesByIndex.go b/messages/getAllMessagesByIndex.go index e845d4d..9226052 100644 --- a/messages/getAllMessagesByIndex.go +++ b/messages/getAllMessagesByIndex.go @@ -16,7 +16,7 @@ type Message struct { } // Get all messages available on the node by a given index. -func GetAllMessagesByIndex(nodeUrl string, index string) []Message { +func GetAllMessagesByIndex(nodeUrl string, index string) ([]Message, error) { node := iotago.NewNodeHTTPAPIClient(nodeUrl) msgIdsResponse, err := node.MessageIDsByIndex( @@ -25,7 +25,7 @@ func GetAllMessagesByIndex(nodeUrl string, index string) []Message { ) if err != nil { - log.Fatal("Unable to get message IDs.") + return nil, errors.New("unable to get message IDs") } var i uint32 @@ -35,20 +35,20 @@ func GetAllMessagesByIndex(nodeUrl string, index string) []Message { for i = 0; i < msgIdsResponse.Count; i++ { messageId, err := iotago.MessageIDFromHexString(msgIdsResponse.MessageIDs[i]) if err != nil { - log.Fatal(err) + return nil, errors.New("unable to convert message ID from hex to message ID representation") } messageReturned, err := node.MessageByMessageID(context.Background(), messageId) if err != nil { - log.Fatal(err) + return nil, errors.New("unable to get message by given message ID") } message, err := formatMessagePayload(*messageReturned, index) if err != nil { log.Println(err) - message = Message { - Index: "Error", + message = Message{ + Index: "Error", Content: err.Error(), } } @@ -59,7 +59,7 @@ func GetAllMessagesByIndex(nodeUrl string, index string) []Message { log.Println("No messages with this index were found.") } - return messages + return messages, nil } // Formats the message payload into a custom message type.