lang

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendStackTraceToPanics

func AppendStackTraceToPanics()

func Catch

func Catch(fn func()) (err error)

Catch executes fn and recovers from any panic, returning the panic value as an error. Use this to attempt operations that might fail without stopping the entire build.

Example:

err := lang.Catch(func() {
    Run(`optional-command`)
})
if err != nil {
    log.Debug("optional command failed: %v", err)
}

func Close

func Close(closeable io.Closer, context ...any)

Close safely closes an io.Closer and logs any error. Unlike a bare defer close(), this doesn't lose the error and provides context for debugging. Use in place of defer file.Close() patterns.

Example:

f := lang.Return(os.Open(path))
defer lang.Close(f, path)

func Continue added in v0.1.0

func Continue[T any](t T, e error) T

Continue returns the value regardless of any error. If there's an error, it's logged but execution continues. Use when errors are acceptable and shouldn't halt the build.

func Default added in v0.0.2

func Default[T comparable](values ...T) T

Default returns the first non-zero value from the provided values. Useful for providing fallback values in a chain.

Example:

name := Default(os.Getenv("NAME"), config.DefaultName, "anonymous")

func HandleErrors

func HandleErrors()

HandleErrors is the main panic recovery handler for go-make. It should be deferred at the start of task execution to catch panics and display formatted error messages. This is automatically called by Makefile() - you typically don't need to call it directly.

Behavior:

  • OkError: exits cleanly without error output
  • StackTraceError: prints formatted error with stack trace, exits with ExitCode
  • Other panics: prints error with stack trace, exits with code 1

func List

func List[T any](values ...T) []T

List returns a slice containing all non-empty values. Values that are nil, zero-length strings, empty slices/maps, or other "empty" values are filtered out. Useful for building lists where some values may be conditionally present.

Example:

args := List("build", verbose && "-v", "-o", output)  // filters out false

func Map

func Map[From, To any](values []From, mapFunc func(From) To) []To

Map returns a new slice with values mapped from incoming to outgoing in mapFunc

func Remove

func Remove[T comparable](values []T, shouldRemove func(T) bool) []T

Remove returns a new slice with values removed based on true returns from shouldRemove

func Return

func Return[T any](t T, e error) T

Return returns the value if error is nil, otherwise panics with the error. This is the standard pattern for error handling in go-make tasks.

Example:

contents := lang.Return(os.ReadFile("config.yaml"))

func Throw

func Throw(e error)

Throw panics if the provided error is non-nil. This is the fundamental error handling primitive in go-make - errors cause immediate task failure with a stack trace pointing to the source.

Types

type OkError

type OkError struct{}

OkError is a sentinel error that indicates successful completion. When caught by HandleErrors, it exits cleanly without printing an error message.

func (*OkError) Error

func (o *OkError) Error() string

type StackTraceError

type StackTraceError struct {
	// Err is the underlying error.
	Err error
	// ExitCode is the exit code to use when this error causes program termination.
	ExitCode int
	// Stack contains the filtered stack trace lines.
	Stack []string
	// Log contains additional output (e.g., stdout/stderr) to display with the error.
	Log string
}

StackTraceError wraps an error with additional context for better error reporting. It captures the stack trace at creation time and can include additional log output.

func NewStackTraceError

func NewStackTraceError(err error) *StackTraceError

NewStackTraceError helps to capture nicer stack trace information

func (*StackTraceError) Error

func (s *StackTraceError) Error() string

func (*StackTraceError) Unwrap

func (s *StackTraceError) Unwrap() error

func (*StackTraceError) WithExitCode

func (s *StackTraceError) WithExitCode(exitCode int) *StackTraceError

func (*StackTraceError) WithLog

func (s *StackTraceError) WithLog(log string) *StackTraceError

Jump to

Keyboard shortcuts

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