-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy patherrors.go
152 lines (137 loc) · 3.53 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
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
package kivik
import (
"errors"
"fmt"
"net/http"
"strings"
)
// Error represents an error returned by Kivik.
//
// This type definition is not guaranteed to remain stable, or even exported.
// When examining errors programatically, you should rely instead on the
// StatusCode() function in this package, rather than on directly observing
// the fields of this type.
type Error struct {
// HTTPStatus is the HTTP status code associated with this error. Normally
// this is the actual HTTP status returned by the server, but in some cases
// it may be generated by Kivik directly. Check the FromServer value if
// the distinction matters to you.
HTTPStatus int
// FromServer is set to true if the error was returned by the server.
// This field is deprecated and will soon be removed.
FromServer bool
// Message is the error message.
Message string
// Err is the originating error, if any.
Err error
}
var (
_ error = &Error{}
_ statusCoder = &Error{}
_ causer = &Error{}
)
func (e *Error) Error() string {
if e.Err == nil {
return e.msg()
}
if e.Message == "" {
return e.Err.Error()
}
return e.Message + ": " + e.Err.Error()
}
// StatusCode returns the HTTP status code associated with the error, or 500
// (internal server error), if none.
func (e *Error) StatusCode() int {
if e.HTTPStatus == 0 {
return http.StatusInternalServerError
}
return e.HTTPStatus
}
// Cause satisfies the github.com/pkg/errors.causer interface by returning e.Err.
func (e *Error) Cause() error {
return e.Err
}
// Unwrap satisfies the errors.Wrapper interface.
func (e *Error) Unwrap() error {
return e.Err
}
// Format implements fmt.Formatter
func (e *Error) Format(f fmt.State, c rune) {
parts := make([]string, 0, 3)
if e.Message != "" {
parts = append(parts, e.Message)
}
switch c {
case 'v':
if f.Flag('+') {
var prefix string
if e.FromServer {
prefix = "server responded with"
} else {
prefix = "kivik generated"
}
parts = append(parts, fmt.Sprintf("%s %d / %s", prefix, e.HTTPStatus, http.StatusText(e.HTTPStatus)))
}
}
if e.Err != nil {
parts = append(parts, e.Err.Error())
}
_, _ = fmt.Fprint(f, strings.Join(parts, ": "))
}
func (e *Error) msg() string {
switch e.Message {
case "":
return http.StatusText(e.StatusCode())
default:
return e.Message
}
}
type statusCoder interface {
StatusCode() int
}
type causer interface {
Cause() error
}
// StatusCode returns the HTTP status code embedded in the error, or 500
// (internal server error), if there was no specified status code. If err is
// nil, StatusCode returns 0. This provides a convenient way to determine the
// precise nature of a Kivik-returned error.
//
// For example, to panic for all but NotFound errors:
//
// err := db.Get(context.TODO(), "docID").ScanDoc(&doc)
// if kivik.StatusCode(err) == kivik.StatusNotFound {
// return
// }
// if err != nil {
// panic(err)
// }
//
// This method uses the statusCoder interface, which is not exported by this
// package, but is considered part of the stable public API. Driver
// implementations are expected to return errors which conform to this
// interface.
//
// type statusCoder interface {
// StatusCode() (httpStatusCode int)
// }
func StatusCode(err error) int {
if err == nil {
return 0
}
var coder statusCoder
for {
if errors.As(err, &coder) {
return coder.StatusCode()
}
if uw := errors.Unwrap(err); uw != nil {
err = uw
continue
}
if c, ok := err.(causer); ok {
err = c.Cause()
continue
}
return http.StatusInternalServerError
}
}