/
error.go
56 lines (44 loc) · 916 Bytes
/
error.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
package main
import (
"fmt"
html "html/template"
"strings"
"github.com/russross/blackfriday"
)
var ef = fmt.Errorf
func assert(err error) {
if err != nil {
panic(err)
}
}
type userError struct {
error
}
func ue(format string, v ...interface{}) userError {
return userError{ef(format, v...)}
}
func (ue userError) Error() string {
return ue.error.Error()
}
type authError struct {
error
}
func ae(format string, v ...interface{}) authError {
return authError{ef(format, v...)}
}
func (ae authError) Error() string {
return ae.error.Error()
}
func formatMessage(s string) html.HTML {
fmtd := strings.TrimSpace(toMarkdown(html.HTMLEscapeString(s)))
if strings.HasPrefix(fmtd, "<p>") {
fmtd = fmtd[3:]
}
if strings.HasSuffix(fmtd, "</p>") {
fmtd = fmtd[:len(fmtd)-4]
}
return html.HTML(fmtd)
}
func toMarkdown(s string) string {
return string(blackfriday.MarkdownBasic([]byte(s)))
}