Skip to content

Commit

Permalink
chore: returning errors
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanCapistrano committed Sep 17, 2023
1 parent 7d81ef0 commit bcf5b2a
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions messages/getAllMessagesByIndex.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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(),
}
}
Expand All @@ -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.
Expand Down

0 comments on commit bcf5b2a

Please sign in to comment.