@@ -134,15 +134,26 @@ func (api *API) OnError(code string, handle Handler) {
134134 api .exceptions = append (api .exceptions , exp )
135135}
136136
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+
137148// UnhandledException method is used to handle all unhandled exceptions
138149func (api * API ) UnhandledException (handle Handler ) {
139150 api .unhandled = handle
140151}
141152
142153// error variables to handle expected errors
143154var (
144- codeNotFound = "URL_NOT_FOUND"
145- codeUncaughtException = "UNCAUGHT_EXCEPTION"
155+ ErrCodeNotFound = "URL_NOT_FOUND"
156+ ErrCodeUncaughtException = "UNCAUGHT_EXCEPTION"
146157)
147158
148159// It's required handle for http module.
@@ -163,7 +174,7 @@ func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
163174 err := recover ()
164175 if err != nil {
165176 if ! ctx .end {
166- ctx .code = codeUncaughtException
177+ ctx .code = ErrCodeUncaughtException
167178 ctx .err = fmt .Errorf ("%v" , err )
168179 ctx .unhandledException ()
169180 }
@@ -173,7 +184,7 @@ func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
173184
174185 // STEP 2: execute all interceptors
175186 for _ , task := range api .interceptors {
176- if ctx .end || ctx .err != nil {
187+ if ctx .end || ctx .code != "" {
177188 break
178189 }
179190
@@ -183,7 +194,7 @@ func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
183194 // STEP 3: check routes
184195 urlPath := []byte (req .URL .Path )
185196 for _ , route := range api .routes {
186- if ctx .end || ctx .err != nil {
197+ if ctx .end || ctx .code != "" {
187198 break
188199 }
189200
@@ -208,7 +219,7 @@ func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
208219 // STEP 5: unhandled exceptions
209220 if ! ctx .end {
210221 if ctx .code == "" && ! ctx .found {
211- ctx .Throw (codeNotFound )
222+ ctx .Throw (ErrCodeNotFound )
212223 }
213224
214225 if api .unhandled != nil {
0 commit comments