The notice package offers a suite of utilities for crafting clear, structured
assertion messages. It simplifies the creation of readable error messages,
featuring a header and contextual rows. With a fluent interface, it enables
seamless message construction and includes helper functions for formatting and
unwrapping errors.
msg := notice.New("expected values to be equal").
Want("%s", "abc").
Have("%s", "xyz")
fmt.Println(msg)
// Output:
// expected values to be equal:
// want: abc
// have: xyzErrMy := errors.New("my error")
msg := notice.New("expected values to be equal").
Want("%s", "abc").
Have("%s", "xyz").
Wrap(ErrMy)
is := errors.Is(msg, ErrMy)
fmt.Println(is)
// Output: truemsg := notice.New("expected values to be equal").
Want("%s", "abc").
Have("%s", "xyz").
MetaSet("key", 123)
fmt.Println(msg.MetaLookup("key"))
// Output: 123 trueFor more examples see the examples_test.go file.
lines := notice.Indent(4, ' ', "line1\nline2\nline3")
fmt.Println(lines)
// Output:
// line1
// line2
// line3Notices implement [error] and can be chained into a linked list. This is useful when multiple independent expectations fail in one assertion.
err := notice.Join(
notice.New("first failure"),
notice.New("second failure"),
)The result is a single error whose [notice.Notice.All] returns the full list. Walk the chain with:
- [notice.Notice.Head] — first element
- [notice.Notice.Next] / [notice.Notice.Prev] — traversal
- [notice.Join] — the builder used above
Each notice delegates [errors.Is] and [errors.As] to its base error (default [notice.ErrNotice]). Change the base with [notice.Notice.Wrap]:
myErr := errors.New("root cause")
n := notice.New("something failed").Wrap(myErr)
fmt.Println(errors.Is(n, myErr)) // trueChains are mutable (see [notice.Notice.Chain]). Prefer [Join] when building from multiple values.