-
Notifications
You must be signed in to change notification settings - Fork 6
/
errors.go
105 lines (87 loc) · 2.67 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
package page
import (
"io"
"time"
)
const MaxStackDepth = 50
// TODO: move to its own package so it is accessible from the entire framework
// The error structure, specifically designed to manage panics during request handling
type Error struct {
// the error string
Err error
// the time the error occurred
Time time.Time
// the copied context when the error occurred
Ctx *Context
// unwound Stack info
Stack []StackFrame
}
// StackFrame holds the file, line and function name in a call chain
type StackFrame struct {
File string
Line int
Func string
}
// DbError represents a database error.
type DbError struct {
Error
// DbStatement is the captured database statement if one caused the error, returned by the db adapter
DbStatement string
}
// NoErr represents no error. A request starts with this.
type NoErr struct {
}
// Known application specific errors
const (
FrameworkErrNone = iota
// FrameworkErrNoTemplate indicates a template does not exist. The control will move on to other ways of rendering.
// No message should be displayed.
FrameworkErrNoTemplate
// FrameworkErrRecordNotFound is a rare situation that might come up as a race condition error between viewing a
// record, and actually editing it. If in the time between clicking on a record to see detail, and viewing the detail,
// the record was deleted by another user, we would return this error.
// In a REST environment, this is 404 error
FrameworkErrRecordNotFound
)
// FrameworkError is an expected error that is part of the framework. Usually you would respond to the error
// by displaying a message to the user, but not always.
type FrameworkError struct {
Err int
Location string
Message string // optional message
}
// NewFrameworkError creates a new FrameworkError
func NewFrameworkError(err int) FrameworkError {
return FrameworkError{Err: err}
}
func (e FrameworkError) SetMessage(msg string) FrameworkError {
e.Message = msg
return e
}
// Error returns the error string
func (e FrameworkError) Error() string {
if e.Message != "" {
return e.Message
}
switch e.Err {
case FrameworkErrNoTemplate:
return "FormBase or control does not have a template" // just detected, this is not likely to be used
case FrameworkErrRecordNotFound:
return "Record does not exist."
}
return ""
}
// HttpError returns the corresponding http error
func (e FrameworkError) HttpError() int {
switch e.Err {
case FrameworkErrRecordNotFound: return 404
}
return 500
}
func (e *NoErr) Error() string {
return ""
}
// WriteString is a utility function that will write a string and panic if an error occurs
func WriteString(w io.Writer, s string) {
if _, err := io.WriteString(w, s); err != nil {panic(err)}
}