錯(cuò)誤處理?

CodeIgniter 通過(guò) SPL collection 和一些框架內(nèi)自定義異常來(lái)生成系統(tǒng)錯(cuò)誤報(bào)告。錯(cuò)誤處理的行為取決于你部署環(huán)境的設(shè)置,當(dāng)一個(gè)錯(cuò)誤或異常被拋出時(shí),只要應(yīng)用不是在 production 環(huán)境下運(yùn)行,就會(huì)默認(rèn)展示出詳細(xì)的錯(cuò)誤報(bào)告。在這種情況下,應(yīng)為用戶顯示一個(gè)更為通用的信息來(lái)保證最佳的用戶體驗(yàn)。

使用異常處理?

本節(jié)為新手程序員或沒(méi)有多少異常處理使用經(jīng)驗(yàn)的開(kāi)發(fā)人員做一個(gè)簡(jiǎn)單概述。

異常處理是在異常被”拋出”的時(shí)候產(chǎn)生的事件。它會(huì)暫停當(dāng)前腳本的執(zhí)行,并將捕獲到的異常發(fā)送到錯(cuò)誤處理程序后顯示適當(dāng)?shù)腻e(cuò)誤提示頁(yè)

throw new \Exception("Some message goes here");

如果你調(diào)用了一個(gè)可能會(huì)產(chǎn)生異常的方法,你可以使用 try/catch block 去捕獲異常

try {
        $user = $userModel->find($id);
}
catch (\Exception $e)
{
        die($e->getMessage());
}

如果 $userModel 拋出了一個(gè)異常,那么它就會(huì)被捕獲,并執(zhí)行 catch 代碼塊內(nèi)的語(yǔ)句。在這個(gè)樣例中,腳本終止并輸出了 UserModel 定義的錯(cuò)誤信息。

在這個(gè)例子中,我們可以捕捉任意類(lèi)型的異常。如果我們僅僅想要監(jiān)視特定類(lèi)型的異常,比如 UnknownFileException,我們就可以把它在 catch 參數(shù)中指定出來(lái)。這樣一來(lái),其它異常和非監(jiān)視類(lèi)型子類(lèi)的異常都會(huì)被傳遞給錯(cuò)誤處理程序

catch (\CodeIgniter\UnknownFileException $e)
{
        // do something here...
}

這便于你自己進(jìn)行錯(cuò)誤處理或是在腳本結(jié)束前做好清理工作。如果你希望錯(cuò)誤處理程序正常運(yùn)行,可以在 catch 語(yǔ)句塊中再拋出一個(gè)新的異常

catch (\CodeIgniter\UnknownFileException $e)
{
        // do something here...

        throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
}

配置?

默認(rèn)情況下,CodeIgniter 將在 developmenttesting 環(huán)境中展示所有的錯(cuò)誤,而在 production 環(huán)境中不展示任何錯(cuò)誤。你可以在主 index.php 文件的頂部找到環(huán)境配置部分來(lái)更改此設(shè)置。

重要

如果發(fā)生錯(cuò)誤,禁用錯(cuò)誤報(bào)告將不會(huì)阻止日志的寫(xiě)入。

記錄異常?

By default, all Exceptions other than 404 - Page Not Found exceptions are logged. This can be turned on and off by setting the $log value of Config\Exceptions:

class Exceptions
{
    public $log = true;
}

To ignore logging on other status codes, you can set the status code to ignore in the same file:

class Exceptions
{
    public $ignoredCodes = [ 404 ];
}

注解

It is possible that logging still will not happen for exceptions if your current Log settings are not set up to log critical errors, which all exceptions are logged as.

自定義異常?

下列是可用的自定義異常:

PageNotFoundException?

這是用來(lái)聲明 404 ,頁(yè)面無(wú)法找到的錯(cuò)誤。當(dāng)異常被拋出時(shí),系統(tǒng)將顯示后面的錯(cuò)誤模板 /application/views/errors/html/error_404.php。你應(yīng)為你的站點(diǎn)自定義所有錯(cuò)誤視圖。如果在 Config/Routes.php 中,你指定了404 的重寫(xiě)規(guī)則,那么它將代替標(biāo)準(zhǔn)的 404 頁(yè)來(lái)被調(diào)用

if (! $page = $pageModel->find($id))
{
        throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}

你可以通過(guò)異常傳遞消息,它將在 404 頁(yè)默認(rèn)消息位置被展示。

ConfigException?

當(dāng)配置文件中的值無(wú)效或 class 類(lèi)不是正確類(lèi)型等情況時(shí),請(qǐng)使用此異常

throw new \CodeIgniter\Exceptions\ConfigException();

它將 HTTP 狀態(tài)碼置為 500,退出狀態(tài)碼被置為 3.

UnknownFileException?

在文件沒(méi)有被找到時(shí),請(qǐng)使用此異常

throw new \CodeIgniter\UnknownFileException();

它將 HTTP 狀態(tài)碼置為 500,退出狀態(tài)碼被置為 4.

UnknownClassException?

當(dāng)一個(gè)類(lèi)沒(méi)有被找到時(shí),請(qǐng)使用此異常

throw new \CodeIgniter\UnknownClassException($className);

它將 HTTP 狀態(tài)碼置為 500,退出狀態(tài)碼被置為 5.

UnknownMethodException?

當(dāng)一個(gè)類(lèi)的方法不存在時(shí),請(qǐng)使用此異常

throw new \CodeIgniter\UnknownMethodException();

它將 HTTP 狀態(tài)碼置為 500,退出狀態(tài)碼被置為 6.

UserInputException?

當(dāng)用戶的輸入無(wú)效時(shí),請(qǐng)使用此異常

throw new \CodeIgniter\UserInputException();

它將 HTTP 狀態(tài)碼置為 500,退出狀態(tài)碼被置為 7.

DatabaseException?

當(dāng)產(chǎn)生如連接不能建立或連接臨時(shí)丟失的數(shù)據(jù)庫(kù)錯(cuò)誤時(shí),請(qǐng)使用此異常

throw new \CodeIgniter\DatabaseException();

它將 HTTP 狀態(tài)碼置為 500,退出狀態(tài)碼被置為 8.

RedirectException?

This exception is a special case allowing for overriding of all other response routing and forcing a redirect to a specific route or URL:

throw new \CodeIgniter\Router\Exceptions\RedirectException($route);

$route may be a named route, relative URI, or a complete URL. You can also supply a redirect code to use instead of the default (302, “temporary redirect”):

throw new \CodeIgniter\Router\Exceptions\RedirectException($route, 301);