-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttperror.go
65 lines (55 loc) · 1.34 KB
/
httperror.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
package vbnet
import (
"fmt"
)
// HTTPError collects useful meta informations for network applications relying
// on the HTTP protocol
type HTTPError interface {
// Message is a textual description of the problem ready to send to the
// user
Message() string
// HTTPCode defines the code used when this error is returned over the HTTP
// protocol
HTTPCode() int
// Code is a unique internal error code that can be used to exactly
// aggregate single errors.
Code() int
// Inner is the internal error that caused this httpError
Inner() error
error
}
type httpErr struct {
message string
httpCode int
code int
inner error
}
func (err httpErr) Message() string {
return err.message
}
func (err httpErr) HTTPCode() int {
return err.httpCode
}
func (err httpErr) Code() int {
return err.code
}
func (err httpErr) Inner() error {
return err.inner
}
func (err httpErr) Error() string {
str := fmt.Sprintf("vbnet.%d: %s (HTTP %d)", err.code, err.message, err.httpCode)
if err.inner != nil {
str += fmt.Sprintf(", due-to: %v", err.inner)
}
return str
}
// NewHTTPError creates a new instance that implements the HTTPError interface
// and returns it
func NewHTTPError(message string, httpCode int, code int, inner error) HTTPError {
return httpErr{
message: message,
httpCode: httpCode,
code: code,
inner: inner,
}
}