-
Notifications
You must be signed in to change notification settings - Fork 3
/
error.go
176 lines (163 loc) · 7.47 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
// Package errs provides structured error handling for Encore applications.
//
// See https://encore.dev/docs/develop/errors for more information about how errors work within Encore applications.
package errs
import (
"net/http"
)
// An Error is an error that provides structured information
// about the error. It includes an error code, a message,
// optionally additional structured details about the error
// and arbitrary key-value metadata.
//
// The Details field is returned to external clients.
// The Meta field is only exposed to internal calls within Encore.
//
// Internally it captures an underlying error for printing
// and for use with errors.Is/As and call stack information.
//
// To provide accurate stack information, users are expected
// to convert non-Error errors into *Error as close to the
// root cause as possible. This is made simple with Wrap.
type Error struct {
// Code is the error code to return.
Code ErrCode `json:"code"`
// Message is a descriptive message of the error.
Message string `json:"message"`
// Details are user-defined additional details.
Details ErrDetails `json:"details"`
// Meta are arbitrary key-value pairs for use within
// the Encore application. They are not exposed to external clients.
Meta Metadata `json:"-"`
}
// Metadata represents structured key-value pairs, for attaching arbitrary
// metadata to errors. The metadata is propagated between internal services
// but is not exposed to external clients.
type Metadata map[string]interface{}
// Wrap wraps the err, adding additional error information.
// If err is nil it returns nil.
//
// If err is already an *Error its code, message, and details
// are copied over to the new error.
func Wrap(err error, msg string, metaPairs ...interface{}) (_ error) {
// Encore will provide an implementation to this function at runtime, we do not expose
// the implementation in the API contract as it is an implementation detail, which may change
// between releases.
//
// The current implementation of this function can be found here:
// https://github.com/encoredev/encore/blob/v1.37.0/runtimes/go/beta/errs/error.go#L66-L82
doPanic("encore apps must be run using the encore command")
return
}
// WrapCode is like Wrap but also sets the error code.
// If code is OK it reports nil.
func WrapCode(err error, code ErrCode, msg string, metaPairs ...interface{}) (_ error) {
// Encore will provide an implementation to this function at runtime, we do not expose
// the implementation in the API contract as it is an implementation detail, which may change
// between releases.
//
// The current implementation of this function can be found here:
// https://github.com/encoredev/encore/blob/v1.37.0/runtimes/go/beta/errs/error.go#L86-L101
doPanic("encore apps must be run using the encore command")
return
}
// Convert converts an error to an *Error.
// If the error is already an *Error it returns it unmodified.
// If err is nil it returns nil.
func Convert(err error) (_ error) {
// Encore will provide an implementation to this function at runtime, we do not expose
// the implementation in the API contract as it is an implementation detail, which may change
// between releases.
//
// The current implementation of this function can be found here:
// https://github.com/encoredev/encore/blob/v1.37.0/runtimes/go/beta/errs/error.go#L106-L117
doPanic("encore apps must be run using the encore command")
return
}
// Code reports the error code from an error.
// If err is nil it reports OK.
// Otherwise if err is not an *Error it reports Unknown.
func Code(err error) (_ ErrCode) {
// Encore will provide an implementation to this function at runtime, we do not expose
// the implementation in the API contract as it is an implementation detail, which may change
// between releases.
//
// The current implementation of this function can be found here:
// https://github.com/encoredev/encore/blob/v1.37.0/runtimes/go/beta/errs/error.go#L122-L129
doPanic("encore apps must be run using the encore command")
return
}
// Meta reports the metadata included in the error.
// If err is nil or the error lacks metadata it reports nil.
func Meta(err error) (_ Metadata) {
// Encore will provide an implementation to this function at runtime, we do not expose
// the implementation in the API contract as it is an implementation detail, which may change
// between releases.
//
// The current implementation of this function can be found here:
// https://github.com/encoredev/encore/blob/v1.37.0/runtimes/go/beta/errs/error.go#L133-L138
doPanic("encore apps must be run using the encore command")
return
}
// Details reports the error details included in the error.
// If err is nil or the error lacks details it reports nil.
func Details(err error) (_ ErrDetails) {
// Encore will provide an implementation to this function at runtime, we do not expose
// the implementation in the API contract as it is an implementation detail, which may change
// between releases.
//
// The current implementation of this function can be found here:
// https://github.com/encoredev/encore/blob/v1.37.0/runtimes/go/beta/errs/error.go#L142-L147
doPanic("encore apps must be run using the encore command")
return
}
// Error reports the error code and message.
func (*Error) Error() (_ string) {
// Encore will provide an implementation to this function at runtime, we do not expose
// the implementation in the API contract as it is an implementation detail, which may change
// between releases.
//
// The current implementation of this function can be found here:
// https://github.com/encoredev/encore/blob/v1.37.0/runtimes/go/beta/errs/error.go#L150-L155
doPanic("encore apps must be run using the encore command")
return
}
// ErrorMessage reports the error message, joining this
// error's message with the messages from any underlying errors.
func (*Error) ErrorMessage() (_ string) {
// Encore will provide an implementation to this function at runtime, we do not expose
// the implementation in the API contract as it is an implementation detail, which may change
// between releases.
//
// The current implementation of this function can be found here:
// https://github.com/encoredev/encore/blob/v1.37.0/runtimes/go/beta/errs/error.go#L159-L183
doPanic("encore apps must be run using the encore command")
return
}
// Unwrap returns the underlying error, if any.
func (*Error) Unwrap() (_ error) {
// Encore will provide an implementation to this function at runtime, we do not expose
// the implementation in the API contract as it is an implementation detail, which may change
// between releases.
//
// The current implementation of this function can be found here:
// https://github.com/encoredev/encore/blob/v1.37.0/runtimes/go/beta/errs/error.go#L186-L188
doPanic("encore apps must be run using the encore command")
return
}
// HTTPError writes structured error information to w using JSON encoding.
// The status code is computed with HTTPStatus.
//
// If err is nil it writes:
//
// {"code": "ok", "message": "", "details": null}
func HTTPError(w http.ResponseWriter, err error) {
// Encore will provide an implementation to this function at runtime, we do not expose
// the implementation in the API contract as it is an implementation detail, which may change
// between releases.
//
// The current implementation of this function can be found here:
// https://github.com/encoredev/encore/blob/v1.37.0/runtimes/go/beta/errs/error.go#L196-L198
doPanic("encore apps must be run using the encore command")
return
}