Handling HTTP errors in Beego
Error handling is one of the most important aspects in a web application design because it helps in two ways. Firstly, it lets the application user know in a relatively friendly manner that something has gone wrong and they should contact the technical support department or someone from tech support should be notified. Secondly, it allows the programmer to put in some niceties to aid in the debugging of issues. In this recipe, we will learn how we can implement error handling in Beego.
How to do it…
- Move to
$GOPATH/src/my-first-beego-project/controllers
and createerrorcontroller.go
, where we will define handlers to handle404
and500
HTTP errors as well as the handler to handle any generic error in an application, as follows:
package controllers import "github.com/astaxie/beego" type ErrorController struct { beego.Controller } func (c *ErrorController) Error404() { c.Data["content"] = "Page Not Found" c.TplName = "404.tpl" } func (c *ErrorController) Error500...