...
1
2
3
4
5
6
7
8
9
10
11
12
13 package cdb
14
15 import (
16 "errors"
17 "net/http"
18 "os"
19 )
20
21 var (
22 errNotFound = statusError{status: http.StatusNotFound, error: errors.New("missing")}
23 errUnrecognizedFile = errors.New("unrecognized file")
24 errConflict = statusError{status: http.StatusConflict, error: errors.New("document update conflict")}
25 )
26
27
28
29 func missing(err error) error {
30 if !os.IsNotExist(err) {
31 return err
32 }
33 return errNotFound
34 }
35
36 func kerr(err error) error {
37 if err == nil {
38 return nil
39 }
40 if errors.Is(err, &statusError{}) {
41
42 return err
43 }
44 if os.IsNotExist(err) {
45 return statusError{status: http.StatusNotFound, error: err}
46 }
47 if os.IsPermission(err) {
48 return statusError{status: http.StatusForbidden, error: err}
49 }
50 return err
51 }
52
53 type statusError struct {
54 error
55 status int
56 }
57
58 func (e statusError) Unwrap() error { return e.error }
59 func (e statusError) HTTPStatus() int { return e.status }
60
View as plain text