@@ -134,15 +134,26 @@ func (api *API) OnError(code string, handle Handler) {
134
134
api .exceptions = append (api .exceptions , exp )
135
135
}
136
136
137
+ // OnErrors method is used to handle a custom errors thrown by users
138
+ func (api * API ) OnErrors (codes []string , handle Handler ) {
139
+ for _ , code := range codes {
140
+ exp := exception {
141
+ code : code ,
142
+ handle : handle ,
143
+ }
144
+ api .exceptions = append (api .exceptions , exp )
145
+ }
146
+ }
147
+
137
148
// UnhandledException method is used to handle all unhandled exceptions
138
149
func (api * API ) UnhandledException (handle Handler ) {
139
150
api .unhandled = handle
140
151
}
141
152
142
153
// error variables to handle expected errors
143
154
var (
144
- codeNotFound = "URL_NOT_FOUND"
145
- codeUncaughtException = "UNCAUGHT_EXCEPTION"
155
+ ErrCodeNotFound = "URL_NOT_FOUND"
156
+ ErrCodeUncaughtException = "UNCAUGHT_EXCEPTION"
146
157
)
147
158
148
159
// It's required handle for http module.
@@ -163,7 +174,7 @@ func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
163
174
err := recover ()
164
175
if err != nil {
165
176
if ! ctx .end {
166
- ctx .code = codeUncaughtException
177
+ ctx .code = ErrCodeUncaughtException
167
178
ctx .err = fmt .Errorf ("%v" , err )
168
179
ctx .unhandledException ()
169
180
}
@@ -173,7 +184,7 @@ func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
173
184
174
185
// STEP 2: execute all interceptors
175
186
for _ , task := range api .interceptors {
176
- if ctx .end || ctx .err != nil {
187
+ if ctx .end || ctx .code != "" {
177
188
break
178
189
}
179
190
@@ -183,7 +194,7 @@ func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
183
194
// STEP 3: check routes
184
195
urlPath := []byte (req .URL .Path )
185
196
for _ , route := range api .routes {
186
- if ctx .end || ctx .err != nil {
197
+ if ctx .end || ctx .code != "" {
187
198
break
188
199
}
189
200
@@ -208,7 +219,7 @@ func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
208
219
// STEP 5: unhandled exceptions
209
220
if ! ctx .end {
210
221
if ctx .code == "" && ! ctx .found {
211
- ctx .Throw (codeNotFound )
222
+ ctx .Throw (ErrCodeNotFound )
212
223
}
213
224
214
225
if api .unhandled != nil {
0 commit comments