Documentation
¶
Index ¶
- Constants
- func ExitCode(err error) int
- func Failure(command string, code int, message string)
- func FailureForError(command string, err error)
- func IsSilent(err error) bool
- func NextCommandsForError(err error) []string
- func StableErrorCode(code int) string
- func Success(command string, data any)
- func WriteJSON(w io.Writer, r Result)
- type CodeError
- type ErrorObject
- type Result
- type SilentCodeError
Constants ¶
const ( SchemaVersion = 1 SchemaType = "tslink.result" )
const ( ExitSuccess = errcode.ExitSuccess ExitError = errcode.ExitError ExitUsage = errcode.ExitUsage ExitAuth = errcode.ExitAuth ExitConflict = errcode.ExitConflict ExitNotFound = errcode.ExitNotFound ExitWarning = errcode.ExitWarning ExitCritical = errcode.ExitCritical )
Semantic exit codes for programmatic consumers, defined with the error-code table in internal/errcode.
Variables ¶
This section is empty.
Functions ¶
func ExitCode ¶
ExitCode extracts the exit code from an error. A stable code decides it through the error-code table, whatever type carries the code, so a code always exits the same way; an error with only a CodeError or SilentCodeError exits with its number. Returns ExitError (1) for any other error and ExitSuccess (0) for nil.
func FailureForError ¶
FailureForError writes a failed JSON result to stdout for err.
func NextCommandsForError ¶
NextCommandsForError returns the recovery commands a human-mode caller should print after the error message: the error's own next list when it carries a stable code, otherwise the default continuation for its exit category.
func StableErrorCode ¶
StableErrorCode maps numeric semantic exit codes to stable machine strings.
Types ¶
type CodeError ¶
CodeError is an error that carries a semantic exit code.
func ErrConflict ¶
ErrConflict returns a conflict error (exit code 4).
func ErrConflictWithData ¶
ErrConflictWithData returns a conflict with additive structured context.
func ErrNotFound ¶
ErrNotFound returns a not-found error (exit code 5).
type ErrorObject ¶
type ErrorObject struct {
Code string `json:"code"`
Message string `json:"message"`
Next []string `json:"next,omitempty"`
Data any `json:"data,omitempty"`
}
ErrorObject is the stable machine/human error payload in the JSON envelope.
func ErrorObjectForError ¶
func ErrorObjectForError(code int, err error) *ErrorObject
ErrorObjectForError returns the structured error object for err.
func NewErrorObject ¶
func NewErrorObject(code int, message string) *ErrorObject
NewErrorObject returns a generic stable error object for code/message.
type Result ¶
type Result struct {
Type string `json:"type"`
OK bool `json:"ok"`
SchemaVersion int `json:"schema_version"`
Command string `json:"command,omitempty"`
Code int `json:"code"`
Data any `json:"data,omitempty"`
Error *ErrorObject `json:"error,omitempty"`
}
Result is the versioned JSON envelope for structured output.
func NewFailure ¶
NewFailure returns a failed JSON result with a generic stable error code derived from the numeric exit code.
func NewFailureForError ¶
NewFailureForError returns a failed JSON result using any stable error code carried by err, falling back to the generic numeric-code mapping.
func NewSuccess ¶
NewSuccess returns a successful JSON result.
func (Result) MarshalJSON ¶
type SilentCodeError ¶
type SilentCodeError struct {
Code int
}
SilentCodeError carries an exit code for commands that have already printed their complete result and should not receive the generic failure envelope.
func SilentExit ¶
func SilentExit(code int) *SilentCodeError
SilentExit returns a non-printing error for the requested exit code.
func (*SilentCodeError) Error ¶
func (e *SilentCodeError) Error() string