// Package archererr classifies runtime errors so the process can pick an exit // code and so callers can distinguish interrupted runs. package archererr import ( "errors" "fmt" ) type Kind int const ( KindOther Kind = iota KindConfig KindConnection KindWrite KindValidation KindInterrupted ) // Error wraps an error with a classification. type Error struct { Kind Kind Err error } func (e *Error) Error() string { return e.Err.Error() } func (e *Error) Unwrap() error { return e.Err } func Wrap(kind Kind, err error) error { if err == nil { return nil } var existing *Error if errors.As(err, &existing) { return err } return &Error{Kind: kind, Err: err} } func Newf(kind Kind, format string, args ...any) error { return &Error{Kind: kind, Err: fmt.Errorf(format, args...)} } func KindOf(err error) Kind { var e *Error if errors.As(err, &e) { return e.Kind } return KindOther } // ExitCode maps a classification to a process exit code. func ExitCode(err error) int { if err == nil { return 0 } switch KindOf(err) { case KindConfig: return 2 case KindConnection: return 3 case KindWrite: return 4 case KindValidation: return 5 case KindInterrupted: return 130 default: return 1 } }