-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
70 lines (55 loc) · 1.66 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
package webmention
import (
"errors"
"fmt"
"net/http"
)
var (
ErrNotImplemented = errors.New("not implemented")
ErrNoEndpointFound = errors.New("no webmention endpoint found")
ErrNoRelWebmention = errors.New("no webmention relationship found")
ErrInvalidRelWebmention = errors.New("target has invalid webmention url")
ErrSourceDeleted = errors.New("source got deleted")
ErrSourceNotFound = errors.New("source not found")
ErrSourceDoesNotLinkToTarget = errors.New("source does not link to target")
)
type (
ErrorResponder interface {
RespondError(w http.ResponseWriter, r *http.Request) bool
}
ErrMethodNotAllowed struct{}
ErrBadRequest struct {
Message string
}
ErrTooManyRequests struct{}
)
func MethodNotAllowed() error {
return ErrMethodNotAllowed{}
}
func (e ErrMethodNotAllowed) Error() string {
return "method not allowed"
}
func (e ErrMethodNotAllowed) RespondError(w http.ResponseWriter, r *http.Request) bool {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return true
}
func BadRequest(msg string) error {
return ErrBadRequest{msg}
}
func (e ErrBadRequest) Error() string {
return fmt.Sprintf("bad request: %s", e.Message)
}
func (e ErrBadRequest) RespondError(w http.ResponseWriter, r *http.Request) bool {
http.Error(w, e.Error(), http.StatusBadRequest)
return true
}
func TooManyRequests() error {
return ErrTooManyRequests{}
}
func (e ErrTooManyRequests) Error() string {
return "too many requests"
}
func (e ErrTooManyRequests) RespondError(w http.ResponseWriter, r *http.Request) bool {
http.Error(w, e.Error(), http.StatusTooManyRequests)
return true
}