forked from go-kivik/kivik
/
errors.go
79 lines (69 loc) · 2.03 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 kivik
// Error represents an error returned by Kivik.
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.
FromServer bool
// Err is the originating error.
Err error
}
var _ error = &Error{}
var _ statusCoder = &Error{}
var _ causer = &Error{}
func (e *Error) Error() string {
return 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 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
}
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() int
// }
func StatusCode(err error) int {
if err == nil {
return 0
}
if coder, ok := err.(statusCoder); ok {
return coder.StatusCode()
}
return StatusInternalServerError
}