-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
163 lines (150 loc) · 4.45 KB
/
errors.go
File metadata and controls
163 lines (150 loc) · 4.45 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package aeio
import (
"encoding/json"
"fmt"
"log"
"net/http"
"runtime"
"strings"
)
const (
errDatastore = "error_datastore"
errKey = "error_key"
errMarshal = "error_marshal"
errRequest = "error_request"
errResource = "error_resource"
errResponse = "error_response"
errSchema = "error_schema"
errHttpCode = "error_http_code"
errUnknown = "error_unknown"
errUnmarshal = "error_unmarshal"
)
var (
errorInvalidPath = &complexError{
Name: errKey,
Desc: "The path used is not valid",
Code: http.StatusBadRequest,
}
errorDatastorePut = &complexError{
Name: errDatastore,
Desc: "Could not put to datastore",
Code: http.StatusInternalServerError,
}
errorDatastoreRead = &complexError{
Name: errDatastore,
Desc: "Could not get from datastore",
Code: http.StatusNotFound,
}
errorDatastoreDelete = &complexError{
Name: errDatastore,
Desc: "Could not delete in datastore",
Code: http.StatusInternalServerError,
}
errorDatastoreCount = &complexError{
Name: errDatastore,
Code: http.StatusInternalServerError,
}
errorDatastoreAncestorNotFound = &complexError{
Name: errDatastore,
Desc: "The ancestor for this key was not found",
Hint: "Verify that the path is valid",
Code: http.StatusBadRequest,
}
errorDatastoreInvalidCursor = &complexError{
Name: errDatastore,
Desc: "The cursor passed on the request is not valid for this datastore",
Hint: "Restart the listing pagination to get valid cursors",
Code: http.StatusBadRequest,
}
errorInvalidHttpStatusCode = &complexError{
Name: errHttpCode,
Code: http.StatusBadRequest,
}
errorResponseMarshal = &complexError{
Name: errMarshal,
Code: http.StatusInternalServerError,
}
errorRequestUnmarshal = &complexError{
Name: errUnmarshal,
Desc: "The body could not be interpreted as json and unmarshaled to data object",
Code: http.StatusBadRequest,
}
errorResponseWrite = &complexError{
Name: errResponse,
Code: http.StatusInternalServerError,
}
errorUnknown = &complexError{
Name: errUnknown,
Code: http.StatusInternalServerError,
Desc: "Got some error not well described by the framework",
}
errorResourceModelNotImplemented = &complexError{
Name: errResource,
Code: http.StatusBadRequest,
Desc: "The path has objects not implemented",
Hint: "Verify that the path requested has all elements implemented and or registered correctly",
}
errorInvalidResourceChild = &complexError{
Name: errSchema,
Desc: "The path has invalid objects hierarchy",
Code: http.StatusBadRequest,
Hint: "Verify that the path requested has all elements implemented and or registered correctly",
}
errorEmptyRequestBody = &complexError{
Name: errRequest,
Desc: "There was no data found on the request body",
Hint: "Check that the request is of the right method and valid json",
Code: http.StatusBadRequest,
}
errorRequestBodyRead = &complexError{
Name: errRequest,
Desc: "Error reading request body",
Hint: "Verify the presence of invalid characters",
Code: http.StatusBadRequest,
}
)
type complexError struct {
Name string `json:"name"` // limited error name strings, like codes for mapping
Desc string `json:"description"` // improved description of the problem for humans
Hint string `json:"hint"` // if it is a common user mistake try to educate
Where string `json:"stack"`
Code int `json:"-"` // http status code caused by this error
Debug string `json:"debug"` // original error message
cause error // original error, only added by .withCause()
}
func (e complexError) Error() string {
ej, err := json.Marshal(e)
if err != nil {
log.Fatalln(err)
}
return string(ej)
// return fmt.Sprintf(`name: "%s", description: "%s", hint: "%s", code: "%d", debug: "%s"`, e.Name, e.Desc, e.Hint, e.Code, e.Debug)
}
func (e complexError) withCause(cause error) complexError {
e.cause = cause
e.Debug = cause.Error()
return e
}
func (e complexError) withCode(code int) complexError {
e.Code = code
return e
}
func (e complexError) withStack(levels int) complexError {
stack := make([]string, 0)
for i := 0; i < levels; i++ {
_, file, line, _ := runtime.Caller(i)
stack = append(stack, fmt.Sprintf("%s:%d", file, line))
}
// js, _ := json.MarshalIndent(stack, "", " ")
// log.Print(string(js))
e.Where = fmt.Sprintf("%s", strings.Join(stack, " -- "))
return e
}
func (e complexError) withLog() complexError {
// log.Print(e.Error())
return e
}
func (e complexError) withHint(hint string) complexError {
e.Hint = hint
return e
}