swoft / whoops
Swoft 的 Whoops 组件
v2.0.11
2021-05-06 16:33 UTC
Requires
- php: >7.1
- filp/whoops: ^2.4
README
Swoft http 错误处理,由 filp/whoops 提供
安装
- composer 命令
composer require --dev swoft/whoops
使用方法
- 方法 1
使用 Swoft\Whoops\WhoopsMiddleware
作为全局中间件(在 app/bean.php
中)
'httpDispatcher' => [ // Add global http middleware 'middlewares' => [ // Notice: Please add to the first place \Swoft\Whoops\WhoopsMiddleware::class, ], ],
- 方法 2
修改 App\Exception\Handler\HttpExceptionHandler
类
<?php declare(strict_types=1); namespace App\Exception\Handler; use Swoft\Error\Annotation\Mapping\ExceptionHandler; use Swoft\Http\Message\ContentType; use Swoft\Http\Message\Response; use Swoft\Http\Server\Exception\Handler\AbstractHttpErrorHandler; use Swoft\Log\Helper\CLog; use Swoft\Whoops\WhoopsHandler; use Throwable; use function bean; use function context; use const APP_DEBUG; /** * Class HttpExceptionHandler * * @ExceptionHandler(\Throwable::class) */ class HttpExceptionHandler extends AbstractHttpErrorHandler { /** * @param Throwable $e * @param Response $response * * @return Response */ public function handle(Throwable $e, Response $response): Response { $request = context()->getRequest(); if ($request->getUriPath() === '/favicon.ico') { return $response->withStatus(404); } // Log CLog::error($e->getMessage()); // Debug is false if (!APP_DEBUG) { return $response ->withStatus(500) ->withContent($e->getMessage()); } // Debug is true $whoops = bean(WhoopsHandler::class); $content = $whoops->run($e, $request); return $response->withContent($content)->withContentType(ContentType::HTML);; } }
许可证
该组件是开源软件,遵循Apache 许可协议。