-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors.go
49 lines (40 loc) · 1.2 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
package kouch
import (
"fmt"
"os"
"github.com/go-kivik/couchdb/chttp"
"github.com/go-kivik/kivik"
)
// InitError returns an error for init failures.
type InitError string
func (i InitError) Error() string { return string(i) }
// ExitStatus returns ExitFailedToInitialize
func (i InitError) ExitStatus() int { return chttp.ExitFailedToInitialize }
type exitStatuser interface {
ExitStatus() int
}
// ExitStatus returns the exit status embedded in the error.
func ExitStatus(err error) int {
if err == nil {
return 0
}
if statuser, ok := err.(exitStatuser); ok { // nolint: misspell
return statuser.ExitStatus()
}
return chttp.ExitUnknownFailure
}
// Exit outputs err.Error() to stderr, then exits with the exit status embedded
// in the error.
func Exit(err error) {
msg, exitStatus := exit(err)
_, _ = fmt.Fprintf(os.Stderr, "kouch: (%d) %s\n", exitStatus, msg)
os.Exit(exitStatus)
}
func exit(err error) (string, int) {
exitStatus := ExitStatus(err)
httpStatus := kivik.StatusCode(err)
if exitStatus == chttp.ExitNotRetrieved && httpStatus >= 400 && httpStatus < 600 {
return fmt.Sprintf("The requested URL returned error: %d %s", httpStatus, err), exitStatus
}
return err.Error(), exitStatus
}