Files
openfsd/pkg/protocol/error.go
Reese Norris b899f56871 refactor: extract pkg/protocol with golden vectors
Pure wire-format package with goldens from docs/protocol.md and
differential adapters in fsd. Coverage floor for protocol package.
2026-07-16 12:35:37 -04:00

35 lines
849 B
Go

package protocol
import (
"fmt"
"io"
"strconv"
"strings"
)
// FormatError builds an FSD $ER packet matching openfsd wire format:
//
// $ERserver:unknown:%d::%s\r\n
//
// Note: openfsd uses lowercase "server", unpadded error code, and empty
// causing-parameter field — not the docs form $ERSERVER:unknown:006::...
func FormatError(code ErrorCode, message string) string {
var b strings.Builder
b.Grow(32 + len(message))
b.WriteString("$ERserver:unknown:")
b.WriteString(strconv.Itoa(int(code)))
b.WriteString("::")
b.WriteString(message)
b.WriteString("\r\n")
return b.String()
}
// WriteError writes a formatted $ER packet to w.
func WriteError(w io.Writer, code ErrorCode, message string) error {
if w == nil {
return fmt.Errorf("protocol: nil writer")
}
_, err := io.WriteString(w, FormatError(code, message))
return err
}