error.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Package krberror provides error type and functions for gokrb5.
  2. package krberror
  3. import (
  4. "fmt"
  5. "strings"
  6. )
  7. // Error type descriptions.
  8. const (
  9. separator = " < "
  10. EncodingError = "Encoding_Error"
  11. NetworkingError = "Networking_Error"
  12. DecryptingError = "Decrypting_Error"
  13. EncryptingError = "Encrypting_Error"
  14. ChksumError = "Checksum_Error"
  15. KRBMsgError = "KRBMessage_Handling_Error"
  16. ConfigError = "Configuration_Error"
  17. KDCError = "KDC_Error"
  18. )
  19. // Krberror is an error type for gokrb5
  20. type Krberror struct {
  21. RootCause string
  22. EText []string
  23. }
  24. // Error function to implement the error interface.
  25. func (e Krberror) Error() string {
  26. return fmt.Sprintf("[Root cause: %s] ", e.RootCause) + strings.Join(e.EText, separator)
  27. }
  28. // Add another error statement to the error.
  29. func (e *Krberror) Add(et string, s string) {
  30. e.EText = append([]string{fmt.Sprintf("%s: %s", et, s)}, e.EText...)
  31. }
  32. // New creates a new instance of Krberror.
  33. func New(et, s string) Krberror {
  34. return Krberror{
  35. RootCause: et,
  36. EText: []string{s},
  37. }
  38. }
  39. // Errorf appends to or creates a new Krberror.
  40. func Errorf(err error, et, format string, a ...interface{}) Krberror {
  41. if e, ok := err.(Krberror); ok {
  42. e.Add(et, fmt.Sprintf(format, a...))
  43. return e
  44. }
  45. return NewErrorf(et, format+": %s", append(a, err)...)
  46. }
  47. // NewErrorf creates a new Krberror from a formatted string.
  48. func NewErrorf(et, format string, a ...interface{}) Krberror {
  49. var s string
  50. if len(a) > 0 {
  51. s = fmt.Sprintf("%s: %s", et, fmt.Sprintf(format, a...))
  52. } else {
  53. s = fmt.Sprintf("%s: %s", et, format)
  54. }
  55. return Krberror{
  56. RootCause: et,
  57. EText: []string{s},
  58. }
  59. }