Add error handler middleware
This commit is contained in:
		
							
								
								
									
										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. | ||||
| func Start(conf common.Conf, db *sql.DB) { | ||||
| 	r := gin.Default() | ||||
| 	r.Use(gzip.Gzip(gzip.DefaultCompression)) | ||||
| 	r.Use(gzip.Gzip(gzip.DefaultCompression), ErrorHandler()) | ||||
|  | ||||
| 	api := r.Group("/api") | ||||
| 	{ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user