Documentation
¶
Index ¶
- func LogErr(err error, events ...func(e *zerolog.Event))
- func LogErrCtx(ctx context.Context, err error, events ...func(e *zerolog.Event))
- func Must(err error, events ...func(e *zerolog.Event))
- func Must1[T any](ret T, err error) T
- func Partition[T any](results []Result[T]) ([]T, []error)
- func Recovery(setter ErrSetter, callbacks ...func(err error) error)
- func RecoveryErr(setter *error, callbacks ...func(err error) error)
- func Throw(setter ErrSetter, err error, contexts ...context.Context) bool
- func ThrowErr(rawSetter *error, err error, contexts ...context.Context) bool
- type Checkable
- type ErrSetter
- type Error
- func (e Error) Err() error
- func (e Error) Expect(format string, args ...any)
- func (e Error) GetErr() error
- func (e Error) IfErr(fn func(error)) Error
- func (e Error) InspectErr(fn func(error))
- func (e Error) IsErr() bool
- func (e Error) IsOK() bool
- func (e Error) Log(events ...func(e *zerolog.Event)) Error
- func (e Error) LogCtx(ctx context.Context, events ...func(e *zerolog.Event)) Error
- func (e Error) MapErr(fn func(err error) error) Error
- func (e Error) MarshalJSON() ([]byte, error)
- func (e Error) Match(onOk func(), onErr func(error))
- func (e Error) MatchWithError(onOk func() error, onErr func(error) error) error
- func (e Error) Must()
- func (e Error) MustWithLog(events ...func(e *zerolog.Event))
- func (e Error) String() string
- func (e Error) ThrowErr(setter ErrSetter, contexts ...context.Context) bool
- func (e Error) TryUnwrap() (error, bool)
- func (e Error) WithErr(err error, tags ...errors.Tags) Error
- func (e Error) WithErrorf(format string, args ...any) Error
- func (e Error) WithFn(fn func() error) Error
- type Future
- type FutureErr
- type ProxyErr
- type Result
- func All[T any](results ...Result[T]) Result[[]T]
- func Collect[T any](results []Result[T]) Result[[]T]
- func Fail[T any](err error) Result[T]
- func FlatMapTo[T, U any](r Result[T], fn func(T) Result[U]) Result[U]
- func MapTo[T, U any](r Result[T], fn func(T) U) Result[U]
- func OK[T any](v T) Result[T]
- func Try[T any](fn func() T) (r Result[T])
- func Wrap[T any](v T, err error) Result[T]
- func WrapFn[T any](fn func() (T, error)) Result[T]
- func (r Result[T]) Err() error
- func (r Result[T]) Expect(format string, args ...any) T
- func (r Result[T]) FlatMap(fn func(val T) Result[T]) Result[T]
- func (r Result[T]) GetErr() error
- func (r Result[T]) IfErr(fn func(err error)) Result[T]
- func (r Result[T]) IfOK(fn func(val T)) Result[T]
- func (r Result[T]) Inspect(fn func(val T))
- func (r Result[T]) InspectErr(fn func(err error))
- func (r Result[T]) IsErr() bool
- func (r Result[T]) IsOK() bool
- func (r Result[T]) Log(events ...func(e *zerolog.Event)) Result[T]
- func (r Result[T]) LogCtx(ctx context.Context, events ...func(e *zerolog.Event)) Result[T]
- func (r Result[T]) Map(fn func(val T) T) Result[T]
- func (r Result[T]) MapErr(fn func(err error) error) Result[T]
- func (r Result[T]) MapErrOr(fn func(err error) Result[T]) Result[T]
- func (r Result[T]) MarshalJSON() ([]byte, error)
- func (r Result[T]) Match(onOk func(T), onErr func(error))
- func (r Result[T]) MatchWithResult(onOk func(T) Result[T], onErr func(error) Result[T]) Result[T]
- func (r Result[T]) Must(events ...func(e *zerolog.Event))
- func (r Result[T]) Or(defaultVal T) Result[T]
- func (r Result[T]) OrElse(fn func() T) Result[T]
- func (r Result[T]) String() string
- func (r Result[T]) ThrowErr(setter ErrSetter, contexts ...context.Context) bool
- func (r Result[T]) TryUnwrap() (T, bool)
- func (r Result[T]) Unwrap() T
- func (r Result[T]) UnwrapErr() (T, error)
- func (r Result[T]) UnwrapOr(defaultVal T) T
- func (r Result[T]) UnwrapOrElse(fn func() T) T
- func (r Result[T]) UnwrapOrEmpty() (t T)
- func (r Result[T]) UnwrapOrLog(events ...func(e *zerolog.Event)) T
- func (r Result[T]) UnwrapOrThrow(setter ErrSetter, contexts ...context.Context) (t T)
- func (r Result[T]) Validate(fn func(val T) error) Result[T]
- func (r Result[T]) ValueTo(v *T) Error
- func (r Result[T]) WithErr(err error, tags ...errors.Tags) Result[T]
- func (r Result[T]) WithErrorf(format string, args ...any) Result[T]
- func (r Result[T]) WithFn(fn func() (T, error)) Result[T]
- func (r Result[T]) WithValue(v T) Result[T]
- type Void
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Partition ¶
Partition separates a slice of Results into values and errors This function takes a slice of Results and separates them into two slices: one containing all successful values and another containing all errors. This is useful when you want to process all successes and handle all errors separately rather than stopping at the first error.
Example:
results := []result.Result[int]{result.OK(1), result.Fail[int](err1), result.OK(2)}
values, errors := result.Partition(results)
fmt.Printf("Values: %v, Errors: %v\n", values, errors)
func RecoveryErr ¶
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
func (Error) InspectErr ¶
func (Error) MarshalJSON ¶
func (Error) Match ¶
Match allows pattern matching on the Error, applying the appropriate function This method provides a way to handle both success (no error) and error cases in a single operation, similar to pattern matching in functional languages.
Example:
errorResult.Match(
func() { fmt.Println("No error occurred") },
func(err error) { fmt.Printf("Error: %v\n", err) },
)
func (Error) MatchWithError ¶
MatchWithError allows pattern matching with an error-returning function This method is similar to Match, but both handler functions return an error, allowing for chaining operations that may themselves produce errors.
Example:
err := errorResult.MatchWithError(
func() error { return nil },
func(prevErr error) error { return fmt.Errorf("wrapped: %w", prevErr) },
)
func (Error) MustWithLog ¶
func (Error) TryUnwrap ¶
TryUnwrap attempts to unwrap the error, returning it and a boolean indicating if it's an error This method is useful when you want to safely extract an error from an Error result without triggering a panic. It returns the error (or nil if no error) and a boolean indicating whether an error was present.
Example:
if err, ok := errorResult.TryUnwrap(); ok {
fmt.Printf("Error occurred: %v\n", err)
} else {
fmt.Println("No error present")
}
type ProxyErr ¶
type ProxyErr struct {
// contains filtered or unexported fields
}
func ErrProxyOf ¶
type Result ¶
type Result[T any] struct { // contains filtered or unexported fields }
func Collect ¶
Collect takes a slice of Results and returns either all values or the first error This function processes a slice of Results and collects all successful values into a single Result containing a slice of values. If any Result contains an error, the function stops and returns that error in a Result.
Example:
results := []result.Result[int]{result.OK(1), result.OK(2), result.OK(3)}
collected := result.Collect(results)
if vals, ok := collected.TryUnwrap(); ok {
fmt.Printf("Collected values: %v\n", vals)
}
func Try ¶
Try converts a function that may panic into a Result This function wraps a potentially panicking function and converts panics into error Results, providing a safer way to call functions that might panic.
Example:
result := result.Try(func() int {
// Some operation that might panic
return riskyOperation()
})
if value, ok := result.TryUnwrap(); ok {
fmt.Printf("Success: %d\n", value)
}
func (Result[T]) IfErr ¶
IfErr executes fn if the result is an error, then returns the result unchanged. This is similar to InspectErr but allows chaining with other operations.
func (Result[T]) IfOK ¶
IfOK executes fn if the result is OK, then returns the result unchanged. This is similar to Inspect but allows chaining with other operations.
func (Result[T]) InspectErr ¶
func (Result[T]) MarshalJSON ¶
func (Result[T]) Match ¶
Match allows pattern matching on the Result, applying the appropriate function This method provides a way to handle both success and error cases in a single operation similar to pattern matching in functional languages.
Example:
result.OK(42).Match(
func(value int) { fmt.Printf("Success: %d\n", value) },
func(err error) { fmt.Printf("Error: %v\n", err) },
)
func (Result[T]) MatchWithResult ¶
MatchWithResult allows pattern matching with a result-returning function This method is similar to Match, but both handler functions return a Result[T], allowing for chaining operations that may themselves produce Results.
Example:
result.OK(42).MatchWithResult(
func(value int) result.Result[int] { return result.OK(value * 2) },
func(err error) result.Result[int] { return result.Fail[int](err) },
)
func (Result[T]) TryUnwrap ¶
TryUnwrap attempts to unwrap the value, returning it and a boolean indicating success This method is useful when you want to safely extract a value from a Result without triggering a panic. It returns the value (or zero value if error) and a boolean indicating whether the extraction was successful.
Example:
if value, ok := result.TryUnwrap(); ok {
fmt.Printf("Success: %v\n", value)
} else {
fmt.Println("Operation failed")
}
func (Result[T]) UnwrapOrElse ¶
func (r Result[T]) UnwrapOrElse(fn func() T) T
func (Result[T]) UnwrapOrEmpty ¶
func (r Result[T]) UnwrapOrEmpty() (t T)