-
Notifications
You must be signed in to change notification settings - Fork 21
/
errors.go
79 lines (65 loc) · 2.04 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package ts3
import (
"errors"
"fmt"
"strconv"
"strings"
)
var (
// ErrInvalidConnectHeader is returned by NewClient if the server
// doesn't respond with the required connection header.
ErrInvalidConnectHeader = errors.New("invalid connect header")
// ErrNilOption is returned by NewClient if an option is nil.
ErrNilOption = errors.New("nil option")
// ErrNotConnected is returned by Exec and ExecCmd if the client is not connected.
ErrNotConnected = errors.New("not connected")
// ErrTimeout is returned by Exec and ExecCmd if no response is received
// within the specified timeout duration.
ErrTimeout = errors.New("timeout")
)
// Error represents a error returned from the TeamSpeak 3 server.
type Error struct {
ID int
Msg string
Details map[string]interface{}
}
// NewError returns a new Error parsed from TeamSpeak 3 server response.
func NewError(matches []string) *Error {
e := &Error{Msg: Decode(matches[2])}
var err error
if e.ID, err = strconv.Atoi(matches[1]); err != nil {
// This should be impossible given it matched \d+ in the regexp.
e.ID = -1
}
if rem := strings.TrimSpace(matches[3]); rem != "" {
e.Details = make(map[string]interface{})
for _, s := range strings.Split(rem, " ") {
d := strings.SplitN(s, "=", 2)
v := Decode(d[0])
if i, err := strconv.Atoi(d[1]); err == nil {
e.Details[v] = i
} else {
e.Details[v] = Decode(d[1])
}
}
}
return e
}
func (e *Error) Error() string {
if len(e.Details) > 0 {
return fmt.Sprintf("%v %v (%v)", e.Msg, e.Details, e.ID)
}
return fmt.Sprintf("%v (%v)", e.Msg, e.ID)
}
// InvalidResponseError is the error returned when the response data was invalid.
type InvalidResponseError struct {
Reason string
Data []string
}
// NewInvalidResponseError returns a new InvalidResponseError from lines.
func NewInvalidResponseError(reason string, lines []string) *InvalidResponseError {
return &InvalidResponseError{Reason: reason, Data: lines}
}
func (e *InvalidResponseError) Error() string {
return fmt.Sprintf("%v (%+v)", e.Reason, e.Data)
}