Merge pull request #133 from unistack-org/eh
fix error handler
This commit was merged in pull request #133.
	This commit is contained in:
		
							
								
								
									
										6
									
								
								http.go
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								http.go
									
									
									
									
									
								
							| @@ -602,11 +602,15 @@ func (h *httpServer) Name() string { | |||||||
|  |  | ||||||
| func NewServer(opts ...server.Option) *httpServer { | func NewServer(opts ...server.Option) *httpServer { | ||||||
| 	options := server.NewOptions(opts...) | 	options := server.NewOptions(opts...) | ||||||
|  | 	eh := DefaultErrorHandler | ||||||
|  | 	if v, ok := options.Context.Value(errorHandlerKey{}).(errorHandler); ok && v != nil { | ||||||
|  | 		eh = v | ||||||
|  | 	} | ||||||
| 	return &httpServer{ | 	return &httpServer{ | ||||||
| 		opts:         options, | 		opts:         options, | ||||||
| 		exit:         make(chan chan error), | 		exit:         make(chan chan error), | ||||||
| 		subscribers:  make(map[*httpSubscriber][]broker.Subscriber), | 		subscribers:  make(map[*httpSubscriber][]broker.Subscriber), | ||||||
| 		errorHandler: DefaultErrorHandler, | 		errorHandler: eh, | ||||||
| 		pathHandlers: rhttp.NewTrie(), | 		pathHandlers: rhttp.NewTrie(), | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
| @@ -68,10 +68,12 @@ func Server(hs *http.Server) server.Option { | |||||||
| 	return server.SetOption(serverKey{}, hs) | 	return server.SetOption(serverKey{}, hs) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | type errorHandler func(ctx context.Context, s server.Handler, w http.ResponseWriter, r *http.Request, err error, status int) | ||||||
|  |  | ||||||
| type errorHandlerKey struct{} | type errorHandlerKey struct{} | ||||||
|  |  | ||||||
| // ErrorHandler specifies handler for errors | // ErrorHandler specifies handler for errors | ||||||
| func ErrorHandler(fn func(ctx context.Context, s server.Handler, w http.ResponseWriter, r *http.Request, err error, status int)) server.Option { | func ErrorHandler(fn errorHandler) server.Option { | ||||||
| 	return server.SetOption(errorHandlerKey{}, fn) | 	return server.SetOption(errorHandlerKey{}, fn) | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user