承接 cerbero/notifiable-exception 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cerbero/notifiable-exception

Composer 安装命令:

composer require cerbero/notifiable-exception

包简介

Laravel package to send notifications when some exceptions are thrown.

README 文档

README

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

Laravel package to send notifications when exceptions are thrown.

Install

Via Composer

$ composer require cerbero/notifiable-exception

We might need to install other packages depending on the notification channels we want to use (e.g. Slack, Telegram). Please refer to Laravel Notification Channels for more information.

For better performance notifications are queued, please check the documentation to find out what are the requirements for your queue driver.

Usage

In order to be notifiable, exceptions need to implement the Notifiable interface and use the Notifies trait:

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

class UrgentException extends Exception implements Notifiable
{
    use Notifies;
}

Otherwise, if we don't need to extend a particular exception class, we may just extend the NotifiableException for convenience:

use Cerbero\NotifiableException\Exceptions\NotifiableException;

class UrgentException extends NotifiableException

When notifiable exceptions are not handled manually in a try-catch, they are notified automatically. However when we actually need to handle them we can send their notifications by calling the notify() method in the try-catch:

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

Sometimes we might want some channel routes to always be notified when an exception is thrown. If so, we can set default routes for every channel we want to notify in config/notifiable_exception.php:

$ php artisan vendor:publish --tag=notifiable_exception_config

As an example, the following configuration defines a Slack and a mail route that will always be notified when any notifiable exception is thrown:

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

Please note: this README shows routes in the code for convenience, however it is recommended to set routes in environment variables that can then be read from configuration files.

Different routes might need to be notified depending on what instance of notifiable exception is thrown. Ad hoc channels and routes can be defined in notifiable exceptions themselves by overriding the method getCustomRoutes():

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

In the example above, the phone number +1 555-666-6666 will receive an SMS whenever UrgentException is thrown, alongside with the default routes specified in the configuration.

If we want an exception to notify only its custom routes while ignoring the default ones, we can instruct the method overridesRoutes() to do so:

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

Messages to send can be customized per channel by overriding the method 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()),
    ];
}

By default Laravel supports some notification channels (e.g. mail, slack), however custom channel classes need to be specified when using third-party solutions. We can define them by overriding the method getCustomChannels():

use NotificationChannels\Telegram\TelegramChannel;

...

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

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email andrea.marco.sartori@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

cerbero/notifiable-exception 适用场景与选型建议

cerbero/notifiable-exception 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 171 次下载、GitHub Stars 达 76, 最近一次更新时间为 2019 年 10 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「exceptions」 「notifications」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 cerbero/notifiable-exception 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cerbero/notifiable-exception 我们能提供哪些服务?
定制开发 / 二次开发

基于 cerbero/notifiable-exception 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 171
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 76
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 76
  • Watchers: 4
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-10-08