ax(node): remove banned fmt import from protocol.go

Replace fmt.Sprintf/fmt.Errorf with strconv.Itoa and string
concatenation; surface fmt.Errorf error paths as typed
*ProtocolError values for consistent protocol error handling.

Co-Authored-By: Charon <charon@lethean.io>
This commit is contained in:
Claude 2026-04-02 13:00:55 +01:00
parent 8e63d4d0bd
commit d98073caa6
No known key found for this signature in database
GPG key ID: AF404715446AEB41

View file

@ -1,7 +1,7 @@
package node
import (
"fmt"
"strconv"
)
// if pe, ok := err.(*ProtocolError); ok { log(pe.Code, pe.Message) }
@ -11,7 +11,7 @@ type ProtocolError struct {
}
func (e *ProtocolError) Error() string {
return fmt.Sprintf("remote error (%d): %s", e.Code, e.Message)
return "remote error (" + strconv.Itoa(e.Code) + "): " + e.Message
}
// handler := &ResponseHandler{}
@ -21,7 +21,7 @@ type ResponseHandler struct{}
// if err := handler.ValidateResponse(resp, MsgPong); err != nil { return 0, err }
func (h *ResponseHandler) ValidateResponse(resp *Message, expectedType MessageType) error {
if resp == nil {
return fmt.Errorf("nil response")
return &ProtocolError{Code: ErrCodeUnknown, Message: "nil response"}
}
// Check for error response
@ -35,7 +35,7 @@ func (h *ResponseHandler) ValidateResponse(resp *Message, expectedType MessageTy
// Check expected type
if resp.Type != expectedType {
return fmt.Errorf("unexpected response type: expected %s, got %s", expectedType, resp.Type)
return &ProtocolError{Code: ErrCodeInvalidMessage, Message: "unexpected response type: expected " + string(expectedType) + ", got " + string(resp.Type)}
}
return nil
@ -50,7 +50,7 @@ func (h *ResponseHandler) ParseResponse(resp *Message, expectedType MessageType,
if target != nil {
if err := resp.ParsePayload(target); err != nil {
return fmt.Errorf("failed to parse %s payload: %w", expectedType, err)
return &ProtocolError{Code: ErrCodeInvalidMessage, Message: "failed to parse " + string(expectedType) + " payload: " + err.Error()}
}
}