LoginSignup
2
2

More than 5 years have passed since last update.

Beego Error handingする

Last updated at Posted at 2015-10-04

1:controllersフォルダの下に下記のファイルを作成する

404と500とデータベースerrorを定義する

app/controllers/error.go
package controllers

import (
    "github.com/astaxie/beego"
)

type ErrorController struct {
    beego.Controller
}

func (c *ErrorController) Error404() {
    c.Data["content"] = "page not found"
    c.TplNames = "404.tpl"
}

func (c *ErrorController) Error500() {
    c.Data["content"] = "server error"
    c.TplNames = "500.tpl"
}

func (c *ErrorController) ErrorDb() {
    c.Data["content"] = "database is now down"
    c.TplNames = "dberror.tpl"
}

2:router.goに下記を追加する

routers/router.go
package routers

import (
    "github.com/app/controllers"
    "github.com/astaxie/beego"
)

func init() {
    beego.ErrorController(&controllers.ErrorController{})
    beego.Router("/", &controllers.MainController{})
}

3:viewsフォルダに500.tpl, 404.tpl, dberror.tplを作成

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2