yii2自定义500错误
由于项目想加预警监控,有一块儿是涉及到程序内部错误的500,这样的错误级别比较高,所以就需要捕获这样的错误,顺便自定义了一把视图样式
看了这篇博客,知道了如何去自定义自己错误页面 : http://tech.lubanr.com/2015/12/12/yii2-0框架的错误和异常处理机制/
如果我们需要定制自己的异常处理方式,需要做的就是继承yii\base\ErrorHandler,写一个定制的renderException,最后在$config中定制自己的errorHandler
1.创建ErrorHandler 继承这个yii\base\ErrorHandler抽象类,然后定义这个父类中的抽象方法
<?php
namespace common\component\exception;
/**
* User: szliugx@gmail.com
* Date: 2016/9/20
* Time: 14:24
*/
use yii;
use yii\base\ErrorHandler as BaseErrorHandler;
use common\component\earlywarning\EarlyWarning; class ErrorHandler extends BaseErrorHandler
{ public $errorView = '@app/views/errorHandler/error.php';
public function renderException($exception)
{
if(Yii::$app->request->getIsAjax()){
exit( json_encode( array('code' =>$exception->getCode(),'msg' =>$exception->getMessage()) ));
}else{
//将500的代码,发送监控预警
if(!empty($exception->getCode()) && $exception->getCode() ==8){
$params = [];
$params['projectName'] = "oct-youban";
$params['level'] = 5;
$params['title'] = "500:".$exception->getMessage();
$params['value'] = $exception->getCode();
$params['message'] = $exception->getFile().":".$exception->getLine();
$params['bizcode'] = 8;
$params['subcode'] = 8001;
EarlyWarning::WarninApi($params);
}
echo Yii::$app->getView()->renderFile($this->errorView,['exception' => $exception,],$this);
}
}
}
2.创建视图文件 : @app/views/errorHandler/error.php
<?php
/**
* User: szliugx@gmail.com
* Date: 2016/9/20
* Time: 15:23
*/
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<meta http-equiv="Expires" content="-1">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
<title><?php if(!empty($exception->getCode())&&($exception->getCode() == 8)){echo "出错啦";}else{ echo $exception->getMessage();}?></title>
<link href="/css/error.css" rel="stylesheet" 0="frontend\assets\AppAsset">
</head>
<body>
<div class="psy-status">
<div class="status-icon icon-desk"></div>
<div class="status-text">
<p><?php if(!empty($exception->getCode())&&($exception->getCode() == 8)){echo "出错啦";}else{ echo $exception->getMessage();}?></p>
</div>
</div>
</body>
</html>
3.修改应用的配置文件:@app/config/main.php
'errorHandler' => [
//'errorAction' => 'site/error',
'class' => 'common\component\exception\ErrorHandler',
],
修改上面三处,就能达到想要的目的,效果如下:
500错误页:
404错误页 :
yii2自定义500错误的更多相关文章
- 自定义 404 与 500 错误页面,URL 地址不会重定向(一)
对于 404 与 500 错误发生时,我们希望自己定义一个更加人性化的页面. 例子 当访问下面这个地址时: http://localhost/aaaa/bbb/ccc/ddd/eee/fff/ggg ...
- SpringBoot自定义错误页面,SpringBoot 404、500错误提示页面
SpringBoot自定义错误页面,SpringBoot 404.500错误提示页面 SpringBoot 4xx.html.5xx.html错误提示页面 ====================== ...
- nginx自定义500,502,504错误页面无法跳转【转】
1.自定一个页面,这个页面是一个链接地址可以直接访问的. 以下是nginx的配置: location / { proxy_pass http://tomcat_app108; ...
- 【yii2从Apache迁移到nginx上访问报500错误】
[yii2从Apache迁移到nginx上访问报500错误] 今天迁移yii2项目从Apache到nginx,出现了几个小问题,记录一下 index.php 加上 error_reporting(E_ ...
- Nginx 自定义404、500错误页面跳转
自定义Nginx错误界面跳转 1.开启Nginx.conf配置文件下的自定义接口参数. http { fastcgi_intercept_errors on; } 2.在Server区域添加自定义的错 ...
- yii2:doajax(post)会报500错误
yii2:doajax(post)会报500错误:这是因为yii2开启了防御csrf的攻击机制,可去先去掉,在控制器里去掉:public $enableCsrfValidation = false , ...
- apache 网页301重定向、自定义400/403/404/500错误页面
首先简单介绍一下,.htaccess文件是Apache服务器中的一个配置文件(Nginx服务器没有),它负责相关目录下的网页配置.通过对.htaccess文件进行设置,可以帮我们实现:网页301重定向 ...
- django的404,500错误自定义页面的配置
django404,500错误自定义页面: 1.设置settings文件 DEBUG = False ALLOWED_HOSTS = ['127.0.0.1', 'localhost']或者ALLOW ...
- 【转载】ASP.NET自定义404和500错误页面
在ASP.NET网站项目实际上线运行的过程中,有时候在运行环境下会出现400错误或者500错误,这些错误默认的页面都不友好,比较简单单调,其实我们可以自行设置这些错误所对应的页面,让这些错误跳转到我们 ...
随机推荐
- 这可能是最详细的 iOS 学习入门指南(含书目/文档/学习资料)
1 零基础小白如何进行 iOS 系统学习 首先,学习目标要明确: 其次,有了目标,要培养兴趣,经常给自己一些正面的反馈,比如对自己的进步进行鼓励,在前期小步快走: 再次,学技术最重要的一点就是多动手. ...
- caffe:自己搭建网络来训练
1.准备样本 要训练自己的样本,首先需要把样本准备好,需要准备的是训练集和测试集,caffe支持直接使用图片,当然把样本转换为leveldb或lmdb格式的话训练起来会更快一点.这里我先偷个懒,直接使 ...
- C++编译错误:multiple types in one declaration
这是在使用QT的时候看到的.这种情况往往是结构体或者是class最后少加了一个分好,加上即可,这个bug找了我好久,mark一下.
- SpringAnnotation注解之@Component,@Repository,@Service,@Controller
@Component:组件,表示此写上了此注解的bean,作为一个组件存在于容器中.这样的话别的地方就可以使用@Resource这个注解来把这个组件作为一个资源来使用了.初始化bean的名字为类名首字 ...
- ios 第3天
在手动引用计数中 每一次调用 retain retainCount 就会加一 每一次release retainCount就会减一 当retainCount 为零时 就会free (p) ; ...
- New Concept English three(16)
35w/m 43 Mary and her husband Dimitri lived in the tiny village of Perachora in southern Greece. One ...
- jquery设置控件位置的方法
纯JS写法,代码如下: document.getElementById("child").style.left="800px";document.getElem ...
- iOS实现程序长时间未操作退出
大部分银行客户端都有这样的需求,在用户一定时间内未操作,即认定为token失效,但未操作是任何判定的呢?我的想法是用户未进行任何touch时间,原理就是监听runloop事件.我们需要进行的操作是创建 ...
- 实现DQN算法玩CartPole
先安装tensorflow 1.2版本和python 3.6, 接着安装: numpy-1.13.1+mkl-cp36-cp36m-win_amd64.whl 的版本,这个是windows下的,如果l ...
- Linux 释放物理内存和虚拟内存
1.查看内存占用情况 $ free -m -h total used free shared buff/cache available Mem: .7G .0G .9G 385M 780M .0G S ...