/
errors.go
51 lines (45 loc) · 1.39 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
package kt
import (
"net/http"
"github.com/flimzy/kivik/errors"
)
// CheckError compares the error's status code with that expected.
func (c *Context) CheckError(err error) (match bool, success bool) {
status := c.Int("status")
if status == 0 && err == nil {
return true, true
}
switch errors.StatusCode(err) {
case status:
// This is expected
return true, status == 0
case 0:
c.Errorf("Expected failure %d/%s", status, http.StatusText(status))
return false, true
default:
if status == 0 {
c.Errorf("Unexpected failure: %d/%s", errors.StatusCode(err), err)
return false, false
}
c.Errorf("Unexpected failure state.\nExpected: %d/%s\n Actual: %d/%s", status, http.StatusText(status), errors.StatusCode(err), err)
return false, false
}
}
// IsExpected checks the error against the expected status, and returns true
// if they match.
func (c *Context) IsExpected(err error) bool {
m, _ := c.CheckError(err)
return m
}
// IsSuccess is similar to IsExpected, except for its return value. This method
// returns true if the expected status == 0, regardless of the error.
func (c *Context) IsSuccess(err error) bool {
_, s := c.CheckError(err)
return s
}
// IsExpectedSuccess combines IsExpected() and IsSuccess(), returning true only
// if there is no error, and no error was expected.
func (c *Context) IsExpectedSuccess(err error) bool {
m, s := c.CheckError(err)
return m && s
}