err.go 752 B

1234567891011121314151617181920212223242526272829303132
  1. // Package common provides common utility functions for error handling, formatting, and multi-error management.
  2. package common
  3. import (
  4. "errors"
  5. "fmt"
  6. "github.com/mhsanaei/3x-ui/v2/logger"
  7. )
  8. // NewErrorf creates a new error with formatted message.
  9. func NewErrorf(format string, a ...any) error {
  10. msg := fmt.Sprintf(format, a...)
  11. return errors.New(msg)
  12. }
  13. // NewError creates a new error from the given arguments.
  14. func NewError(a ...any) error {
  15. msg := fmt.Sprintln(a...)
  16. return errors.New(msg)
  17. }
  18. // Recover handles panic recovery and logs the panic error if a message is provided.
  19. func Recover(msg string) any {
  20. panicErr := recover()
  21. if panicErr != nil {
  22. if msg != "" {
  23. logger.Error(msg, "panic:", panicErr)
  24. }
  25. }
  26. return panicErr
  27. }