Documentation ¶
Overview ¶
Package must implements assertions.
Index ¶
- Variables
- func Check(err error) error
- func CheckAll(errs ...error)
- func Checkf(err error, format string, args ...any) error
- func Equal[T comparable](a T, b T) bool
- func Equalf[T comparable](a T, b T, format string, args ...any) bool
- func Func[X any](f func() (X, error)) func() X
- func Never()
- func Neverf(format string, args ...any)
- func Nil(v any) bool
- func Nilf(v any, format string, args ...any) bool
- func Not(q bool) bool
- func NotNil(v any) bool
- func NotNilf(v any, format string, args ...any) bool
- func Notf(q bool, format string, args ...any) bool
- func Ok[T any](t T, ok bool) T
- func Okf[T any](t T, ok bool, format string, args ...any) T
- func Result[T any](t T, err error) T
- func Resultf[T any](t T, err error, format string, args ...any) T
- func True(q bool) bool
- func Truef(q bool, format string, args ...any) bool
- func Try[X any](f func() X) func() (x X, err error)
- type CheckError
- type CompareError
- type NeverError
- type OkError
- type ResultError
- type ValueError
Constants ¶
This section is empty.
Variables ¶
var False = Not
False is an alias of Not.
var Falsef = Notf
Falsef is an alias of Notf.
Functions ¶
func Check ¶
Check panics if the error is not nil. Otherwise, it returns a nil error (so that it is convenient to chain). The raised non-nil error is wrapped in a CheckError before panicking.
func CheckAll ¶ added in v2.7.0
func CheckAll(errs ...error)
CheckAll panics at the first non-nil error. The raised non-nil error is wrapped in a CheckError before panicking.
func Checkf ¶ added in v2.12.0
Checkf panics if the error is not nil. Otherwise, it always returns a nil error (so that it is convenient to chain).
The fmt.Sprintf -style format string and with optional arguments are used to format the error message raised in the panic. The original non-nil input error, and the formatted error message, are joined (see errors.Join and wrapped in CheckError before panicking.
func Equal ¶ added in v2.3.3
func Equal[T comparable](a T, b T) bool
Equal panics if the provided comparable values are not equal. Otherwise, returns true.
func Equalf ¶ added in v2.12.0
func Equalf[T comparable](a T, b T, format string, args ...any) bool
Equalf panics if the provided comparable values are not equal. Otherwise, returns true.
The fmt.Sprintf -style format string and with optional arguments are used to format the error message. The formatted error message is wrapped in CompareError before panicking.
func Func ¶
Func takes a function f() => (x, error), and returns a function f() => x that may panic in the event of error.
Any raised error is wrapped in ResultError.
func Never ¶ added in v2.0.13
func Never()
Never signifies code that should never be reached. It raises a panic when called.
func Neverf ¶ added in v2.12.0
Neverf signifies code that should never be reached. It raises a panic when called.
The args parameter defines an optional fmt.Sprintf-style format string and arguments.
func Nilf ¶ added in v2.16.0
Nilf returns true if v is (untyped) nil. Otherwise, panics.
The fmt.Sprintf -style format string and with optional arguments are used to format the error message. The formatted error message is wrapped in CompareError before panicking.
func NotNilf ¶ added in v2.16.0
NotNilf returns true if v is not (untyped) nil. Otherwise, panics.
The fmt.Sprintf -style format string and with optional arguments are used to format the error message. The formatted error message is wrapped in CompareError before panicking.
func Ok ¶
Ok accepts a (value, ok) tuple as input and panics if ok is false, otherwise returns value.
func Okf ¶ added in v2.12.0
Okf accepts a (value, ok) tuple, and a fmt.Sprintf -style format string with optional arguments, as input and panics if ok is false. Otherwise, returns value. The formatted error message is wrapped in OkError before panicking.
func Result ¶
Result accepts a (value, err) tuple as input and panics if err != nil, otherwise returns value. The non-nil input error is wrapped in ResultError before panicking.
For example, must.Result(os.Open("doesnotexist")) may panic with an error such as "error in must.Result[*os.File]: open doesnotexist: no such file or directory", or, on success, return *os.File.
func Resultf ¶ added in v2.12.0
Resultf accepts a (value, err) tuple, and a fmt.Sprintf -style format string with optional arguments, as input and panics if err != nil. Otherwise, returns value. The original non-nil input error, and the formatted error message, are joined (see errors.Join and wrapped in ResultError before panicking.
func Try ¶ added in v2.12.0
Try takes a function f() => x that may panic, and instead returns a function f() => (x, error).
If the raised panic is of type error, it is returned directly. Otherwise, it is wrapped in a ValueError.
Types ¶
type CheckError ¶ added in v2.12.0
type CheckError struct {
// contains filtered or unexported fields
}
CheckError is the type of error that may be returned by a check function such as Check, CheckAll, and Checkf.
func (CheckError) Error ¶ added in v2.12.0
func (e CheckError) Error() string
Error implements the standard error interface.
func (CheckError) Unwrap ¶ added in v2.12.0
func (e CheckError) Unwrap() error
Unwrap (for use with errors.Is, etc.) the original error caught by a check function, or the original error and the formatted error message received by formatting variant of a check function (one ending in "f"), in which case the returned error implements `[]Unwrap()`.
type CompareError ¶ added in v2.12.0
type CompareError[T comparable] struct { // contains filtered or unexported fields }
CompareError is the type of error that may be returned by a comparison function such as True or Equal, or a formating variant of a comparison function (one ending in "f").
func (CompareError[T]) Error ¶ added in v2.12.0
func (e CompareError[T]) Error() string
Error implements the standard error interface.
func (CompareError[T]) Unwrap ¶ added in v2.12.0
func (e CompareError[T]) Unwrap() error
Unwrap (for use with errors.Is, etc.) returns nil for an error returned by a comparison function, or the formatted error message received by a formating variant of a comparison function (one ending in "f").
type NeverError ¶ added in v2.12.0
type NeverError struct {
// contains filtered or unexported fields
}
NeverError is the type of error that may be returned by Never, and represents an event that should never happen.
func (NeverError) Error ¶ added in v2.12.0
func (e NeverError) Error() string
Error implements the standard error interface.
type OkError ¶ added in v2.12.0
type OkError[T any] struct { // contains filtered or unexported fields }
OkError is the type of error that may be returned by Ok or Okf.
It is generic in order to capture the information about the type of the hoped result without having to allocate memory at the error creation time.
type ResultError ¶ added in v2.12.0
type ResultError[T any] struct { // contains filtered or unexported fields }
ResultError is the type of error that may be returned by Result or Resultf.
It is generic in order to capture the information about the type of the hoped result without having to allocate memory at the error creation time.
func (ResultError[T]) Error ¶ added in v2.12.0
func (e ResultError[T]) Error() string
Error implements the standard error interface.
type ValueError ¶ added in v2.12.0
type ValueError[T any] struct { // contains filtered or unexported fields }
ValueError is the type of error that may hold a value, for example a non-error value recovered from a panic in Try.
It is generic in order to capture the information about the type of the hoped result without having to allocate memory at the error creation time.
Note that the value itself is type `any`, not one that matches the generic type constraint.
func (ValueError[T]) Error ¶ added in v2.12.0
func (e ValueError[T]) Error() string
Error implements the standard error interface.
func (ValueError[T]) Value ¶ added in v2.12.0
func (e ValueError[T]) Value() any