Documentation
¶
Overview ¶
softassert provides utilities for performing assertions.
Unlike traditional assertions that may terminate the program or panic, soft assertions log an error message when a condition is not met but allow the program to continue running. The runner of the program, such as a Go test, is expected to flag failed assertions.
Best practices: - Use it to check for programming errors and invariants. - Use it to communicate assumptions about the code. - Use it to abort or recover from an unexpected state. - Never use it as a substitute for regular error handling, validation, or control flow.
Index ¶
- func Fail(logger log.Logger, staticMessage string, tags ...tag.Tag)
- func That(logger log.Logger, condition bool, staticMessage string, tags ...tag.Tag) bool
- func UnexpectedDataLoss(logger log.Logger, staticMessage string, optionalErr error, tags ...tag.Tag) error
- func UnexpectedInternalErr(logger log.Logger, staticMessage string, optionalErr error, tags ...tag.Tag) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Fail ¶
Fail logs an error using `staticMessage` to indicate a failed assertion.
`staticMessage` is expected to be a static string to help with grouping and searching logs. Dynamic information should be passed via `tags`.
Example: softassert.Fail(logger, "unreachable code reached", tag.NewStringTag("state", object.state))
func That ¶
That performs a soft assertion by logging an error if the given condition is false. It is meant to indicate a condition is always expected to be true. Returns true if the condition is met, otherwise false.
`staticMessage` is expected to be a static string to help with grouping and searching logs. Dynamic information should be passed via `tags`.
Example: softassert.That(logger, object.state == "ready", "object is not ready")
func UnexpectedDataLoss ¶ added in v1.30.0
func UnexpectedDataLoss(logger log.Logger, staticMessage string, optionalErr error, tags ...tag.Tag) error
UnexpectedDataLoss creates a serviceerror.DataLoss from a static message and optional error. The error message follows the format "<staticMessage>: <optionalErr>", if optionalErr is not nil.
It also logs `staticMessage` using softassert.Fail. `optionalErr` is added as a tag with key "softassert-error-details".
func UnexpectedInternalErr ¶ added in v1.30.0
func UnexpectedInternalErr(logger log.Logger, staticMessage string, optionalErr error, tags ...tag.Tag) error
UnexpectedInternalErr creates a serviceerror.Internal from a static message and error details. The error message follows the format "<staticMessage>: <optionalErr>", if optionalErr is not nil.
It also logs `staticMessage` using softassert.Fail. `optionalErr` is added as a tag with key "softassert-error-details".
Types ¶
This section is empty.