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