errors.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Package logger provides logging functionality
  2. package logger
  3. import (
  4. "errors"
  5. "fmt"
  6. )
  7. // Common logger error types
  8. var (
  9. // ErrInvalidFormat indicates an invalid log format was specified
  10. ErrInvalidFormat = errors.New("invalid log format")
  11. // ErrInvalidLevel indicates an invalid log level was specified
  12. ErrInvalidLevel = errors.New("invalid log level")
  13. // ErrOutputFailed indicates a failure to set or write to the log output
  14. ErrOutputFailed = errors.New("failed to set or write to log output")
  15. // ErrFormatterFailed indicates a failure in the log formatter
  16. ErrFormatterFailed = errors.New("log formatter failed")
  17. // ErrInvalidConfig indicates missing or invalid logger configuration
  18. ErrInvalidConfig = errors.New("invalid logger configuration")
  19. )
  20. // Error represents a logger-specific error with detailed context
  21. type Error struct {
  22. // Op is the operation that failed (e.g., "format", "level", "output")
  23. Op string
  24. // Err is the underlying error
  25. Err error
  26. // Details contains additional error context
  27. Details string
  28. // Setting indicates the configuration setting involved (if applicable)
  29. Setting string
  30. // Value indicates the invalid value (if applicable)
  31. Value string
  32. }
  33. // Error returns a string representation of the error
  34. func (e *Error) Error() string {
  35. msg := fmt.Sprintf("logger %s failed", e.Op)
  36. if e.Setting != "" {
  37. msg = fmt.Sprintf("%s for setting %q", msg, e.Setting)
  38. }
  39. if e.Value != "" {
  40. msg = fmt.Sprintf("%s with value %q", msg, e.Value)
  41. }
  42. if e.Details != "" {
  43. msg = fmt.Sprintf("%s: %s", msg, e.Details)
  44. }
  45. if e.Err != nil {
  46. msg = fmt.Sprintf("%s: %v", msg, e.Err)
  47. }
  48. return msg
  49. }
  50. // Unwrap returns the underlying error
  51. func (e *Error) Unwrap() error {
  52. return e.Err
  53. }
  54. // Is reports whether target matches this error
  55. func (e *Error) Is(target error) bool {
  56. return errors.Is(e.Err, target)
  57. }
  58. // NewError creates a new logger error
  59. func NewError(op string, err error, details string) *Error {
  60. return &Error{
  61. Op: op,
  62. Err: err,
  63. Details: details,
  64. }
  65. }
  66. // NewConfigError creates a new logger configuration error
  67. func NewConfigError(op string, setting string, value string, err error, details string) *Error {
  68. return &Error{
  69. Op: op,
  70. Setting: setting,
  71. Value: value,
  72. Err: err,
  73. Details: details,
  74. }
  75. }
  76. // IsConfigError returns true if the error indicates a configuration problem
  77. func IsConfigError(err error) bool {
  78. var e *Error
  79. return errors.As(err, &e) && (errors.Is(e.Err, ErrInvalidFormat) ||
  80. errors.Is(e.Err, ErrInvalidLevel) ||
  81. errors.Is(e.Err, ErrInvalidConfig))
  82. }
  83. // IsOutputError returns true if the error indicates an output problem
  84. func IsOutputError(err error) bool {
  85. var e *Error
  86. return errors.As(err, &e) && errors.Is(e.Err, ErrOutputFailed)
  87. }