-
Notifications
You must be signed in to change notification settings - Fork 62
/
types.go
111 lines (91 loc) · 2.42 KB
/
types.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package pnerr
import (
"fmt"
"io"
"io/ioutil"
)
// Error validating type or value of passed in params.
// For ex. Channel Missing, Subscribe Key Missing, etc.
// In most cases this happens due an incorrect SDK usage.
// If you don't switch over this error type, you should be sure enough
// that passed in params are correct.
type ValidationError struct {
message string
}
func (e ValidationError) OrigErr() error {
return nil
}
func (e ValidationError) Error() string {
return fmt.Sprintf("pubnub/validation: %s", e.message)
}
func NewValidationError(endpoint, msg string) *ValidationError {
return &ValidationError{
message: fmt.Sprintf("pubnub: %s: %s", endpoint, msg),
}
}
// Error building request with wrong params
type BuildRequestError struct {
message string
}
func (e BuildRequestError) OrigErr() error {
return nil
}
func (e BuildRequestError) Error() string {
return fmt.Sprintf("pubnub/build: %s", e.message)
}
func NewBuildRequestError(msg string) *BuildRequestError {
return &BuildRequestError{
message: msg,
}
}
// Server response has error code, for ex.:
// - BadRequest (400) - wrong params generated by SDK
// - Access Denied (403) - insufficient PAM permissions
type ServerError struct {
StatusCode int
Body []byte
}
func (e ServerError) Error() string {
return fmt.Sprintf(
"pubnub/server: Server respond with error code %d: %s", e.StatusCode,
string(e.Body))
}
func NewServerError(statusCode int, body io.ReadCloser) *ServerError {
bodyString, _ := ioutil.ReadAll(body)
return &ServerError{
StatusCode: statusCode,
Body: bodyString,
}
}
// Something wrong with network connection.
type ConnectionError struct {
message string
OrigError error
}
func (e ConnectionError) Error() string {
return fmt.Sprintf("pubnub/connection: %s: %s", e.message,
e.OrigError.Error())
}
func NewConnectionError(msg string, origError error) *ConnectionError {
return &ConnectionError{
message: msg,
OrigError: origError,
}
}
// Malformed request or issues with decoding encrypted message
type ResponseParsingError struct {
message string
Body io.ReadCloser
OrigError error
}
func (e ResponseParsingError) Error() string {
return fmt.Sprintf("pubnub/parsing: %s: %s", e.message, e.Body)
}
func NewResponseParsingError(msg string,
body io.ReadCloser, origError error) *ResponseParsingError {
return &ResponseParsingError{
message: msg,
Body: body,
OrigError: origError,
}
}