beego 如何自定error
2024-09-05 12:10:01
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 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" } |
通过上面的例子我们可以看到,所有的函数都是有一定规律的,都是 Error
开头,后面的名字就是我们调用 Abort
的名字,
例如 Error404
函数其实调用对应的就是 Abort("404")。
我们就只要在 beego.Run
之前采用 beego.ErrorController
注册这个错误处理函数就可以了
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package main import ( _ "btest/routers" "btest/controllers" "github.com/astaxie/beego" ) func main() { beego.ErrorController(&controllers.ErrorController{}) beego.Run() } |
最新文章
- [LeetCode] Sort Characters By Frequency 根据字符出现频率排序
- [ASP.NET MVC 大牛之路]01 - 开篇
- B1/B2签证拒签
- oracle创建、删除账户
- iOS 核心动画
- Java for LeetCode 054 Spiral Matrix
- python 获取 mac 地址 的代码
- noip2002提高组题解
- Layout No collapsible
- NOIopenjudge 407:Heritage
- printf格式输出总结
- Request.ServerVariables完整参考
- Windows环境部署并调试pyspark(一)
- ⑥bootstrap表单使用基础案例
- C# Log4net记录日志
- SQL AUTO INCREMENT 字段
- Html一些特殊字符(Html语法字符)的一种表达方式
- Java大数相加(多个大数相加)-hdu1250
- C#数组和集合整理
- Android application 和 activity 标签详解
热门文章
- 使用Quartus进行功能仿真时出现“testbench_vector_input_file option does not exist”的解决方法
- Python3之正则表达式详解
- 关于.Net中Process和ProcessStartInfor的使用
- 【原】spring+mybatis下sqlSession.delete和insert返回值-2147482646问题
- yum和rpm工具使用
- [Python之路] 闭包
- (考试大整理~)Xxy 的车厢调度
- mac重启nginx时报nginx.pid不存在的解决办法
- Spring——代理实现AOP
- 微信小程序_(表单组件)button组件的使用