55package rest
66
77import (
8- "errors"
9- "log"
8+ "fmt"
109 "net/http"
1110 "regexp"
1211 "strings"
@@ -48,8 +47,8 @@ type interceptor struct {
4847
4948// user exceptions, which is a common way to handle an error thrown by user
5049type exception struct {
51- message string
52- handle Handler
50+ code string
51+ handle Handler
5352}
5453
5554// Initialize an API with prefix value and return the API pointer
@@ -127,10 +126,10 @@ func (api *API) Patch(pattern string, handle Handler) {
127126}
128127
129128// OnError method is used to handle a custom errors thrown by users
130- func (api * API ) OnError (err string , handle Handler ) {
129+ func (api * API ) OnError (code string , handle Handler ) {
131130 exp := exception {
132- message : err ,
133- handle : handle ,
131+ code : code ,
132+ handle : handle ,
134133 }
135134 api .exceptions = append (api .exceptions , exp )
136135}
@@ -142,16 +141,16 @@ func (api *API) UnhandledException(handle Handler) {
142141
143142// error variables to handle expected errors
144143var (
145- errNotFound = errors . New ( "URL_NOT_FOUND" )
146- errUncaughtException = errors . New ( "UNCAUGHT_EXCEPTION" )
144+ codeNotFound = "URL_NOT_FOUND"
145+ codeUncaughtException = "UNCAUGHT_EXCEPTION"
147146)
148147
149148// It's required handle for http module.
150149// Every request travels from this method.
151150func (api * API ) ServeHTTP (res http.ResponseWriter , req * http.Request ) {
152151
153152 // STEP 1: initialize context
154- ctx := Context {
153+ ctx := & Context {
155154 Request : req ,
156155 Response : res ,
157156 Query : req .URL .Query (),
@@ -163,10 +162,9 @@ func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
163162 defer func () {
164163 err := recover ()
165164 if err != nil {
166- //TODO: log only with debugger mode
167- log .Println ("uncaught exception - " , err )
168- if ctx .end == false {
169- ctx .err = errUncaughtException
165+ if ! ctx .end {
166+ ctx .code = codeUncaughtException
167+ ctx .err = fmt .Errorf ("%v" , err )
170168 ctx .unhandledException ()
171169 }
172170 return
@@ -179,7 +177,7 @@ func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
179177 break
180178 }
181179
182- task .handle (& ctx )
180+ task .handle (ctx )
183181 }
184182
185183 // STEP 3: check routes
@@ -192,29 +190,29 @@ func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
192190 if (route .method == "" || strings .EqualFold (route .method , req .Method )) && route .regex .Match (urlPath ) {
193191 ctx .found = route .method != "" //?
194192 ctx .Params = utils .Exec (route .regex , route .params , urlPath )
195- route .handle (& ctx )
193+ route .handle (ctx )
196194 }
197195 }
198196
199197 // STEP 4: check handled exceptions
200198 for _ , exp := range api .exceptions {
201- if ctx .end || ctx .err == nil {
199+ if ctx .end || ctx .code == "" {
202200 break
203201 }
204202
205- if exp .message == ctx .err . Error () {
206- exp .handle (& ctx )
203+ if exp .code == ctx .code {
204+ exp .handle (ctx )
207205 }
208206 }
209207
210208 // STEP 5: unhandled exceptions
211209 if ! ctx .end {
212- if ctx .err == nil && ! ctx .found {
213- ctx .err = errNotFound
210+ if ctx .code == "" && ! ctx .found {
211+ ctx .Throw ( codeNotFound )
214212 }
215213
216214 if api .unhandled != nil {
217- api .unhandled (& ctx )
215+ api .unhandled (ctx )
218216 }
219217 }
220218
0 commit comments