-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_or.go
55 lines (48 loc) · 1.34 KB
/
error_or.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package ctrl
import (
"fmt"
)
// ErrorOr returns nil if condition is true, otherwise returns an error.
func ErrorOr(condition bool) error {
if !condition {
return fmt.Errorf("assertion failed")
}
return nil
}
// ErrorOrf returns nil if condition is true, otherwise returns an error with a formatted message.
func ErrorOrf(condition bool, format string, args ...any) error {
if !condition {
m := fmt.Sprintf(format, args...)
return fmt.Errorf("assertion failed: %s", m)
}
return nil
}
// ErrorOrFunc returns nil if the function returns true, otherwise returns an error.
func ErrorOrFunc(f func() bool) error {
if !f() {
return fmt.Errorf("assertion failed")
}
return nil
}
// ErrorOrFuncf returns nil if the function returns true, otherwise returns an error with a formatted message.
func ErrorOrFuncf(f func() bool, format string, args ...any) error {
if !f() {
m := fmt.Sprintf(format, args...)
return fmt.Errorf("assertion failed: %s", m)
}
return nil
}
// ErrorOrWithErr returns nil if condition is true, otherwise returns the given error.
func ErrorOrWithErr(condition bool, err error) error {
if !condition {
return err
}
return nil
}
// ErrorOrFuncWithErr returns nil if the function returns true, otherwise returns the given error.
func ErrorOrFuncWithErr(f func() bool, err error) error {
if !f() {
return err
}
return nil
}