-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
92 lines (72 loc) · 2.84 KB
/
Copy patherror.go
File metadata and controls
92 lines (72 loc) · 2.84 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package wu
import (
"fmt"
"os"
)
type WuErr struct {
isOn bool
}
// NewWuErr creates a new WuErr with error output enabled.
func NewWuErr() *WuErr { return &WuErr{isOn: true} }
// ErrorOn enables error output.
func ErrorOn() { wuerr.isOn = true }
// ErrorOff disables error output.
func ErrorOff() { wuerr.isOn = false }
// SetErr sets the error output state.
func (w *WuErr) SetErr(b bool) { w.isOn = b }
// Err checks for an error and prints it if enabled. Returns true if an error occurred.
func (w *WuErr) Err(err error, desc string) bool {
if err == nil {
return false
}
desc = desc + " CErr: " //Simplified conditional logic
if w.isOn {
wulog.logColorful("#ccc", "88", "", desc, err)
}
return true
}
// ErrNew creates a new error.
func ErrNew(s string) error { return fmt.Errorf(s) } //Use fmt.Errorf for better error wrapping
// New Error
func Errf(format string, a ...any) error { return ErrNew(fmt.Sprintf(format, a...)) }
// Err checks for an error and prints it if enabled. Returns true if an error occurred.
func Err(err error) bool { return wuerr.Err(err, "") }
// CheckErr is an alias for Err.
func CheckErr(err error) bool { return Err(err) }
// OK checks for the absence of an error. Returns true if no error occurred.
func OK(err error) bool { return !Err(err) }
// NotErr, NoErr, Noerr are aliases for OK.
func NotErr(err error) bool { return OK(err) }
func NoErr(err error) bool { return OK(err) }
func Noerr(err error) bool { return OK(err) }
// ErrDesc checks for an error and prints it with a description if enabled. Returns true if an error occurred.
func ErrDesc(err error, desc string) bool { return wuerr.Err(err, desc) }
// Err2 is an alias for ErrDesc.
func Err2(err error, desc string) bool { return ErrDesc(err, desc) }
// ErrFatal checks for an error, prints it, and exits if enabled. Returns true if an error occurred.
func (w *WuErr) ErrFatal(err error, desc string) bool {
if err == nil {
return false
}
desc = desc + " ErrFatal:" //Simplified conditional logic
if w.isOn {
wulog.logColorful("#ccc", "124", "", desc, err)
}
os.Exit(1)
return true
}
// ErrFatal checks for an error, prints it, and exits if enabled. Returns true if an error occurred.
func ErrFatal(err error) bool { return wuerr.ErrFatal(err, "") }
// ErrFatalDesc checks for an error, prints it with a description, and exits if enabled. Returns true if an error occurred.
func ErrFatalDesc(err error, desc string) bool { return wuerr.ErrFatal(err, desc) }
// ErrFatal2 is an alias for ErrFatalDesc.
func ErrFatal2(err error, desc string) bool { return ErrFatalDesc(err, desc) }
// ErrFatalExit checks for an error, prints a description, and exits.
func ErrFatalExit(err error, desc string) {
if err != nil {
wulog.logColorful("#ccc", "124", "", desc, err)
os.Exit(1)
}
}
// Global variable initialization moved to top for clarity.
var wuerr = NewWuErr()