error.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Package krberror provides error type and functions for gokrb5.
  2. package krberror
  3. import (
  4. "fmt"
  5. "strings"
  6. )
  7. const (
  8. SEPARATOR = " < "
  9. ENCODING_ERROR = "Encoding_Error"
  10. NETWORKING_ERROR = "Networking_Error"
  11. DECRYPTING_ERROR = "Decrypting_Error"
  12. ENCRYPTING_ERROR = "Encrypting_Error"
  13. CHKSUM_ERROR = "Checksum_Error"
  14. KRBMSG_ERROR = "KRBMessage_Handling_Error"
  15. )
  16. // Krberror is an error type for gokrb5
  17. type Krberror struct {
  18. RootCause string
  19. EText []string
  20. }
  21. // Error function to implement the error interface.
  22. func (e Krberror) Error() string {
  23. return fmt.Sprintf("[Root cause: %s] ", e.RootCause) + strings.Join(e.EText, SEPARATOR)
  24. }
  25. // Add another error statement to the error.
  26. func (e *Krberror) Add(et string, s string) {
  27. e.EText = append([]string{fmt.Sprintf("%s: %s", et, s)}, e.EText...)
  28. }
  29. // NewKrberror creates a new instance of Krberror.
  30. func NewKrberror(et, s string) Krberror {
  31. return Krberror{
  32. RootCause: et,
  33. EText: []string{s},
  34. }
  35. }
  36. // Errorf appends to or creates a new Krberror.
  37. func Errorf(err error, et, format string, a ...interface{}) Krberror {
  38. if e, ok := err.(Krberror); ok {
  39. e.EText = append([]string{fmt.Sprintf("%s: "+format, et, a)}, e.EText...)
  40. return e
  41. }
  42. return NewErrorf(et, format+": %v", a, err)
  43. }
  44. // NewErrorf creates a new Krberror from a formatted string.
  45. func NewErrorf(et, format string, a ...interface{}) Krberror {
  46. return Krberror{
  47. RootCause: et,
  48. EText: []string{fmt.Sprintf("%s: %s", et, fmt.Sprintf(format, a))},
  49. }
  50. }