ax(node): replace banned fmt import in worker.go with ProtocolError
All fmt.Errorf calls replaced with &ProtocolError{Code, Message} using
the package-native error type, removing the banned fmt import entirely.
Co-Authored-By: Charon <charon@lethean.io>
This commit is contained in:
parent
82781c6f95
commit
485411912a
1 changed files with 18 additions and 19 deletions
|
|
@ -2,7 +2,6 @@ package node
|
|||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
|
|
@ -127,7 +126,7 @@ func (worker *Worker) HandleMessage(conn *PeerConnection, msg *Message) {
|
|||
func (worker *Worker) handlePing(msg *Message) (*Message, error) {
|
||||
var ping PingPayload
|
||||
if err := msg.ParsePayload(&ping); err != nil {
|
||||
return nil, fmt.Errorf("invalid ping payload: %w", err)
|
||||
return nil, &ProtocolError{Code: ErrCodeInvalidMessage, Message: "invalid ping payload: " + err.Error()}
|
||||
}
|
||||
|
||||
pong := PongPayload{
|
||||
|
|
@ -142,7 +141,7 @@ func (worker *Worker) handlePing(msg *Message) (*Message, error) {
|
|||
func (worker *Worker) handleGetStats(msg *Message) (*Message, error) {
|
||||
identity := worker.node.GetIdentity()
|
||||
if identity == nil {
|
||||
return nil, fmt.Errorf("node identity not initialized")
|
||||
return nil, &ProtocolError{Code: ErrCodeUnauthorized, Message: "node identity not initialized"}
|
||||
}
|
||||
|
||||
stats := StatsPayload{
|
||||
|
|
@ -205,17 +204,17 @@ func convertMinerStats(miner MinerInstance, rawStats interface{}) MinerStatsItem
|
|||
// response, err := worker.handleStartMiner(message)
|
||||
func (worker *Worker) handleStartMiner(msg *Message) (*Message, error) {
|
||||
if worker.minerManager == nil {
|
||||
return nil, fmt.Errorf("miner manager not configured")
|
||||
return nil, &ProtocolError{Code: ErrCodeOperationFailed, Message: "miner manager not configured"}
|
||||
}
|
||||
|
||||
var payload StartMinerPayload
|
||||
if err := msg.ParsePayload(&payload); err != nil {
|
||||
return nil, fmt.Errorf("invalid start miner payload: %w", err)
|
||||
return nil, &ProtocolError{Code: ErrCodeInvalidMessage, Message: "invalid start miner payload: " + err.Error()}
|
||||
}
|
||||
|
||||
// Validate miner type is provided
|
||||
if payload.MinerType == "" {
|
||||
return nil, fmt.Errorf("miner type is required")
|
||||
return nil, &ProtocolError{Code: ErrCodeInvalidMessage, Message: "miner type is required"}
|
||||
}
|
||||
|
||||
// Get the config from the profile or use the override
|
||||
|
|
@ -225,11 +224,11 @@ func (worker *Worker) handleStartMiner(msg *Message) (*Message, error) {
|
|||
} else if worker.profileManager != nil {
|
||||
profile, err := worker.profileManager.GetProfile(payload.ProfileID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("profile not found: %s", payload.ProfileID)
|
||||
return nil, &ProtocolError{Code: ErrCodeNotFound, Message: "profile not found: " + payload.ProfileID}
|
||||
}
|
||||
config = profile
|
||||
} else {
|
||||
return nil, fmt.Errorf("no config provided and no profile manager configured")
|
||||
return nil, &ProtocolError{Code: ErrCodeOperationFailed, Message: "no config provided and no profile manager configured"}
|
||||
}
|
||||
|
||||
// Start the miner
|
||||
|
|
@ -252,12 +251,12 @@ func (worker *Worker) handleStartMiner(msg *Message) (*Message, error) {
|
|||
// response, err := worker.handleStopMiner(message)
|
||||
func (worker *Worker) handleStopMiner(msg *Message) (*Message, error) {
|
||||
if worker.minerManager == nil {
|
||||
return nil, fmt.Errorf("miner manager not configured")
|
||||
return nil, &ProtocolError{Code: ErrCodeOperationFailed, Message: "miner manager not configured"}
|
||||
}
|
||||
|
||||
var payload StopMinerPayload
|
||||
if err := msg.ParsePayload(&payload); err != nil {
|
||||
return nil, fmt.Errorf("invalid stop miner payload: %w", err)
|
||||
return nil, &ProtocolError{Code: ErrCodeInvalidMessage, Message: "invalid stop miner payload: " + err.Error()}
|
||||
}
|
||||
|
||||
err := worker.minerManager.StopMiner(payload.MinerName)
|
||||
|
|
@ -275,12 +274,12 @@ func (worker *Worker) handleStopMiner(msg *Message) (*Message, error) {
|
|||
// response, err := worker.handleGetLogs(message)
|
||||
func (worker *Worker) handleGetLogs(msg *Message) (*Message, error) {
|
||||
if worker.minerManager == nil {
|
||||
return nil, fmt.Errorf("miner manager not configured")
|
||||
return nil, &ProtocolError{Code: ErrCodeOperationFailed, Message: "miner manager not configured"}
|
||||
}
|
||||
|
||||
var payload GetLogsPayload
|
||||
if err := msg.ParsePayload(&payload); err != nil {
|
||||
return nil, fmt.Errorf("invalid get logs payload: %w", err)
|
||||
return nil, &ProtocolError{Code: ErrCodeInvalidMessage, Message: "invalid get logs payload: " + err.Error()}
|
||||
}
|
||||
|
||||
// Validate and limit the Lines parameter to prevent resource exhaustion
|
||||
|
|
@ -291,7 +290,7 @@ func (worker *Worker) handleGetLogs(msg *Message) (*Message, error) {
|
|||
|
||||
miner, err := worker.minerManager.GetMiner(payload.MinerName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("miner not found: %s", payload.MinerName)
|
||||
return nil, &ProtocolError{Code: ErrCodeNotFound, Message: "miner not found: " + payload.MinerName}
|
||||
}
|
||||
|
||||
lines := miner.GetConsoleHistory(payload.Lines)
|
||||
|
|
@ -309,7 +308,7 @@ func (worker *Worker) handleGetLogs(msg *Message) (*Message, error) {
|
|||
func (worker *Worker) handleDeploy(conn *PeerConnection, msg *Message) (*Message, error) {
|
||||
var payload DeployPayload
|
||||
if err := msg.ParsePayload(&payload); err != nil {
|
||||
return nil, fmt.Errorf("invalid deploy payload: %w", err)
|
||||
return nil, &ProtocolError{Code: ErrCodeInvalidMessage, Message: "invalid deploy payload: " + err.Error()}
|
||||
}
|
||||
|
||||
// Reconstruct Bundle object from payload
|
||||
|
|
@ -329,19 +328,19 @@ func (worker *Worker) handleDeploy(conn *PeerConnection, msg *Message) (*Message
|
|||
switch bundle.Type {
|
||||
case BundleProfile:
|
||||
if worker.profileManager == nil {
|
||||
return nil, fmt.Errorf("profile manager not configured")
|
||||
return nil, &ProtocolError{Code: ErrCodeOperationFailed, Message: "profile manager not configured"}
|
||||
}
|
||||
|
||||
// Decrypt and extract profile data
|
||||
profileData, err := ExtractProfileBundle(bundle, password)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to extract profile bundle: %w", err)
|
||||
return nil, &ProtocolError{Code: ErrCodeOperationFailed, Message: "failed to extract profile bundle: " + err.Error()}
|
||||
}
|
||||
|
||||
// Unmarshal into interface{} to pass to ProfileManager
|
||||
var profile interface{}
|
||||
if err := UnmarshalJSON(profileData, &profile); err != nil {
|
||||
return nil, fmt.Errorf("invalid profile data JSON: %w", err)
|
||||
return nil, &ProtocolError{Code: ErrCodeInvalidMessage, Message: "invalid profile data JSON: " + err.Error()}
|
||||
}
|
||||
|
||||
if err := worker.profileManager.SaveProfile(profile); err != nil {
|
||||
|
|
@ -374,7 +373,7 @@ func (worker *Worker) handleDeploy(conn *PeerConnection, msg *Message) (*Message
|
|||
// Extract miner bundle
|
||||
minerPath, profileData, err := ExtractMinerBundle(bundle, password, installDir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to extract miner bundle: %w", err)
|
||||
return nil, &ProtocolError{Code: ErrCodeOperationFailed, Message: "failed to extract miner bundle: " + err.Error()}
|
||||
}
|
||||
|
||||
// If the bundle contained a profile config, save it
|
||||
|
|
@ -404,7 +403,7 @@ func (worker *Worker) handleDeploy(conn *PeerConnection, msg *Message) (*Message
|
|||
return msg.Reply(MsgDeployAck, ack)
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown bundle type: %s", payload.BundleType)
|
||||
return nil, &ProtocolError{Code: ErrCodeInvalidMessage, Message: "unknown bundle type: " + payload.BundleType}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue