扫码订阅《 》或入驻星球,即可阅读文章!

GOLANG ROADMAP

阅读模式

  • 沉浸
  • 自动
  • 日常
首页
Go学习
  • Go学院

    • Go小课
    • Go视界
    • Go小考
    • Go实战
  • Go资源

    • 优质课程
    • 在线宝典
    • 资源下载
    • 帮找资源
训练营 🔥
  • Go体系课&实战训练营
  • 升值加薪陪跑训练营
Go求职
  • 求职刷题

    • 企业题库
    • 面试宝典
    • 求职面经
  • 求职服务

    • 内推互助
    • 求职助力
    • 内推公司
Go友会
  • 城市
  • 校园
推广返佣
  • 返佣排行
  • 返佣规则
  • 推广学院
实验区
  • Go周边
  • Go宝典

    • 推荐图书
    • 精品博文
  • Go开源

    • Go仓库
    • Go月刊
更多
  • 用户中心

    • 我的信息
    • 我的返佣
    • 我的消息
  • 玩转星球

    • 星球介绍
    • 星主权益
    • 吐槽专区
    • 成长记录
  • 合作交流

    • 商务合作
    • 讲师招募
    • 生态伙伴
author-avatar

GOLANG ROADMAP


首页
Go学习
  • Go学院

    • Go小课
    • Go视界
    • Go小考
    • Go实战
  • Go资源

    • 优质课程
    • 在线宝典
    • 资源下载
    • 帮找资源
训练营 🔥
  • Go体系课&实战训练营
  • 升值加薪陪跑训练营
Go求职
  • 求职刷题

    • 企业题库
    • 面试宝典
    • 求职面经
  • 求职服务

    • 内推互助
    • 求职助力
    • 内推公司
Go友会
  • 城市
  • 校园
推广返佣
  • 返佣排行
  • 返佣规则
  • 推广学院
实验区
  • Go周边
  • Go宝典

    • 推荐图书
    • 精品博文
  • Go开源

    • Go仓库
    • Go月刊
更多
  • 用户中心

    • 我的信息
    • 我的返佣
    • 我的消息
  • 玩转星球

    • 星球介绍
    • 星主权益
    • 吐槽专区
    • 成长记录
  • 合作交流

    • 商务合作
    • 讲师招募
    • 生态伙伴
  • beego简介

  • beego安装升级

  • 快速入门

  • beego的MVC架构-介绍

  • beego的MVC架构-model设计

  • beego的MVC架构-view设计

  • beego的MVC架构-controller设置

    • 参数配置
    • 路由设置
    • 控制器函数
    • XSRF过滤
    • 请求数据处理
    • session 控制
    • 过滤器
    • flash 数据
    • URL 构建
    • 多种格式数据输出
    • 表单数据验证
    • 错误处理
    • 日志处理
  • beego的模块设计

  • beego高级编程

  • 应用部署

  • beego第三方库

  • 应用例子

  • beego实用库

  • 其他

  • FAQ

扫码订阅《 》或入驻星球,即可阅读文章!

错误处理


beego官网

我们在做 Web 开发的时候,经常需要页面跳转和错误处理,beego 这方面也进行了考虑,通过 Redirect 方法来进行跳转:

func (this *AddController) Get() {
    this.Redirect("/", 302)
}
1
2
3

如何中止此次请求并抛出异常,beego 可以在控制器中这操作:

func (this *MainController) Get() {
    this.Abort("401")
    v := this.GetSession("asta")
    if v == nil {
        this.SetSession("asta", int(1))
        this.Data["Email"] = 0
    } else {
        this.SetSession("asta", v.(int)+1)
        this.Data["Email"] = v.(int)
    }
    this.TplName = "index.tpl"
}
1
2
3
4
5
6
7
8
9
10
11
12

这样 this.Abort("401") 之后的代码不会再执行,而且会默认显示给用户如下页面:

401.png

beego 框架默认支持 401、403、404、500、503 这几种错误的处理。用户可以自定义相应的错误处理,例如下面重新定义 404 页面:

func page_not_found(rw http.ResponseWriter, r *http.Request){
    t,_:= template.New("404.html").ParseFiles(beego.BConfig.WebConfig.ViewsPath+"/404.html")
    data :=make(map[string]interface{})
    data["content"] = "page not found"
    t.Execute(rw, data)
}
func main() {
    beego.ErrorHandler("404",page_not_found)
    beego.Router("/", &controllers.MainController{})
    beego.Run()
}
1
2
3
4
5
6
7
8
9
10
11

我们可以通过自定义错误页面 404.html 来处理 404 错误。

beego 更加人性化的还有一个设计就是支持用户自定义字符串错误类型处理函数,例如下面的代码,用户注册了一个数据库出错的处理页面:

func dbError(rw http.ResponseWriter, r *http.Request){
    t,_:= template.New("dberror.html").ParseFiles(beego.BConfig.WebConfig.ViewsPath+"/dberror.html")
    data :=make(map[string]interface{})
    data["content"] = "database is now down"
    t.Execute(rw, data)
}
func main() {
    beego.ErrorHandler("dbError",dbError)
    beego.Router("/", &controllers.MainController{})
    beego.Run()
}
1
2
3
4
5
6
7
8
9
10
11

一旦在入口注册该错误处理代码,那么你可以在任何你的逻辑中遇到数据库错误调用 this.Abort("dbError") 来进行异常页面处理。

# Controller 定义 Error

从 1.4.3 版本开始,支持 Controller 方式定义 Error 错误处理函数,这样就可以充分利用系统自带的模板处理,以及 context 等方法。

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) Error501() {
    c.Data["content"] = "server error"
    c.TplName = "501.tpl"
}
func (c *ErrorController) ErrorDb() {
    c.Data["content"] = "database is now down"
    c.TplName = "dberror.tpl"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

通过上面的例子我们可以看到,所有的函数都是有一定规律的,都是 Error 开头,后面的名字就是我们调用 Abort 的名字,例如 Error404 函数其实调用对应的就是 Abort("404")

我们就只要在 beego.Run 之前采用 beego.ErrorController 注册这个错误处理函数就可以了

package main
import (
    _ "btest/routers"
    "btest/controllers"
    "github.com/astaxie/beego"
)
func main() {
    beego.ErrorController(&controllers.ErrorController{})
    beego.Run()
}
1
2
3
4
5
6
7
8
9
10