summaryrefslogtreecommitdiffstats
path: root/error.go
blob: b6270b90776143d7a705387ea3610f9ce5cbf7de (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package cnp

// Error represents an error as used in a CNP error response.
type Error interface {
	// CNPError returns the value of the "error" parameter in a CNP error
	// response.
	CNPError() string

	Error() string
}

const (
	// ReasonSyntax represents the "syntax" reason parameter value.
	ReasonSyntax = "syntax"
	// ReasonVersion represents the "version" reason parameter value.
	ReasonVersion = "version"
	// ReasonInvalid represents the "invalid" reason parameter value.
	ReasonInvalid = "invalid"
	// ReasonNotSupported represents the "not_supported" reason parameter value.
	ReasonNotSupported = "not_supported"
	// ReasonTooLarge represents the "too_large" reason parameter value.
	ReasonTooLarge = "too_large"
	// ReasonNotFound represents the "not_found" reason parameter value.
	ReasonNotFound = "not_found"
	// ReasonDenied represents the "denied" reason parameter value.
	ReasonDenied = "denied"
	// ReasonRejected represents the "rejected" reason parameter value.
	ReasonRejected = "rejected"
	// ReasonServerError represents the "server_error" reason parameter value.
	ReasonServerError = "server_error"
)

// NewError returns a new Error based on a reason parameter value.
//
// If the reason is blank, nil is returned.
// If the reason is unknown, ErrorServerError is returned.
func NewError(reason string) Error {
	switch reason {
	case ReasonSyntax:
		return ErrorSyntax{"Invalid CNP Message Syntax"}
	case ReasonVersion:
		return ErrorVersion{"Unsupported CNP Protocol Version"}
	case ReasonInvalid:
		return ErrorInvalid{"Invalid CNP Message"}
	case ReasonNotSupported:
		return ErrorNotSupported{"CNP Feature Not Supported"}
	case ReasonTooLarge:
		return ErrorTooLarge{"CNP Message Too Large"}
	case ReasonNotFound:
		return ErrorNotFound{"Not Found"}
	case ReasonDenied:
		return ErrorDenied{"Denied"}
	case ReasonRejected:
		return ErrorRejected{"Rejected"}
	case "":
		return nil
	default:
		fallthrough
	case ReasonServerError:
		return ErrorServerError{"Internal Server Error"}
	}
}

// ErrorSyntax represents the CNP "syntax" error reason.
type ErrorSyntax struct {
	Reason string
}

// CNPError on ErrorSyntax returns the error parameter value "syntax".
func (e ErrorSyntax) CNPError() string {
	return ReasonSyntax
}

func (e ErrorSyntax) Error() string {
	return "CNP syntax error: " + e.Reason
}

// ErrorVersion represents the CNP "version" error reason.
type ErrorVersion struct {
	Reason string
}

// CNPError on ErrorVersion returns the error parameter value "version".
func (e ErrorVersion) CNPError() string {
	return ReasonVersion
}

func (e ErrorVersion) Error() string {
	return "Unsupported CNP version: " + e.Reason
}

// ErrorInvalid represents the CNP "invalid" error reason.
type ErrorInvalid struct {
	Reason string
}

// CNPError on ErrorInvalid returns the error parameter value "invalid".
func (e ErrorInvalid) CNPError() string {
	return ReasonInvalid
}

func (e ErrorInvalid) Error() string {
	return "Invalid CNP message: " + e.Reason
}

// ErrorNotSupported represents the CNP "not_supported" error reason.
type ErrorNotSupported struct {
	Reason string
}

// CNPError on ErrorNotSupported returns the error parameter value
// "not_supported".
func (e ErrorNotSupported) CNPError() string {
	return ReasonNotSupported
}

func (e ErrorNotSupported) Error() string {
	return "Requested CNP feature is not supported: " + e.Reason
}

// ErrorTooLarge represents the CNP "too_large" error reason.
type ErrorTooLarge struct {
	Reason string
}

// CNPError on ErrorTooLarge returns the error parameter value "too_large".
func (e ErrorTooLarge) CNPError() string {
	return ReasonTooLarge
}

func (e ErrorTooLarge) Error() string {
	return "CNP message is too large: " + e.Reason
}

// ErrorNotFound represents the CNP "not_found" error reason.
type ErrorNotFound struct {
	Reason string
}

// CNPError on ErrorNotFound returns the error parameter value "not_found".
func (e ErrorNotFound) CNPError() string {
	return ReasonNotFound
}

func (e ErrorNotFound) Error() string {
	return "Requested path was not found: " + e.Reason
}

// ErrorDenied represents the CNP "denied" error reason.
type ErrorDenied struct {
	Reason string
}

// CNPError on ErrorDenied returns the error parameter value "denied".
func (e ErrorDenied) CNPError() string {
	return ReasonDenied
}

func (e ErrorDenied) Error() string {
	return "Server denied access: " + e.Reason
}

// ErrorRejected represents the CNP "rejected" error reason.
type ErrorRejected struct {
	Reason string
}

// CNPError on ErrorRejected returns the error parameter value "rejected".
func (e ErrorRejected) CNPError() string {
	return ReasonRejected
}

func (e ErrorRejected) Error() string {
	return "Server rejected request: " + e.Reason
}

// ErrorServerError represents the CNP "server_error" error reason.
type ErrorServerError struct {
	Reason string
}

// CNPError on ErrorServerError returns the error parameter value
// "server_error".
func (e ErrorServerError) CNPError() string {
	return ReasonServerError
}

func (e ErrorServerError) Error() string {
	return "Internal server error: " + e.Reason
}

// ErrorURL is a non-CNPError that represents an invalid CNP URL.
type ErrorURL struct {
	// Err represents the error reason.
	Err error
	// URL is the URL that triggered the error.
	URL string
}

func (e ErrorURL) Error() string {
	if e.Err == nil {
		return "CNP URL error"
	}
	return e.Err.Error()
}