Skip to content

Commit

Permalink
feat: add error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
thoas committed Oct 16, 2023
1 parent 7a7e2ad commit 10475e5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
3 changes: 2 additions & 1 deletion server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ func (s *HTTPServer) Init() error {
failure.Handle(handlers.delete))
}

router.GET("/error", handlers.internalError)
router.GET("/error", handlers.error)
router.GET("/panic", handlers.panic)

if s.config.Options.EnablePrometheus {
router.GET("/metrics", prometheusHandler())
Expand Down
12 changes: 11 additions & 1 deletion server/http_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ func (h handlers) stats(c *gin.Context) {
c.JSON(http.StatusOK, api.GetStats())
}

func (h handlers) internalError(c *gin.Context) {
func (h handlers) panic(c *gin.Context) {
panic(errors.WithStack(fmt.Errorf("KO")))
}

func (h handlers) error(c *gin.Context) {
c.JSON(http.StatusInternalServerError, struct {
Message string `json:"message"`
Time time.Time `json:"time"`
}{
Message: "Internal server error",
Time: time.Now(),
})
}

// healthcheck displays an ok response for healthcheck
func (h handlers) healthcheck(startedAt time.Time) func(c *gin.Context) {
return func(c *gin.Context) {
Expand Down

0 comments on commit 10475e5

Please sign in to comment.