cerbero/notifiable-exception

Laravel 包,用于在抛出某些异常时发送通知。

1.1.1 2020-03-04 12:51 UTC

This package is auto-updated.

Last update: 2024-09-09 13:53:31 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Laravel 包,用于抛出异常时发送 通知

安装

通过 Composer

$ composer require cerbero/notifiable-exception

我们可能需要安装其他依赖包,具体取决于我们想要使用的通知渠道(例如 Slack、Telegram)。请参考 Laravel Notification Channels 获取更多信息。

为了更好的性能,通知将被排队,请查看 文档 了解您队列驱动程序的要求。

使用方法

为了让异常具有可通知性,需要实现 Notifiable 接口并使用 Notifies 特性

use Cerbero\NotifiableException\Notifiable;
use Cerbero\NotifiableException\Notifies;
use Exception;

class UrgentException extends Exception implements Notifiable
{
    use Notifies;
}

否则,如果我们不需要扩展特定的异常类,我们可以直接扩展 NotifiableException 以便于使用

use Cerbero\NotifiableException\Exceptions\NotifiableException;

class UrgentException extends NotifiableException

当可通知的异常没有在 try-catch 中手动处理时,它们将自动被通知。然而,当我们确实需要处理它们时,我们可以在 try-catch 中调用 notify() 方法来发送通知

try {
    $this->methodThrowingNotifiableException();
} catch (NotifiableException $e) {
    $e->notify();
    // exception handling logic
}

有时我们可能希望某些渠道路由在抛出异常时始终被通知。如果这样,我们可以在 config/notifiable_exception.php 中为每个我们想要通知的渠道设置默认路由

$ php artisan vendor:publish --tag=notifiable_exception_config

例如,以下配置定义了一个 Slack 和一个邮件路由,当抛出任何可通知的异常时将始终被通知

'default_routes' => [
    'mail' => [
        'example@test.com',
    ],
    'slack' => [
        'https://hooks.slack.com/services/xxx/xxx/xxx',
    ],
],

注意:此 README 为了方便起见在代码中显示了路由,但建议将路由设置为环境变量,然后从配置文件中读取。

不同的路由可能需要根据抛出的可通知异常实例来通知。可以通过覆盖 getCustomRoutes() 方法在可通知异常本身中定义临时的渠道和路由

class UrgentException extends NotifiableException
{
    protected function getCustomRoutes(): array
    {
        return [
            'nexmo' => [
                '15556666666',
            ],
        ];
    }
}

在上面的例子中,当抛出 UrgentException 时,电话号码 +1 555-666-6666 将接收到短信,同时还有配置中指定的默认路由。

如果我们希望异常仅通知其自定义路由而忽略默认路由,我们可以指示 overridesRoutes() 方法这样做

protected function overridesRoutes(): bool
{
    return true;
}

可以通过覆盖 getMessages() 方法按渠道自定义要发送的消息

public function getMessages(): array
{
    return [
        'mail' => (new MailMessage)
            ->error()
            ->subject('An error occurred')
            ->line($this->getMessage()),
        'slack' => (new SlackMessage)
            ->error()
            ->content($content)
            ->attachment(function (SlackAttachment $attachment) {
                $attachment
                    ->title($this->getMessage())
                    ->fields([
                        'File' => $this->getFile(),
                        'Line' => $this->getLine(),
                        'Code' => $this->getCode(),
                        'Previous exception' => $this->getPrevious() ? get_class($this->getPrevious()) : 'none',
                    ]);
            }),
        'nexmo' => (new NexmoMessage)->content($this->getMessage()),
    ];
}

默认情况下,Laravel 支持一些通知渠道(例如 mailslack),但使用第三方解决方案时需要指定自定义渠道类。我们可以通过覆盖 getCustomChannels() 方法来定义它们

use NotificationChannels\Telegram\TelegramChannel;

...

public function getCustomChannels(): array
{
    return [
        'telegram' => TelegramChannel::class,
    ];
}

变更日志

请参阅 CHANGELOG 了解最近更改的内容。

测试

$ composer test

贡献

请参阅 CONTRIBUTINGCODE_OF_CONDUCT 了解详细信息。

安全

如果您发现任何安全问题,请通过电子邮件 andrea.marco.sartori@gmail.com 联系我们,而不是使用问题跟踪器。

致谢

许可证

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