// Package output renders results and errors for agents: compact text by // default, JSON on request, and stable exit codes. package output import ( "encoding/json" "errors" "fmt" "io" "net/url" "connectrpc.com/connect" "tangled.org/beardic.us/chatline/internal/config" ) // Exit codes. See docs/spec.md section 5.2. const ( ExitOK = 0 ExitAPI = 1 ExitAuth = 2 ExitConfig = 3 ExitUsage = 4 ExitTimeout = 5 ExitPermission = 6 ExitNotFound = 7 ) // Error is a classified error with a snake_case code and an exit status. type Error struct { Code string Detail string Exit int Err error } func (e *Error) Error() string { return e.Code + ": " + e.Detail } func (e *Error) Unwrap() error { return e.Err } // New returns a classified error. func New(exit int, code, format string, args ...any) *Error { return &Error{Code: code, Detail: fmt.Sprintf(format, args...), Exit: exit} } // Usage returns a usage error (exit 4). func Usage(code, format string, args ...any) *Error { return New(ExitUsage, code, format, args...) } // Classify maps any error to an *Error, using Connect codes for API errors. func Classify(err error) *Error { if err == nil { return nil } var e *Error if errors.As(err, &e) { return e } var ce *config.Error if errors.As(err, &ce) { return &Error{Code: "config_error", Detail: ce.Msg, Exit: ExitConfig, Err: err} } var cerr *connect.Error if errors.As(err, &cerr) { detail := cerr.Message() if detail == "" { detail = cerr.Code().String() } switch cerr.Code() { case connect.CodeUnauthenticated: return &Error{Code: "unauthenticated", Detail: detail, Exit: ExitAuth, Err: err} case connect.CodePermissionDenied: return &Error{Code: "permission_denied", Detail: detail, Exit: ExitPermission, Err: err} case connect.CodeNotFound: return &Error{Code: "not_found", Detail: detail, Exit: ExitNotFound, Err: err} case connect.CodeInvalidArgument: return &Error{Code: "invalid_argument", Detail: detail, Exit: ExitAPI, Err: err} case connect.CodeFailedPrecondition: return &Error{Code: "failed_precondition", Detail: detail, Exit: ExitAPI, Err: err} case connect.CodeDeadlineExceeded: return &Error{Code: "timeout", Detail: detail, Exit: ExitTimeout, Err: err} case connect.CodeUnavailable: return &Error{Code: "unavailable", Detail: detail, Exit: ExitAPI, Err: err} default: return &Error{Code: "api_error", Detail: cerr.Code().String() + ": " + detail, Exit: ExitAPI, Err: err} } } var uerr *url.Error if errors.As(err, &uerr) { return &Error{Code: "network_error", Detail: uerr.Error(), Exit: ExitAPI, Err: err} } return &Error{Code: "error", Detail: err.Error(), Exit: ExitAPI, Err: err} } // WriteError prints err to w in the given format and returns its exit code. func WriteError(w io.Writer, format string, err error) int { e := Classify(err) if format == "json" { b, _ := json.Marshal(map[string]any{"error": map[string]any{"code": e.Code, "detail": e.Detail, "exit": e.Exit}}) fmt.Fprintln(w, string(b)) } else { fmt.Fprintf(w, "error: %s: %s\n", e.Code, e.Detail) } return e.Exit }