Documentation
¶
Index ¶
- func AppendStackTraceToPanics()
- func Catch(fn func()) (err error)
- func Close(closeable io.Closer, context ...any)
- func Continue[T any](t T, e error) T
- func Default[T comparable](values ...T) T
- func HandleErrors()
- func List[T any](values ...T) []T
- func Map[From, To any](values []From, mapFunc func(From) To) []To
- func Remove[T comparable](values []T, shouldRemove func(T) bool) []T
- func Return[T any](t T, e error) T
- func Throw(e error)
- type OkError
- type StackTraceError
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 ¶
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
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
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.
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