Error handling - Core helpers
This page lists all error handling operations available in the core package of lo.
This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.
Your feedback helps us improve!
Validateโ
Creates an error when a condition is not met; returns nil when it is.
slice := []string{"a"}err := lo.Validate(len(slice) == 0, "Slice should be empty")// error("Slice should be empty")err := lo.Validate(len(slice) == 0, "Slice should be empty but contains %v", slice)// error("Slice should be empty but contains [a]")Prototype:func Validate(ok bool, format string, args ...any) errorMustXโ
Panics if err is an error or false, returning successful values otherwise. Variants cover functions returning 0 to 6 values.
// returns 10, panics if err is not nilv := lo.Must(strconv.Atoi("10"))// panics with custom messagelo.Must0(fmt.Errorf("boom"), "failed to parse")// panics if myFunc returns an errorfunc myFunc() (int, string, float64, bool, error) { ... }a, b, c, d := lo.Must4(myFunc())Variant:Prototypes:func Must[T any](val T, err any, messageArgs ...any) Tfunc Must0(err any, messageArgs ...any)func Must1[T any](val T, err any, messageArgs ...any) Tfunc Must2[T1, T2 any](val1 T1, val2 T2, err any, messageArgs ...any) (T1, T2)func Must3[T1, T2, T3 any](val1 T1, val2 T2, val3 T3, err any, messageArgs ...any) (T1, T2, T3)func Must4[T1, T2, T3, T4 any](val1 T1, val2 T2, val3 T3, val4 T4, err any, messageArgs ...any) (T1, T2, T3, T4)func Must5[T1, T2, T3, T4, T5 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, err any, messageArgs ...any) (T1, T2, T3, T4, T5)func Must6[T1, T2, T3, T4, T5, T6 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, val6 T6, err any, messageArgs ...any) (T1, T2, T3, T4, T5, T6)TryXโ
Calls a function and returns false in case of error or panic. Variants cover callbacks returning 0 to 6 values.
ok := lo.Try(func() error {// return an error to mark failurereturn fmt.Errorf("boom")})// ok == falseok = lo.Try0(func() {// panics are caught and return falsepanic("boom")})// ok == falseok = lo.Try2(func() (int, error) {return 42, nil})// ok == trueVariant:Prototypes:func Try(callback func() error) boolfunc Try0(callback func()) boolfunc Try1(callback func() error) boolfunc Try2[T any](callback func() (T, error)) boolfunc Try3[T, R any](callback func() (T, R, error)) boolfunc Try4[T, R, S any](callback func() (T, R, S, error)) boolfunc Try5[T, R, S, Q any](callback func() (T, R, S, Q, error)) boolfunc Try6[T, R, S, Q, U any](callback func() (T, R, S, Q, U, error)) boolTryOrXโ
Like Try, but returns provided fallback values in case of error; also returns a success flag. Variants cover callbacks returning 1 to 6 values.
value, ok := lo.TryOr(func() (int, error) {return 0, fmt.Errorf("boom")}, 123)// value == 123, ok == falsevalue, ok = lo.TryOr(func() (int, error) {return 42, nil}, 0)// value == 42, ok == trueVariant:Prototypes:func TryOr[A any](callback func() (A, error), fallbackA A) (A, bool)func TryOr1[A any](callback func() (A, error), fallbackA A) (A, bool)func TryOr2[A, B any](callback func() (A, B, error), fallbackA A, fallbackB B) (A, B, bool)func TryOr3[A, B, C any](callback func() (A, B, C, error), fallbackA A, fallbackB B, fallbackC C) (A, B, C, bool)func TryOr4[A, B, C, D any](callback func() (A, B, C, D, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D) (A, B, C, D, bool)func TryOr5[A, B, C, D, E any](callback func() (A, B, C, D, E, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D, fallbackE E) (A, B, C, D, E, bool)func TryOr6[A, B, C, D, E, F any](callback func() (A, B, C, D, E, F, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D, fallbackE E, fallbackF F) (A, B, C, D, E, F, bool)TryWithErrorValueโ
Runs a function and returns the error value (panic or error) along with a boolean indicating success.
err, ok := lo.TryWithErrorValue(func() error { panic("error") })// err == "error", ok == falsePrototype:func TryWithErrorValue(callback func() error) (errorValue any, ok bool)TryCatchโ
Calls the catch function when the callback errors or panics.
caught := falselo.TryCatch(func() error { panic("error") }, func() { caught = true })// caught == truePrototype:func TryCatch(callback func() error, catch func())TryCatchWithErrorValueโ
Calls the catch function with the error value when the callback errors or panics.
lo.TryCatchWithErrorValue(func() error {panic("error")},func(val any) {fmt.Println(val)},)Prototype:func TryCatchWithErrorValue(callback func() error, catch func(any))ErrorsAsโ
A generic wrapper around
errors.Asthat returns the typed error and a boolean indicating success.if rateLimitErr, ok := lo.ErrorsAs[*RateLimitError](err); ok {// handle}Variant:Prototype:func ErrorsAs[T error](err error) (T, bool)Assertโ
Does nothing when condition is true; otherwise panics.
// Base variant with optional messageage := 12lo.Assert(age >= 15, "user age must be >= 15")// panics: "user age must be >= 15"// Without message - panics with default messagex := -1lo.Assert(x > 0)// panics: "assertion failed: condition is not true"// Formatted variant with custom messageage = 12lo.Assertf(age >= 15, "user age must be >= 15, got %d", age)// panics: "user age must be >= 15, got 12"// When condition is true - no panicage = 20lo.Assert(age >= 15, "user age must be >= 15")// continues normallyCustom handler
Replace
lo.Assertandlo.Assertfwith your own statement:lo.Assertf = func(condition bool, format string, args ...any) {if !condition {panic(fmt.Errorf("%s: %s", "customErr", fmt.Sprintf(format, args...)))}}Variant:Prototypes:func Assert(condition bool, message ...string)func Assertf(condition bool, format string, args ...any)