diff options
| author | mhsanaei <ho3ein.sanaei@gmail.com> | 2025-09-20 10:35:50 +0300 |
|---|---|---|
| committer | mhsanaei <ho3ein.sanaei@gmail.com> | 2025-09-20 10:35:50 +0300 |
| commit | 6ced549deaecb42b9bb93ea9efcb4c1bbaabe8a4 (patch) | |
| tree | 28d8d82530476cf607e4d05ca189ae05868711e6 /util/common | |
| parent | f60682a6b7cb749fee403c84e2587c3ad7e7ced0 (diff) | |
docs: add comments for all functions
Diffstat (limited to 'util/common')
| -rw-r--r-- | util/common/err.go | 4 | ||||
| -rw-r--r-- | util/common/format.go | 1 | ||||
| -rw-r--r-- | util/common/multi_error.go | 3 |
3 files changed, 8 insertions, 0 deletions
diff --git a/util/common/err.go b/util/common/err.go index 85a743ad..e12bd13f 100644 --- a/util/common/err.go +++ b/util/common/err.go @@ -1,3 +1,4 @@ +// Package common provides common utility functions for error handling, formatting, and multi-error management. package common import ( @@ -7,16 +8,19 @@ import ( "github.com/mhsanaei/3x-ui/v2/logger" ) +// NewErrorf creates a new error with formatted message. func NewErrorf(format string, a ...any) error { msg := fmt.Sprintf(format, a...) return errors.New(msg) } +// NewError creates a new error from the given arguments. func NewError(a ...any) error { msg := fmt.Sprintln(a...) return errors.New(msg) } +// Recover handles panic recovery and logs the panic error if a message is provided. func Recover(msg string) any { panicErr := recover() if panicErr != nil { diff --git a/util/common/format.go b/util/common/format.go index c73e3a01..c40bd3dc 100644 --- a/util/common/format.go +++ b/util/common/format.go @@ -4,6 +4,7 @@ import ( "fmt" ) +// FormatTraffic formats traffic bytes into human-readable units (B, KB, MB, GB, TB, PB). func FormatTraffic(trafficBytes int64) string { units := []string{"B", "KB", "MB", "GB", "TB", "PB"} unitIndex := 0 diff --git a/util/common/multi_error.go b/util/common/multi_error.go index ff9ff628..c695e3c0 100644 --- a/util/common/multi_error.go +++ b/util/common/multi_error.go @@ -4,8 +4,10 @@ import ( "strings" ) +// multiError represents a collection of errors. type multiError []error +// Error returns a string representation of all errors joined with " | ". func (e multiError) Error() string { var r strings.Builder r.WriteString("multierr: ") @@ -16,6 +18,7 @@ func (e multiError) Error() string { return r.String() } +// Combine combines multiple errors into a single error, filtering out nil errors. func Combine(maybeError ...error) error { var errs multiError for _, err := range maybeError { |
