# 不知道算不算Yii2的bug，Yii2的错误处理。

**URL:** https://www.shiqidu.com/t/topic/603
**Category:** 开发调优
**Tags:** Yii
**Created:** [2019 年7 月 24 日 06:35 UTC](https://www.shiqidu.com/t/topic/603 "2019-07-24T06:35:22Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### 作者： ![Eureka](https://www.shiqidu.com/user_avatar/www.shiqidu.com/eureka/32/5_2.png) [@Eureka](https://www.shiqidu.com/u/Eureka)
#### 发布日期： [2019 年7 月 24 日 06:35 UTC](https://www.shiqidu.com/t/topic/603/1 "2019-07-24T06:35:22Z")

</div>

比如我有一个 `ArticleController` 继承自 `BaseController` ，在 `BaseController`中，我会判断文章的是否存在，比如前台给了一个错误的 `articleID` ，那么我在 `BaseController` 中会抛出异常

```php
throw new NotFoundHttpException('文章不存在或状态异常');

```

我已经在配置文件中设置了 `errorAction`

```php
'errorHandler' => [
	'errorAction' => 'site/error',
],

```

在 `YII_DEBUG = true` 的情况下，应该走我自定义的错误页面吧。可是结果却没有走。

追了下代码，发现问题应该出在这。

```php
protected function renderException($exception)
{
	...
	$useErrorView = $response->format === Response::FORMAT_HTML && (!YII_DEBUG || $exception instanceof UserException);

	if ($useErrorView && $this->errorAction !== null) {
			$result = Yii::$app->runAction($this->errorAction); // 在这里会直接报错
			if ($result instanceof Response) {
					$response = $result;
			} else {
					$response->data = $result;
			}
	}
	...
}

```
