From 6ced549deaecb42b9bb93ea9efcb4c1bbaabe8a4 Mon Sep 17 00:00:00 2001 From: mhsanaei Date: Sat, 20 Sep 2025 09:35:50 +0200 Subject: docs: add comments for all functions --- util/common/err.go | 4 ++++ util/common/format.go | 1 + util/common/multi_error.go | 3 +++ 3 files changed, 8 insertions(+) (limited to 'util/common') 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 { -- cgit v1.2.3