added a controller to manage controller's errors
This commit is contained in:
		
							
								
								
									
										21
									
								
								controllers/error_handler.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								controllers/error_handler.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
package controllers
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	beego "github.com/beego/beego/v2/server/web"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func HandleControllerErrors(c beego.Controller, code int, err *error, data *map[string]interface{}, messages ...string) {
 | 
			
		||||
	for _, mess := range messages {
 | 
			
		||||
		fmt.Println(mess)
 | 
			
		||||
	}
 | 
			
		||||
	if data != nil {
 | 
			
		||||
		c.Data["json"] = data
 | 
			
		||||
	}
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		c.Data["json"] = map[string]string{"error": (*err).Error()}
 | 
			
		||||
	}
 | 
			
		||||
	c.Ctx.Output.SetStatus(code)
 | 
			
		||||
	c.ServeJSON()
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user