beego 如何自定error
beego通过Redirect方法来进行跳转:
|
1
2
3
|
func (this *AddController) Get() { this.Redirect("/", 302)} |
如何终止此次请求并抛出异常,beego可以在控制器中这样操作:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
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"} |
这样 this.Abort("401") 之后的代码不会再执行,而且会默认显示给用户如下页面:

beego 框架默认支持 401、403、404、500、503 这几种错误的处理。用户可以自定义相应的错误处理,例如下面重新定义 404 页面:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
//定义处理404错误的函数<br>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) //如果是404错误返回什么 beego.Router("/", &controllers.MainController{}) //正常跳转 beego.Run()} |
我们可以通过自定义错误页面 404.html 来处理 404 错误。
beego 更加人性化的还有一个设计就是支持用户自定义字符串错误类型处理函数,
例如下面的代码,用户注册了一个数据库出错的处理页面:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
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()} |
一旦在入口注册该错误处理代码,那么你可以在任何你的逻辑中遇到数据库错误调用 this.Abort("dbError") 来进行异常页面处理。
Controller定义Error
从 1.4.3 版本开始,支持 Controller 方式定义 Error 错误处理函数,这样就可以充分利用系统自带的模板处理,以及 context 等方法。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package controllersimport ( "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"} |
通过上面的例子我们可以看到,所有的函数都是有一定规律的,都是 Error 开头,后面的名字就是我们调用 Abort 的名字,
例如 Error404 函数其实调用对应的就是 Abort("404")。
我们就只要在 beego.Run 之前采用 beego.ErrorController 注册这个错误处理函数就可以了
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package mainimport ( _ "btest/routers" "btest/controllers" "github.com/astaxie/beego")func main() { beego.ErrorController(&controllers.ErrorController{}) beego.Run()} |
beego 如何自定error的更多相关文章
- Golang通过git clone beego框架报错 error: while accessing https://github.com/astaxie/beego/info/refs fatal: HTTP request failed package github.com/astaxie/beego: exit status 128
在Centos6.4尝试搭建beego框架,使用git命令clone时报错 # cd .; git clone https://github.com/astaxie/beego /www/projec ...
- 用html5的视频元素所遇到的第一个坑
html5 有一个video标签,这个是被大家所熟知的事情.按照w3c的规范,我认真的写出如下代码: <video preload="auto" controls=" ...
- golang csv,xls,xlsx
要用到的包: "golang.org/x/text/encoding/simplifiedchinese" "golang.org/x/text/transform ...
- php源码建博客5--建库建表-配置文件-错误日志
主要: 整理框架 建库建表 配置文件类 错误日志记录 --------------本篇后文件结构:-------------------------------------- blog ├─App │ ...
- golang实现图片上传
golang实现图片上传 该代码为使用beego实现前后端图片上传.话不多说,直接上代码. 1.前端代码 html代码: <div class="col-5 f-l text text ...
- beego 使用连接mysql 报错 register db Ping `default1`, Error 1049: Unknown database 'test_beego' must have one register DataBase alias named `default`
项目移植到另一台电脑后出现以下问题,及其解决方法: package models import ( "github.com/astaxie/beego/orm" _ "g ...
- Beego源码分析(转)
摘要 beego 是 @astaxie 开发的重量级Go语言Web框架.它有标准的MVC模式,完善的功能模块,和优异的调试和开发模式等特点.并且beego在国内企业用户较多,社区发达和Q群,文档齐全, ...
- Go-并发和并行-协程-信道-缓冲信道-select-mutex-读写文件-beego框架
并发 Go 是并发式语言,而不是并行式语言.在讨论 Go 如何处理并发之前,我们必须理解何为并发,以及并发与并行的区别. 并发是什么? 并发是指立即处理多个任务的能力.一个CPU的情况下<意指看 ...
- 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)
前言 首先声明一下,我这个是对WebUploader开源上传控件的二次封装,底层还是WebUploader实现的,只是为了更简洁的使用他而已. 下面先介绍一下WebUploader 简介: WebUp ...
随机推荐
- Mysql索引类型分析
一.简介 MySQL目前主要有以下几种索引类型: 1.普通索引 2.唯一索引 3.主键索引 4.组合索引 5.全文索引 二.语句 CREATE TABLE table_na ...
- (八)zabbix获取到的数值自定义单位
1) 查找php文件 # find / -name "func.inc.php" /usr/share/zabbix/include/func.inc.php 2)修改文件 #vi ...
- 用python 遍历文件夹中所有.dcm文件
import dicomimport os def eachFile(filepath): for file in os.listdir(filepath): child = os.path.join ...
- 简单了解HTTP协议的基本知识,请求流程、请求方法等
HTTP 是Hyper Text Transfer Protocol(超文本传输协议)的缩写 1.超文本传输协议是一种详细规定了浏览器和万维网服务器之间互相通信的规则. 2.HTTP协议(HyperT ...
- Mybatis 解决问题的记录与博客
问题:mybatis 空值映射的问题Mybatis在使用resultMap来映射查询结果中的列,如果查询结果中包含空值的列(不是null),则Mybatis在映射的时候,不会映射这个字段 https: ...
- [Python]Python3调用java代码
环境:Ubuntu16.04 桌面版 Ubuntu安装java的详细教程:https://www.cnblogs.com/ttkl/p/11933884.html 安装JPype1 pip3 inst ...
- TensorFlow使用记录 (五): 激活函数和初始化方式
In general ELU > leaky ReLU(and its variants) > ReLU > tanh > logistic. If you care a lo ...
- Jmeter(十一)函数助手
可以在JMeter的选项菜单中找到函数助手对话框 我们可以从下拉列表中选择一个函数,并为其参数设定值.如图,表格的左边一列是函数参数的简要描述,右边一列是供用户填充参数的值.不同函数要求的参数也不同. ...
- Binary Protocol
A. Binary Protocol 这道题要唯一注意的一点就是数字0的表示--0个"1"来表达,所以字符串"100"所表示的数字就是100 附代码: // C ...
- nmap脚本nse的使用
nmap脚本(nse)使用总结 0x01 nmap按脚本分类扫描 nmap脚本主要分为以下几类,在扫描时可根据需要设置--script=类别这种方式进行比较笼统的扫描: auth: 负责处理鉴权证书( ...