企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] # 错误 ## 介绍 Laravel 错误及异常处理默认已配置好,`App\Exceptions\Handler`类负责记录应用程序触发的所有异常并呈现给用户。 ## 配置 `config/app.php`配置文件中的`debug`选项决定了对于一个错误实际上将显示多少信息给用户。默认情况下,该选项的设置将遵照存储在`.env`文件中的`APP_DEBUG`环境变量的值。 对于本地开发,你应该将`APP_DEBUG`环境变量的值设置为`true`。在生产环境中,该值应始终为`false`。如果在生产中将该值设置为`true`,则可能会将敏感配置值暴露给应用程序的终端用户。 ## 异常处理器 ### Report 方法 所有异常都是由`App\Exceptions\Handler`类处理的。这个类包含两个方法:`report`和`render`。`report`方法用于记录异常或将它们发送给如[Bugsnag](https://bugsnag.com/)或[Sentry](https://github.com/getsentry/sentry-laravel)等外部服务。默认情况下,`report`方法将异常传递给记录异常的基类。 ``` /** * 报告或记录异常 * * 此处是发送异常给 Sentry、Bugsnag 等外部服务的好位置。 * * @param \Exception $exception * @return void */ public function report(Exception $exception) { if ($exception instanceof CustomException) { // } parent::report($exception); } ``` > 提示:不要在`report`方法中进行太多的`instanceof`检查 #### 全局日志 在正常情况下, Laravel 会自动将当前用户的 ID 作为数据添加到每一条异常日志中。 通过重写`App\Exceptions\Handler`类中的`context`方法来定义全局环境变量。 ``` /** * 定义默认的环境变量 * * @return array */ protected function context() { return array_merge(parent::context(), [ 'foo' => 'bar', ]); } ``` #### `report`辅助函数 有时你可能需要报告异常,但又不希望终止当前请求的处理。`report`辅助函数允许你使用异常处理器的`report`方法在不显示错误页面的情况下快速报告异常: ``` public function isValid($value) { try { // 验证值... } catch (Exception $e) { report($e); return false; } } ``` #### 按类型忽略异常 异常处理器的`$dontReport`属性包含一组不会被记录的异常类型。例如,由 404 错误导致的异常以及其他几种类型的错误不会写入日志文件。你可以根据需要添加其他异常类型到此数组中: ``` /** * 不应被报告的异常类型清单。 * * @var array */ protected $dontReport = [ \Illuminate\Auth\AuthenticationException::class, \Illuminate\Auth\Access\AuthorizationException::class, \Symfony\Component\HttpKernel\Exception\HttpException::class, \Illuminate\Database\Eloquent\ModelNotFoundException::class, \Illuminate\Validation\ValidationException::class, ]; ``` ### Render 方法 `render`方法负责将给定的异常转换为将被发送回浏览器的 HTTP 响应。默认情况下,异常将传递响应的基类。不过,可以按自己意愿检查异常类型或返回自己的自定义响应: ``` /** * 将异常转换为 HTTP 响应。 * * @param \Illuminate\Http\Request $request * @param \Exception $exception * @return \Illuminate\Http\Response */ public function render($request, Exception $exception) { if ($exception instanceof CustomException) { return response()->view('errors.custom', [], 500); } return parent::render($request, $exception); } ``` ### Reportable & Renderable 异常 除了在异常处理器的`report`和`render`方法中检查异常类型,你还可以直接在自定义异常上定义`report`和`render`方法。当定义了这些方法时,它们会被框架自动调用: ``` // 自定义异常类 <?php namespace App\Exceptions; use Exception; class RenderException extends Exception { /** * 报告异常 * * @return void */ public function report() { // } /** * 转换异常为 HTTP 响应 * * @param \Illuminate\Http\Request * @return \Illuminate\Http\Response */ public function render($request) { return response(...); } } ``` > Tip:可以声明`report`方法和必要参数,它们将通过 Laravel 的 [服务容器] 自动注入方法中。 ## HTTP 异常 ``` abort(404); // 页面未找到 abort(403, 'Unauthorized action.'); ``` ### 自定义 HTTP 错误页面 ``` // 生成自定义错误模板页面,生成目录 resources/views/errors $ php artisan vendor:publish --tag=laravel-errors // 由 abort 函数抛出的 HttpException 实例将作为 $exception 变量传递给视图 <h2>{{ $exception->getMessage() }}</h2> ```