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 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() } |
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 ...
随机推荐
- HTTP协议几个版本的理解
HTTP (HyperText transfer protocol) 超文本传输协议 Http是一个应用层协议,基于TCP协议(传输层)之上,规定WWW服务器 浏览器之间信息传递规范.使用的默认端口号 ...
- ML paper 导图笔记.md
<Learning Structured Representation for Text Classification via Reinforcement Learning> <基于 ...
- Blade 模板
在Laravel 5.3中,@foreach指令提供了更加强大的功能,在每一个@foreach循环体中都可以调用一个新的$loop变量.该变量是一个stdClass实例,包含了当前循环的元数据信息,让 ...
- python Print 输出
print 默认输出是换行的,如果要实现不换行需要在变量末尾加上逗号 , #!/usr/bin/python # -*- coding: UTF-8 -*- x="a" y=&qu ...
- js的三种异步处理
js的三种异步处理 Promise 对象 含义: Promise是异步编程的一种解决方案, 优点: 相比传统回调函数和事件更加合理和优雅,Promise是链式编程(后面会详细讲述),有效的解决了令 ...
- web大文件上传断点续传源码
总结一下大文件分片上传和断点续传的问题.因为文件过大(比如1G以上),必须要考虑上传过程网络中断的情况.http的网络请求中本身就已经具备了分片上传功能,当传输的文件比较大时,http协议自动会将文件 ...
- E. Compress Words(Hash,KMP)
E. Compress Words time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- A. Odds and Ends(思维)
A. Odds and Ends time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Travis-CI自动化测试并部署至自己的CentOS服务器
一直都想自己部署一下自动化测试部署,在了解了Travis-CI之后终于准备在这次和小伙伴一起做的一个博客类网站实验下了. 因为这是一个前后端分离的项目,所以我这里只管前端工程的自动化部署,前端主要用V ...
- JavaScript 高级系列之节流 [throttle] 与防抖 [debounce]
一.概念 这两个东西都是为了项目优化而出现的,官方是没有具体定义的,他们的出现主要是为了解决一些短时间内连续执行的事件带来性能上的不佳和内存的消耗巨大等问题:像这类事件一般像 scroll keyup ...