# 异常处理
此页面参照了EasySwoole Distributed的文档,原文档地址
[https://www.kancloud.cn/tmtbe/goswoole/1086151]: https://www.kancloud.cn/tmtbe/goswoole/1086151
路由如果没有匹配到任何页面,会调用配置 error_controller_name 指定的异常处理类,默认为GoController::class。
```php
public function onExceptionHandle(\Throwable $e)
{
if ($this->whoopsConfig->isEnable() && Server::$instance->getServerConfig()->isDebug()) {
throw $e;
}
if ($this->clientData->getResponse() != null) {
$this->response->withStatus(404);
$this->response->withHeader("Content-Type", "text/html;charset=UTF-8");
if($e instanceof RouteException) {
$msg = '404 Not found / ' . $e->getMessage();
return $msg;
}else if ($e instanceof AccessDeniedException) {
$this->response->withStatus(401);
$msg = '401 Access denied / ' . $e->getMessage();
return $msg;
}else if($e instanceof ResponseException){
$this->response->withStatus(200);
return $this->errorResponse($e->getMessage(), $e->getCode());
}else if ($e instanceof AlertResponseException){
$this->response->withStatus(500);
return $this->errorResponse($e->getMessage(), $e->getCode());
}
}
return parent::onExceptionHandle($e);
}
```
## 自定义异常处理
自定义异常处理,有2个时机。
> 如果异常发生时,代码没有执行到控制器里比如404,405,则会调用GoController::onExceptionHandle 的方法进行处理。也就是 error_controller_name 配置的异常处理类。该类需要继承 GoController 。
> 如果异常发生时,已经执行到控制器里,比如自己throw了一个异常,会优先调用该控制器里的onExceptionHandle 方法。
如果需要自定义错误处理,请注意异常发生的时机。
## 自定义处理异常案例
```php
<?php
/**
* Created by PhpStorm.
* User: anythink
* Date: 2019/5/31
* Time: 6:26 PM
*/
namespace app\Controller;
use ESD\Go\GoController;
class ExceptionClass extends GoController{
function onExceptionHandle(\Throwable $e)
{
$this->response->withHeader("content-type",'text/html;charset=utf-8');
if($e instanceof \Exception){
return '拦截所有异常';
}
return parent::onExceptionHandle($e); // TODO: Change the autogenerated stub
}
}
```
配置文件
```
route:
error_controller_name: app\Controller\ExceptionClass
```
## 框架提供的可用异常
该异常框架内部不会抛出,均由用户自行使用。
## ResponseException
该异常会被捕获并返回message,code,如果不是ajax请求会返回。
```
错误消息
aaa
```
如果是ajax请求会返回。
```json
{
"code": 23333,
"msg": "aaa",
"data": null
}
```
## AlertResponseException
该异常会捕获并返回 http500 , 内部服务器错误,日志会记录详细的错误信息,用于系统出现问题的时候调用。
```
错误消息
内部服务器错误,请稍后再试
```
如果是ajax请求会返回
```json
{
"code": 500,
"msg": "内部服务器错误,请稍后再试",
"data": null
}
```
- 1 介绍
- 2 安装
- 2.1 环境
- 2.2 安装
- 3 配置
- 3.1 Server配置
- 3.2 端口配置
- 3.3 项目结构
- 3.4 内核优化
- 4 服务
- 4.1 HTTP服务
- 4.1.1 路由
- 4.1.1.1 静态路由
- 4.1.1.2 路由定义
- 4.1.1.3 路由方法
- 4.1.1.4 路由分组
- 4.1.1.5 资源路由
- 4.1.1.6 端口作用域
- 4.1.1.7 异常处理
- 4.1.1.8 跨域请求
- 4.1.1.9 路由缓存
- 4.1.2 控制器
- 4.1.2.1 控制器初始化
- 4.1.2.2 前置后置操作
- 4.1.2.3 跳转与重定向
- 4.1.2.4 异常处理
- 4.1.3 请求
- 4.1.3.1 请求对象
- 4.1.3.2 请求信息
- 4.1.3.3 REQUEST消息
- 4.1.3.4 RESPONSE消息
- 4.1.3.5 STREAM消息
- 4.1.3.6 URI信息
- 4.1.3.7 处理上传文件
- 4.1.3.8 验证器
- 4.2 Websocket服务
- 4.2.1 Websocket配置
- 4.2.2 Websocket路由
- 4.3 TCP服务
- 4.3.1 TCP配置
- 4.3.2 TCP路由
- 4.3.3 协程处理案例
- 5 插件
- 5.15 Yii-PDO插件
- 5.15.1 PDO 连接MySQL Mariadb
- 5.15.2 PDO连接PostgreSQL
- 5.15.3 PDO连接GreenPlum
- 5.15.4 PDO连接Oracle
- 5.15.5 PDO连接Cubrid
- 5.15.6 PDO连接SQL Server
- 6 概念
- 7. Yii- I18N国际化
- 8. 模型 Yii-Model
- 8.1 快速创建模型
- 8.2 快速创建多个模型
- 8.3 核心验证器 Core Validators
- 9. 配合数据库工作
- 9.1.数据库访问对象 Database Access Objects
- 9.2 查询构造器 Query Builder
- 9.3 活动记录 Active Record