-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
61 lines (52 loc) · 1.11 KB
/
data.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
package views
import (
"html/template"
"log"
"github.com/TerrenceHo/simplephotohost.com/models"
)
const (
AlertLvlError = "danger"
AlertLvlWarning = "warning"
AlertLvlInfo = "info"
AlertLvlSuccess = "success"
// AlertMsgGeneric is displayed when any random error
// is encountered by our backend.
AlertMsgGeneric = "Something went wrong. Please try again, and contact us if the problem persists."
)
// Alert is used to render Bootstrap Alert messages in templates
type Alert struct {
Level string
Message string
}
// Data is the top level structure that views expect data
// to come in.
type Data struct {
Alert *Alert
User *models.User
CSRF template.HTML
Yield interface{}
}
func (d *Data) SetAlert(err error) {
if pErr, ok := err.(PublicError); ok {
d.Alert = &Alert{
Level: AlertLvlError,
Message: pErr.Public(),
}
} else {
log.Println(err)
d.Alert = &Alert{
Level: AlertLvlError,
Message: AlertMsgGeneric,
}
}
}
func (d *Data) AlertError(msg string) {
d.Alert = &Alert{
Level: AlertLvlError,
Message: msg,
}
}
type PublicError interface {
error
Public() string
}