19
19
* [ Middlewares] ( #middlewares )
20
20
* [ Handlers] ( #handlers )
21
21
* [ Handlers groups] ( #handlers-groups )
22
+ * [ Error Handler] ( #error-handler )
22
23
* [ WebSocket] ( #websocket )
23
24
* [ Templates] ( #templates )
24
25
* [ Override] ( #override )
@@ -126,7 +127,7 @@ modules:
126
127
namespace : foo # http server metrics namespace (empty by default)
127
128
subsystem : bar # http server metrics subsystem (empty by default)
128
129
buckets : 0.1, 1, 10 # to override default request duration buckets
129
- normalize :
130
+ normalize :
130
131
request_path : true # to normalize http request path, disabled by default
131
132
response_status : true # to normalize http response status code (2xx, 3xx, ...), disabled by default
132
133
templates :
@@ -145,7 +146,7 @@ Notes:
145
146
146
147
# ## Registration
147
148
148
- This module offers the possibility to easily register handlers, groups and middlewares .
149
+ This module offers the possibility to easily register handlers, groups, middlewares and error handlers .
149
150
150
151
# ### Middlewares
151
152
@@ -204,7 +205,7 @@ func main() {
204
205
fxmetrics.FxMetricsModule,
205
206
fxgenerate.FxGenerateModule,
206
207
fxhttpserver.FxHttpServerModule, // load the module
207
- fx.Provide (
208
+ fx.Options (
208
209
fxhttpserver.AsMiddleware(middleware.CORS(), fxhttpserver.GlobalUse), // register echo CORS middleware via echo.Use()
209
210
fxhttpserver.AsMiddleware(NewSomeMiddleware, fxhttpserver.GlobalPre), // register and autowire the SomeMiddleware via echo.Pre()
210
211
),
@@ -296,15 +297,15 @@ func main() {
296
297
fxmetrics.FxMetricsModule,
297
298
fxgenerate.FxGenerateModule,
298
299
fxhttpserver.FxHttpServerModule, // load the module
299
- fx.Provide (
300
+ fx.Options (
300
301
// register and autowire the SomeHandler handler for [GET] /some-path, with the SomeMiddleware and echo CORS() middlewares
301
302
fxhttpserver.AsHandler("GET", "/some-path", NewSomeHandler, NewSomeMiddleware, middleware.CORS()),
302
303
),
303
304
).Run()
304
305
}
305
306
` ` `
306
307
307
- Notes :
308
+ Notes :
308
309
309
310
- you can specify several valid HTTP methods (comma separated) while registering a handler, for example `fxhttpserver.AsHandler("GET,POST", ...)`
310
311
- you can use the shortcut `*` to register a handler for all valid HTTP methods, for example `fxhttpserver.AsHandler("*", ...)`
@@ -416,7 +417,7 @@ func main() {
416
417
fxmetrics.FxMetricsModule,
417
418
fxgenerate.FxGenerateModule,
418
419
fxhttpserver.FxHttpServerModule, // load the module
419
- fx.Provide (
420
+ fx.Options (
420
421
// register and autowire the SomeHandler handler with NewSomeMiddleware middleware for [GET] /group/some-path
421
422
// register and autowire the OtherHandler handler with echo CORS middleware for [POST] /group/other-path
422
423
// register the echo CSRF middleware for all handlers of this group
@@ -439,6 +440,67 @@ Notes:
439
440
- you can use the shortcut `*` to register a handler for all valid HTTP methods, for example `fxhttpserver.NewHandlerRegistration("*", ...)`
440
441
- valid HTTP methods are `CONNECT`, `DELETE`, `GET`, `HEAD`, `OPTIONS`, `PATCH`, `POST`, `PUT`, `TRACE`, `PROPFIND` and `REPORT`
441
442
443
+ # ### Error Handler
444
+
445
+ You can use the `AsErrorHandler()` function to register a custom error handler on your http server.
446
+
447
+ You can provide any [ErrorHandler](registry.go) interface implementation (will be autowired from Fx container)
448
+
449
+ ` ` ` go
450
+ package main
451
+
452
+ import (
453
+ "fmt"
454
+ "net/http"
455
+
456
+ "github.com/ankorstore/yokai/config"
457
+ "github.com/ankorstore/yokai/fxconfig"
458
+ "github.com/ankorstore/yokai/fxgenerate"
459
+ "github.com/ankorstore/yokai/fxhttpserver"
460
+ "github.com/ankorstore/yokai/fxlog"
461
+ "github.com/ankorstore/yokai/fxmetrics"
462
+ "github.com/ankorstore/yokai/fxtrace"
463
+ "github.com/ankorstore/yokai/httpserver"
464
+ "github.com/labstack/echo/v4"
465
+ "github.com/labstack/echo/v4/middleware"
466
+ "go.uber.org/fx"
467
+ )
468
+
469
+ type SomeErrorHandler struct {
470
+ config *config.Config
471
+ }
472
+
473
+ func NewSomeErrorHandler(config *config.Config) *SomeErrorHandler {
474
+ return &SomeErrorHandler{
475
+ config: config,
476
+ }
477
+ }
478
+
479
+ func (h *SomeErrorHandler) Handle() echo.HTTPErrorHandler {
480
+ return func(err error, c echo.Context) {
481
+ if c.Response().Committed {
482
+ return
483
+ }
484
+
485
+ c.String(http.StatusInternalServerError, fmt.Sprintf("error handled in %s: %s", h.config.AppName(), err))
486
+ }
487
+ }
488
+
489
+ func main() {
490
+ fx.New(
491
+ fxconfig.FxConfigModule, // load the module dependencies
492
+ fxlog.FxLogModule,
493
+ fxtrace.FxTraceModule,
494
+ fxmetrics.FxMetricsModule,
495
+ fxgenerate.FxGenerateModule,
496
+ fxhttpserver.FxHttpServerModule, // load the module
497
+ fx.Options(
498
+ fxhttpserver.AsErrorHandler(NewSomeErrorHandler), // register SomeErrorHandler as error handler
499
+ ),
500
+ ).Run()
501
+ }
502
+ ` ` `
503
+
442
504
# ## WebSocket
443
505
444
506
This module supports the `WebSocket` protocol, see the [Echo documentation](https://echo.labstack.com/docs/cookbook/websocket) for more information.
@@ -468,9 +530,9 @@ And the template:
468
530
` ` ` html
469
531
<!-- templates/app.html -->
470
532
<html>
471
- <body>
472
- <h1>App name is {{index . "name"}}</h1>
473
- </body>
533
+ <body>
534
+ <h1>App name is {{index . "name"}}</h1>
535
+ </body>
474
536
</html>
475
537
` ` `
476
538
@@ -521,7 +583,7 @@ func main() {
521
583
fxmetrics.FxMetricsModule,
522
584
fxgenerate.FxGenerateModule,
523
585
fxhttpserver.FxHttpServerModule, // load the module
524
- fx.Provide (
586
+ fx.Options (
525
587
fxhttpserver.AsHandler("GET", "/app", NewTemplateHandler),
526
588
),
527
589
).Run()
0 commit comments