Help us shape the future of Go! Take the 2026 Go Developer Survey

output

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SchemaVersion = 1
	SchemaType    = "tslink.result"
)
View Source
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

func ExitCode(err error) int

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 Failure

func Failure(command string, code int, message string)

Failure writes a failed JSON result to stdout with the given exit code.

func FailureForError

func FailureForError(command string, err error)

FailureForError writes a failed JSON result to stdout for err.

func IsSilent

func IsSilent(err error) bool

IsSilent reports whether err should set an exit code without extra output.

func NextCommandsForError

func NextCommandsForError(err error) []string

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

func StableErrorCode(code int) string

StableErrorCode maps numeric semantic exit codes to stable machine strings.

func Success

func Success(command string, data any)

Success writes a successful JSON result to stdout.

func WriteJSON

func WriteJSON(w io.Writer, r Result)

WriteJSON writes a Result as JSON to the given writer.

Types

type CodeError

type CodeError struct {
	Code    int
	Message string
	Data    any
}

CodeError is an error that carries a semantic exit code.

func ErrAuth

func ErrAuth(msg string) *CodeError

ErrAuth returns an authentication or authorization error (exit code 3).

func ErrConflict

func ErrConflict(msg string) *CodeError

ErrConflict returns a conflict error (exit code 4).

func ErrConflictWithData

func ErrConflictWithData(msg string, data any) *CodeError

ErrConflictWithData returns a conflict with additive structured context.

func ErrNotFound

func ErrNotFound(msg string) *CodeError

ErrNotFound returns a not-found error (exit code 5).

func ErrUsage

func ErrUsage(msg string) *CodeError

ErrUsage returns a usage error (exit code 2).

func (*CodeError) Error

func (e *CodeError) Error() string

func (*CodeError) ErrorData

func (e *CodeError) ErrorData() any

ErrorData returns optional stable machine-readable context for the failure.

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

func NewFailure(command string, code int, message string) Result

NewFailure returns a failed JSON result with a generic stable error code derived from the numeric exit code.

func NewFailureForError

func NewFailureForError(command string, err error) Result

NewFailureForError returns a failed JSON result using any stable error code carried by err, falling back to the generic numeric-code mapping.

func NewSuccess

func NewSuccess(command string, data any) Result

NewSuccess returns a successful JSON result.

func (Result) MarshalJSON

func (r Result) MarshalJSON() ([]byte, error)

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL