Skip to content

Commit

Permalink
added more error handling and bubbled up to the tryWorker level
Browse files Browse the repository at this point in the history
  • Loading branch information
restevens402 committed Aug 7, 2024
1 parent a3b5c95 commit cb0b78d
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions pkg/workers/send_work.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package workers

import (
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -106,7 +107,13 @@ func tryWorker(node *masa.OracleNode, worker Worker, message *messages.Work, res
case <-workerDone:
select {
case response := <-responseCollector:
if isSuccessfulResponse(response) {
success, err := isSuccessfulResponse(response)
if err != nil {
// TODO: This is the original error from the worker message. We can decide how we want to handle this
logrus.Errorf("[-] Error in response: %v", err)
return false
}
if success {
if worker.IsLocal {
logrus.Infof("Local worker with PeerID %s successfully completed the work", node.Host.ID())
} else {
Expand Down Expand Up @@ -241,16 +248,23 @@ func processWorkerResponse(result interface{}, workerID interface{}, responseCol
responseCollector <- msg
}

func isSuccessfulResponse(response *pubsub2.Message) bool {
func isSuccessfulResponse(response *pubsub2.Message) (bool, error) {
if response.ValidatorData == nil {
return true
return true, nil
}
validatorData, ok := response.ValidatorData.(map[string]interface{})
if !ok {
return false
return false, nil
}
// TODO: this code is unwrapping the error returned from the worker. We can improve on how we use this in the result
if responseVal, exists := validatorData["Response"]; exists {
if responseMap, ok := responseVal.(map[string]interface{}); ok {
if errorVal, exists := responseMap["error"]; exists {
return false, errors.New(errorVal.(string))
}
}
}
errorVal, exists := validatorData["error"]
return !exists || errorVal == nil
return true, nil
}

func processAndSendResponse(response *pubsub2.Message) {
Expand Down

0 comments on commit cb0b78d

Please sign in to comment.