dragon-code/notify-exceptions

This package is abandoned and no longer maintained. No replacement package was suggested.

Notify the site administrator of any errors through various channels of communication.

v4.1.0 2022-01-17 16:00 UTC

README

Laravel Notify Exceptions

Total Downloads Latest Stable Version Latest Unstable Version License

You can use both official plugins from the Laravel team and plugins from the community.

Of course, you can also create your own.

Installation

To get the latest version of Notifex, simply require the project using Composer:

composer require dragon-code/notify-exceptions

Instead, you may of course manually update your require block and run composer update:

{
    "require": {
        "dragon-code/notify-exceptions": "^4.0"
    }
}

You can also publish the config file to change implementations (ie. interface to specific class):

php artisan vendor:publish --provider="DragonCode\Notifex\ServiceProvider"

And call php artisan migrate command from console.

Now you can use the app('notifex') method.

Upgrade from andrey-helldar/notify-exceptions

  1. Replace "andrey-helldar/notify-exceptions": "^3.0" with "dragon-code/notify-exceptions": "^4.0" in the composer.json file;
  2. Replace Helldar\Notifex namespace prefix with DragonCode\Notifex;
  3. Call the composer update console command.

Configuration

Ingore bots

By default, the package does not respond to errors created in the process of search bots.

To enable error messages from bots, change the setting ignore_bots in config/notifex.php file.

By default, false.

Email

See configuration file.

Example email message:

email-message

Jira

If you need to create issues in the Jira, then you need to install the package lesstif/php-jira-rest-client:

composer require lesstif/php-jira-rest-client

2018-10-10_23-32-57

Slack

If you need to send messages in the Slack channel, then you need to install the package laravel/slack-notification-channel:

composer require laravel/slack-notification-channel

Your notification services

You can easily connect your notification services. To do this, in block jobs of file config/notifex.php, add a call to its job:

\DragonCode\Notifex\Jobs\ExampleJob::class

If you need to pass any parameters to your job, you can use an associative entry, where the key is the link to the job class, and the values are the parameters:

\DragonCode\Notifex\Jobs\ExampleJob::class => [
    'host'      => env('EXAMPLE_HOST'), // http://127.0.0.1:8080
    'user'      => env('EXAMPLE_USER'), // 'foo'
    'password'  => env('EXAMPLE_PASS'), // 'bar'
    'other_key' => env('EXAMPLE_OTHER_KEY'), // 12345
],

Your job should inherit from the abstract class DragonCode\Notifex\Abstracts\JobAbstract. This will help to correctly create a class for work.

To get the values of the settings you need to use the method getConfig($class, $key):

$host      = $this->getConfig(get_class(), 'host');
$user      = $this->getConfig(get_class(), 'user');
$password  = $this->getConfig(get_class(), 'password');
$other_key = $this->getConfig(get_class(), 'other_key');

// or add `config(string $key)` method:

private function config(string $key)
{
    return $this->getConfig(get_class(), $key);
}

$host      = $this->config('host');
$user      = $this->config('user');
$password  = $this->config('password');
$other_key = $this->config('other_key');

Examples of completed classes can be found here:

It is worth noting that standard jobs of Laravel are used for the call:

php artisan make:job <name>

They should remove the call interface ShouldQueue and extend the class:

// before
use Illuminate\Contracts\Queue\ShouldQueue;

class ExampleJob implements ShouldQueue {}

// after
use DragonCode\Notifex\Abstracts\JobAbstract;

class ExampleJob extends JobAbstract {}

As the abstract class includes a call of all necessary classes and interfaces.

It's all! Enjoy 😊

Using

Add exception capturing to app/Exceptions/Handler.php:

public function report(Throwable $exception)
{
    parent::report($exception);
    
    if (app()->bound('notifex') && $this->shouldReport($exception)) {
        app('notifex')->send($exception);
    }
}

or just use in your code:

try {
    $foo = $bar
} catch(\Exception $exception) {
    app('notifex')->send($exception);
}

IMPORTANT!

To realize the possibility of saving an object to a database table, this object is processed before serialization. Due to the peculiarities of linking objects in PHP, serialization does not support the Throwable interface, and therefore, if you call method app('notifex')->send($exception) before processing a variable, the application may cause an error Expected array for frame 0.

To avoid this, use method parent::report($exception) strictly before sending notifications.

Test message

To verivy that Notifex is configured correctly and our integration is working, use notifex:test command:

php artisan notifex:test

A DragonCode\Notifex\Exceptions\NotifexTestException class will be thrown and captured by Notifex. The captured exception will appear in your configured email immediately.

Support

The package out of the box supports sending notifications to the following services:

  • Email (default, enabled)
  • Slack (default, disabled)
  • Jira (default, disabled)

License

This package is licensed under the MIT License.