Files
trihuy-russian/util/common/err.go
T

33 lines
752 B
Go
Raw Normal View History

2025-09-20 09:35:50 +02:00
// Package common provides common utility functions for error handling, formatting, and multi-error management.
2023-02-09 22:48:06 +03:30
package common
import (
"errors"
"fmt"
2024-03-11 01:01:24 +03:30
2025-09-19 10:05:43 +02:00
"github.com/mhsanaei/3x-ui/v2/logger"
2023-02-09 22:48:06 +03:30
)
2025-09-20 09:35:50 +02:00
// NewErrorf creates a new error with formatted message.
2025-03-12 20:13:51 +01:00
func NewErrorf(format string, a ...any) error {
2023-02-09 22:48:06 +03:30
msg := fmt.Sprintf(format, a...)
return errors.New(msg)
}
2025-09-20 09:35:50 +02:00
// NewError creates a new error from the given arguments.
2025-03-12 20:13:51 +01:00
func NewError(a ...any) error {
2023-02-09 22:48:06 +03:30
msg := fmt.Sprintln(a...)
return errors.New(msg)
}
2025-09-20 09:35:50 +02:00
// Recover handles panic recovery and logs the panic error if a message is provided.
2025-03-12 20:13:51 +01:00
func Recover(msg string) any {
2023-02-09 22:48:06 +03:30
panicErr := recover()
if panicErr != nil {
if msg != "" {
logger.Error(msg, "panic:", panicErr)
}
}
return panicErr
}