-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
130 lines (106 loc) · 2.72 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package controllers
import (
"strings"
"turm/app"
"github.com/revel/revel"
)
/*ErrorType is a type for encoding different errors. */
type ErrorType int
const (
errValidation ErrorType = iota
errDB
errAuth
errEMail
errTypeConv
errContent
)
func (s ErrorType) String() string {
return [...]string{"validation failed", "error.db",
"error.auth", "e-mail error", "error.typeConv",
"error.content"}[s]
}
//flashError flashes an error message and redirects to a page.
func flashError(errType ErrorType, err error, url string, c *revel.Controller, i interface{}) revel.Result {
//muted errors are e.g. caused by erroneous URLs
c.FlashParams()
if err != nil { //log error and send notification e-mail
if !strings.Contains(err.Error(), "muted error") {
app.SendErrorNote()
}
}
if url == "" {
url = c.Session["currPath"].(string)
}
if revel.DevMode {
c.Log.Error(errType.String(), "redirect", url)
}
//execute the correct error action
switch errType {
case errAuth, errDB, errTypeConv, errContent:
c.Flash.Error(c.Message(errType.String()))
case errValidation:
c.Validation.Keep()
case errEMail:
email, parsed := i.(string)
if !parsed {
c.Log.Error("error parsing e-mail", "email", email)
}
c.Flash.Error(c.Message("error.email", email))
}
return c.Redirect(url)
}
//renderError renders a template containing the error.
func renderError(err error, c *revel.Controller) revel.Result {
if err != nil { //log error and send notification e-mail
c.Log.Error("rendering error", "err", err.Error())
app.SendErrorNote()
}
templatePath := "errors/render.html"
c.ViewArgs["errMsg"] = c.Message("error.content")
c.Validation.Keep()
c.Log.Warn("render", "path", templatePath)
return c.RenderTemplate(templatePath)
}
//renderQuietError renders an error message.
func renderQuietError(errType ErrorType, err error, c *revel.Controller) {
if err != nil { //log error and send notification e-mail
c.Log.Error("rendering quiet error", "err", err.Error())
app.SendErrorNote()
}
switch errType {
case errDB:
c.ViewArgs["errMsg"] = c.Message("error.db")
default:
c.ViewArgs["errMsg"] = c.Message("error.undefined")
}
}
//append all validation errors
func getErrorString(errs []*revel.ValidationError) (str string) {
for i, err := range errs {
if i != 0 {
str += "<br>"
}
str += (*err).String()
}
return
}
//response of an ajax request
type response struct {
Status string
Msg string
FieldID string
Valid bool
Value string
ID int
//used to render fullness correctly if users got auto enrolled
//from the wait list
Fullness string
}
const (
//SUCCESS result type
SUCCESS = "success"
//ERROR result type
ERROR = "error"
//INVALID validation result type
INVALID = "invalid"
)