Skip to content

Commit 683529c

Browse files
committed
Modify internal error handler
1 parent 837c19c commit 683529c

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

example/server/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ func main() {
3737

3838
srv := server.NewServer(server.NewConfig(), manager)
3939
srv.SetUserAuthorizationHandler(userAuthorizeHandler)
40-
srv.SetInternalErrorHandler(func(err error) {
41-
fmt.Println("OAuth2 Error:", err.Error())
40+
srv.SetInternalErrorHandler(func(r *http.Request, err error) {
41+
fmt.Println("OAuth2 Error:", r.RequestURI, err.Error())
4242
})
4343

4444
http.HandleFunc("/login", loginHandler)

server/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type RefreshingScopeHandler func(newScope, oldScope string) (allowed bool)
2929
type ResponseErrorHandler func(re *errors.Response)
3030

3131
// InternalErrorHandler Internal error handing
32-
type InternalErrorHandler func(err error)
32+
type InternalErrorHandler func(req *http.Request, err error)
3333

3434
// ClientFormHandler Get client data from form
3535
func ClientFormHandler(r *http.Request) (clientID, clientSecret string, err error) {

server/server.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,6 @@ func (s *Server) GetAuthorizeData(rt oauth2.ResponseType, ti oauth2.TokenInfo) (
228228
func (s *Server) GetErrorData(rerr, ierr error) (data map[string]interface{}, statusCode int) {
229229
if ierr != nil {
230230
rerr = errors.ErrServerError
231-
if fn := s.InternalErrorHandler; fn != nil {
232-
fn(ierr)
233-
}
234231
}
235232
re := &errors.Response{
236233
Error: rerr,
@@ -253,11 +250,18 @@ func (s *Server) GetErrorData(rerr, ierr error) (data map[string]interface{}, st
253250
}
254251

255252
// response redirect error
256-
func (s *Server) resRedirectError(w http.ResponseWriter, req *AuthorizeRequest, rerr, ierr error) (err error) {
253+
func (s *Server) resRedirectError(w http.ResponseWriter, r *http.Request, req *AuthorizeRequest, rerr, ierr error) (err error) {
257254
if req == nil {
258255
err = ierr
259256
return
260257
}
258+
if fn := s.InternalErrorHandler; fn != nil {
259+
verr := ierr
260+
if verr == nil {
261+
verr = rerr
262+
}
263+
fn(r, verr)
264+
}
261265
data, _ := s.GetErrorData(rerr, ierr)
262266
err = s.resRedirect(w, req, data)
263267
return
@@ -283,20 +287,20 @@ func (s *Server) HandleAuthorizeRequest(w http.ResponseWriter, r *http.Request)
283287
}()
284288
req, rerr, ierr := s.ValidationAuthorizeRequest(r)
285289
if rerr != nil || ierr != nil {
286-
err = s.resRedirectError(w, req, rerr, ierr)
290+
err = s.resRedirectError(w, r, req, rerr, ierr)
287291
return
288292
}
289293
userID, err := s.UserAuthorizationHandler(w, r)
290294
if err != nil {
291-
err = s.resRedirectError(w, req, nil, err)
295+
err = s.resRedirectError(w, r, req, nil, err)
292296
return
293297
} else if userID == "" {
294298
return
295299
}
296300
req.UserID = userID
297301
ti, rerr, ierr := s.GetAuthorizeToken(req)
298302
if rerr != nil || ierr != nil {
299-
err = s.resRedirectError(w, req, rerr, ierr)
303+
err = s.resRedirectError(w, r, req, rerr, ierr)
300304
return
301305
}
302306
err = s.resRedirect(w, req, s.GetAuthorizeData(req.ResponseType, ti))
@@ -460,19 +464,26 @@ func (s *Server) HandleTokenRequest(w http.ResponseWriter, r *http.Request) (err
460464
}()
461465
gt, tgr, rerr, ierr := s.ValidationTokenRequest(r)
462466
if rerr != nil || ierr != nil {
463-
err = s.resTokenError(w, rerr, ierr)
467+
err = s.resTokenError(w, r, rerr, ierr)
464468
return
465469
}
466470
ti, rerr, ierr := s.GetAccessToken(gt, tgr)
467471
if rerr != nil || ierr != nil {
468-
err = s.resTokenError(w, rerr, ierr)
472+
err = s.resTokenError(w, r, rerr, ierr)
469473
return
470474
}
471475
err = s.resToken(w, s.GetTokenData(ti))
472476
return
473477
}
474478

475-
func (s *Server) resTokenError(w http.ResponseWriter, rerr, ierr error) (err error) {
479+
func (s *Server) resTokenError(w http.ResponseWriter, r *http.Request, rerr, ierr error) (err error) {
480+
if fn := s.InternalErrorHandler; fn != nil {
481+
verr := ierr
482+
if verr == nil {
483+
verr = rerr
484+
}
485+
fn(r, verr)
486+
}
476487
data, statusCode := s.GetErrorData(rerr, ierr)
477488
s.resToken(w, data, statusCode)
478489
return

0 commit comments

Comments
 (0)