stanbridge / visual-exceptions
通过 iframe 为单页应用提供的可视化 Laravel 异常
Requires
- php: ^7.4|^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- orchestra/testbench: ^5.0
- phpunit/phpunit: ^9.0
- psalm/plugin-laravel: ^1.2
- vimeo/psalm: ^3.11
This package is not auto-updated.
Last update: 2024-09-24 12:23:32 UTC
README
为 SPAs 提供可视化 Laravel 异常
此包为单页应用提供类似传统 Laravel 应用程序的异常可视化表示。这是通过暂时将渲染异常的输出存储到文件中实现的。当客户端收到错误时,您可以使用包含的 render-exception.js
服务在浏览器中打开 iframe 以显示渲染的异常。
安装
您可以通过 composer 安装此包
composer require stanbridge/visual-exceptions
您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="Stanbridge\VisualException\VisualExceptionServiceProvider" --tag="config"
这是已发布配置文件的内容
return [ /* |-------------------------------------------------------------------------- | Master Switch |-------------------------------------------------------------------------- | | This option may be used to completely disable visual exceptions. | */ 'enabled' => env('VISUAL_EXCEPTIONS_ENABLED', env('APP_DEBUG')), /* |-------------------------------------------------------------------------- | Path |-------------------------------------------------------------------------- | | This is the URI path where visual exceptions will be accessible from. | */ 'path' => env('VISUAL_EXCEPTIONS_PATH', 'api/visual-exceptions'), /* |-------------------------------------------------------------------------- | Storage |-------------------------------------------------------------------------- | | This is where the temporary exception output will be stored. | */ 'storage' => 'visual-exceptions/latest.html', /* |-------------------------------------------------------------------------- | Clear on Retrieve |-------------------------------------------------------------------------- | | Use this option to clear the exception file after retrieving it. | */ 'clear_on_retrieve' => env('VISUAL_EXCEPTIONS_CLEAR', true), /* |-------------------------------------------------------------------------- | Middleware |-------------------------------------------------------------------------- | | Set middleware on the route. | */ 'middleware' => ['api'], ];
用法
1. 捕获异常
在您的 app/Exceptions/Handler.php
中,使用以下方法捕获渲染的异常
use \Illuminate\Support\Facades\Config; use Stanbridge\VisualException\VisualException; public function render($request, Throwable $exception) { $render = $this->prepareResponse($request, $exception); if (Config::get('visual-exceptions.enabled')) { VisualException::capture($render); } return $render; }
2. 发布资产
php artisan vendor:publish --provider="Stanbridge\VisualException\VisualExceptionServiceProvider" --tag=assets
3. 显示异常
将发布资产中的 render-exception.js
文件复制到您的单页应用中。
导入库并使用 retrieveLastError()
方法。以下是一个使用 axios 拦截器的示例
import axios from 'axios'; import VisualException from 'path/to/render-exception.js'; axios.interceptors.response.use(response => response, error => { if (error.response.status >= 500) { VisualException.retrieveLastError(); } });
render-exception.js
中的代码来自 Livewire。感谢 Caleb Porzio 和 Jonathan Reinink
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的详细信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全性
如果您发现任何与安全相关的问题,请通过电子邮件 austingym@gmail.com 反馈,而不是使用问题跟踪器。
致谢
许可证
MIT 许可证 (MIT)。请参阅 许可证文件 了解更多信息。