Add error handler middleware

This commit is contained in:
Howl 2016-04-07 11:38:57 +02:00
parent af71442e79
commit ae743cb395
2 changed files with 22 additions and 1 deletions

21
app/error_handler.go Normal file
View File

@ -0,0 +1,21 @@
package app
import (
"github.com/fatih/color"
"github.com/gin-gonic/gin"
)
// ErrorHandler is a middleware for gin that takes care of calls to c.Error().
func ErrorHandler() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
errs := c.Errors.Errors()
if len(errs) != 0 {
color.Red("!!! ERRORS OCCURRED !!!")
color.Red("==> %s %s", c.Request.Method, c.Request.URL.Path)
for _, err := range errs {
color.Red("===> %s", err)
}
}
}
}

View File

@ -12,7 +12,7 @@ import (
// Start begins taking HTTP connections. // Start begins taking HTTP connections.
func Start(conf common.Conf, db *sql.DB) { func Start(conf common.Conf, db *sql.DB) {
r := gin.Default() r := gin.Default()
r.Use(gzip.Gzip(gzip.DefaultCompression)) r.Use(gzip.Gzip(gzip.DefaultCompression), ErrorHandler())
api := r.Group("/api") api := r.Group("/api")
{ {