-
Notifications
You must be signed in to change notification settings - Fork 0
/
serving-errors.go
81 lines (67 loc) · 1.62 KB
/
serving-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
80
81
package mvp
import (
"errors"
"fmt"
"net/http"
"github.com/andreyvit/mvp/flogger"
"github.com/andreyvit/mvp/httperrors"
)
func WritePlainError(w http.ResponseWriter, err error) {
code := httperrors.HTTPCode(err)
message := httperrors.HTTPMessage(err)
DisableCaching(w)
http.Error(w, message, code)
}
func WriteMethodNotAllowed(w http.ResponseWriter) {
DisableCaching(w)
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}
type clientNetworkingError struct {
Cause error
}
func (err clientNetworkingError) Error() string {
return fmt.Sprintf("client networking: %v", err.Cause)
}
func (err clientNetworkingError) Unwrap() error {
return err
}
func ClientNetworkingError(err error) error {
if err == nil {
return nil
}
return clientNetworkingError{err}
}
func IsClientNetworkingError(err error) bool {
var dummy *clientNetworkingError
return errors.As(err, &dummy)
}
func (rc *RC) Fail(err error) {
if err == nil {
return
}
if rc.err == nil {
rc.err = err
}
if IsClientNetworkingError(err) {
flogger.Log(rc, "NOTICE: %v", err)
} else {
flogger.Log(rc, "WARNING: %v", err)
}
}
type RenderingError struct {
Kind string
Template string
Cause error
}
func ViewRenderingError(templ string, cause error) error {
return &RenderingError{"view", templ, cause}
}
func PartialRenderingError(templ string, cause error) error {
return &RenderingError{"partial", templ, cause}
}
func (err *RenderingError) Error() string {
return fmt.Sprintf("rendering %v %s: %v", err.Kind, err.Template, err.Cause)
}
func (err *RenderingError) SkipStackOnPanic() bool {
return true
}