stanbridge/visual-exceptions

通过 iframe 为单页应用提供的可视化 Laravel 异常

0.0.3 2022-01-31 16:49 UTC

This package is not auto-updated.

Last update: 2024-09-24 12:23:32 UTC


README

logo

为 SPAs 提供可视化 Laravel 异常

Latest Version on Packagist Tests Total Downloads

此包为单页应用提供类似传统 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 PorzioJonathan Reinink

测试

composer test

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全性

如果您发现任何与安全相关的问题,请通过电子邮件 austingym@gmail.com 反馈,而不是使用问题跟踪器。

致谢

许可证

MIT 许可证 (MIT)。请参阅 许可证文件 了解更多信息。