Add error handler middleware
This commit is contained in:
parent
af71442e79
commit
ae743cb395
21
app/error_handler.go
Normal file
21
app/error_handler.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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")
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user