blob: 01c6d9904c521d85b602fddbae306c75cd686e00 [file] [log] [blame]
khenaidood948f772021-08-11 17:49:24 -04001// Package krberror provides error type and functions for gokrb5.
2package krberror
3
4import (
5 "fmt"
6 "strings"
7)
8
9// Error type descriptions.
10const (
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
23type Krberror struct {
24 RootCause string
25 EText []string
26}
27
28// Error function to implement the error interface.
29func (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.
34func (e *Krberror) Add(et string, s string) {
35 e.EText = append([]string{fmt.Sprintf("%s: %s", et, s)}, e.EText...)
36}
37
38// New creates a new instance of Krberror.
39func New(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.
47func 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.
56func 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}