cerbero/notifiable-exception
Composer 安装命令:
composer require cerbero/notifiable-exception
包简介
Laravel package to send notifications when some exceptions are thrown.
README 文档
README
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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cerbero/notifiable-exception 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
php7ify is a project that brings new php7 classes and exceptions to php 5.x.
A Laravel Nova package that adds a notification feed in your Nova app.
The CodenamePHP Platform core that contains basic interfaces, exceptions etc..
A simple package to better handle Exceptions.
Monitors errors and exceptions and reports them to Rollbar
Captures outgoing SMS notifications
统计信息
- 总下载量: 171
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 76
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-10-08