-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
90 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,71 @@ | ||
package nodes | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/bacalhau-project/bacalhau/pkg/bacerrors" | ||
) | ||
|
||
const errComponent = "NodesManager" | ||
|
||
const ( | ||
MultipleNodesFound bacerrors.ErrorCode = "MultipleNodesFound" | ||
ConflictNodeState bacerrors.ErrorCode = "ConflictNodeState" | ||
HandshakeRequired bacerrors.ErrorCode = "HandshakeRequired" | ||
ConcurrentUpdate bacerrors.ErrorCode = "ConcurrentUpdate" | ||
) | ||
|
||
// ErrNodeNotFound is returned when nodeInfo was not found for a requested node id | ||
type ErrNodeNotFound struct { | ||
nodeID string | ||
// NewErrNodeNotFound returns a standardized error for when a node is not found | ||
func NewErrNodeNotFound(nodeID string) bacerrors.Error { | ||
return bacerrors.New("node not found: %s", nodeID). | ||
WithCode(bacerrors.NotFoundError). | ||
WithComponent(errComponent) | ||
} | ||
|
||
func NewErrNodeNotFound(nodeID string) ErrNodeNotFound { | ||
return ErrNodeNotFound{nodeID: nodeID} | ||
// NewErrMultipleNodesFound returns a standardized error for when multiple nodes match a prefix | ||
func NewErrMultipleNodesFound(nodeIDPrefix string, matchingNodeIDs []string) bacerrors.Error { | ||
if len(matchingNodeIDs) > 3 { | ||
matchingNodeIDs = matchingNodeIDs[:3] | ||
matchingNodeIDs = append(matchingNodeIDs, "...") | ||
} | ||
return bacerrors.New("multiple nodes found for prefix: %s, matching IDs: %v", nodeIDPrefix, matchingNodeIDs). | ||
WithCode(MultipleNodesFound). | ||
WithHTTPStatusCode(http.StatusConflict). | ||
WithComponent(errComponent). | ||
WithHint("Use a more specific node ID prefix") | ||
} | ||
|
||
func (e ErrNodeNotFound) Error() string { | ||
return fmt.Errorf("nodeInfo not found for nodeID: %s", e.nodeID).Error() | ||
// NewErrHandshakeRequired returns a standardized error for when a handshake is required | ||
func NewErrHandshakeRequired(nodeID string) bacerrors.Error { | ||
return bacerrors.New("node %s not connected - handshake required", nodeID). | ||
WithCode(HandshakeRequired). | ||
WithComponent(errComponent). | ||
WithHTTPStatusCode(http.StatusPreconditionRequired). | ||
WithRetryable() | ||
} | ||
|
||
// ErrMultipleNodesFound is returned when multiple nodes were found for a requested node id prefix | ||
type ErrMultipleNodesFound struct { | ||
nodeIDPrefix string | ||
matchingNodeIDs []string | ||
// NewErrNodeAlreadyApproved returns a standardized error for when a node is already approved | ||
func NewErrNodeAlreadyApproved(nodeID string) bacerrors.Error { | ||
return bacerrors.New("node %s already approved", nodeID). | ||
WithCode(ConflictNodeState). | ||
WithHTTPStatusCode(http.StatusConflict). | ||
WithComponent(errComponent) | ||
} | ||
|
||
func NewErrMultipleNodesFound(nodeIDPrefix string, matchingNodeIDs []string) ErrMultipleNodesFound { | ||
if len(matchingNodeIDs) > 3 { | ||
matchingNodeIDs = append(matchingNodeIDs[:3], "...") | ||
} | ||
return ErrMultipleNodesFound{nodeIDPrefix: nodeIDPrefix, matchingNodeIDs: matchingNodeIDs} | ||
// NewErrNodeAlreadyRejected returns a standardized error for when a node is already rejected | ||
func NewErrNodeAlreadyRejected(nodeID string) bacerrors.Error { | ||
return bacerrors.New("node %s already rejected", nodeID). | ||
WithCode(ConflictNodeState). | ||
WithHTTPStatusCode(http.StatusConflict). | ||
WithComponent(errComponent) | ||
} | ||
|
||
func (e ErrMultipleNodesFound) Error() string { | ||
return fmt.Errorf("multiple nodes found for nodeID prefix: %s, matching nodeIDs: %v", e.nodeIDPrefix, e.matchingNodeIDs).Error() | ||
// NewErrConcurrentModification returns a standardized error for concurrent update conflicts | ||
func NewErrConcurrentModification() bacerrors.Error { | ||
return bacerrors.New("concurrent modification detected"). | ||
WithCode(ConcurrentUpdate). | ||
WithHTTPStatusCode(http.StatusConflict). | ||
WithComponent(errComponent). | ||
WithRetryable(). | ||
WithHint("Request should be retried") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters