-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
94 lines (83 loc) · 1.96 KB
/
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
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
package main
import (
"bitbucket.org/mjl/sherpa"
"database/sql"
"encoding/json"
"log"
"runtime/debug"
"github.com/lib/pq"
)
func sherpaCheck(err error, msg string) {
if err == nil {
return
}
if pqe, ok := err.(*pq.Error); ok && !config.ShowSherpaErrors {
switch pqe.Code {
case "23503":
userError("References to this object still present in database.")
case "23505":
userError("Values are not unique.")
case "23514":
userError("Invalid value(s).")
}
}
m := msg
if m != "" {
m += ": "
}
m += err.Error()
if config.PrintSherpaErrorStack {
log.Println("sherpa serverError:", m)
debug.PrintStack()
}
if config.ShowSherpaErrors {
m = msg + ": " + err.Error()
} else {
m = "An error occurred. Please try again later or contact us."
}
serverError(m)
}
func serverError(m string) {
panic(&sherpa.Error{Code: "serverError", Message: m})
}
func sherpaUserCheck(err error, msg string) {
if err == nil {
return
}
m := msg
if m != "" {
m += ": "
}
m += err.Error()
if false && config.PrintSherpaErrorStack {
log.Println("sherpa userError:", m)
debug.PrintStack()
}
if config.ShowSherpaErrors {
m = msg + ": " + err.Error()
} else {
m = "An error occurred. Please try again later or contact us."
}
userError(m)
}
func userError(m string) {
panic(&sherpa.Error{Code: "userError", Message: m})
}
func sherpaCheckRow(row *sql.Row, r interface{}, msg string) {
var buf []byte
err := row.Scan(&buf)
if err == sql.ErrNoRows {
panic(&sherpa.Error{Code: "userNotFound", Message: "Not found"})
}
sherpaCheck(err, msg+": reading json from database row into buffer")
sherpaCheck(json.Unmarshal(buf, r), msg+": parsing json from database")
}
func checkRow(row *sql.Row, r interface{}, msg string) {
var buf []byte
err := row.Scan(&buf)
if err == sql.ErrNoRows {
log.Fatal("no row in result")
}
check(err, msg+": reading json from database row into buffer")
check(json.Unmarshal(buf, r), msg+": parsing json from database")
}