style: merge assertion.go and errors.go

This commit is contained in:
Samuel Berthe
2025-06-01 16:19:29 +02:00
parent e9281d8a66
commit 49f24de919
7 changed files with 131 additions and 147 deletions
+27
View File
@@ -6,6 +6,8 @@ import (
"reflect"
)
const defaultAssertionFailureMessage = "assertion failed"
// Validate is a helper that creates an error when a condition is not met.
// Play: https://go.dev/play/p/vPyh51XpCBt
func Validate(ok bool, format string, args ...any) error {
@@ -352,3 +354,28 @@ func ErrorsAs[T error](err error) (T, bool) {
ok := errors.As(err, &t)
return t, ok
}
// Assert does nothing when the condition is true, otherwise it panics with an optional message.
// Play: https://go.dev/play/p/Xv8LLKBMNwI
func Assert(condition bool, message ...string) {
if condition {
return
}
panicMessage := defaultAssertionFailureMessage
if len(message) > 0 {
panicMessage = fmt.Sprintf("%s: %s", defaultAssertionFailureMessage, message[0])
}
panic(panicMessage)
}
// Assertf does nothing when the condition is true, otherwise it panics with a formatted message.
// Play: https://go.dev/play/p/TVPEmVcyrdY
func Assertf(condition bool, format string, args ...any) {
if condition {
return
}
panicMessage := fmt.Sprintf("%s: %s", defaultAssertionFailureMessage, fmt.Sprintf(format, args...))
panic(panicMessage)
}