A Laravel 12 proof-of-concept that sends Slack notifications for critical exceptions using the ->withExceptions()
configuration approach introduced in Laravel 11.
- Detects critical exceptions (
ErrorException
,QueryException
) and sends Slack notifications. - Clean exception handling with
ExceptionReporter
service. - Rate limiting to prevent notification spam (5-minute cooldown per unique message).
- Queued notifications for performance.
- Test routes for demonstration:
/test-error
: Triggers anErrorException
./test-query-error
: Triggers aQueryException
.
git clone https://github.com/thekubera/laravel-error-notifier.git
cd laravel-error-notifier
composer install
cp .env.example .env
php artisan key:generate
Configure your Slack webhook in the .env
file:
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/your/webhook/url
This POC uses Laravel's official Slack notification channel.
Install it via Composer:
composer require laravel/slack-notification-channel
For more details, see the Laravel documentation.
You can test the functionality using the included routes:
Route | Description |
---|---|
/test-error |
Triggers a generic ErrorException . |
/test-query-error |
Triggers a QueryException by querying a non-existent table. |
Visiting either of these routes in your browser will trigger an exception that is caught and, if classified as critical, reported to Slack.
-
App\Services\ExceptionReporter
Contains the logic to determine whether an exception is critical and to send notifications. -
App\Notifications\CriticalErrorNotification
Defines the structure of the Slack message. -
bootstrap/app.php
Registers the exception handling via the->withExceptions()
method. -
routes/web.php
Contains test routes for triggering exceptions.
Ensure your .env
file includes the following:
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/your/webhook/url
QUEUE_CONNECTION=sync
If you're using a queue driver other than sync
, make sure to run the queue worker:
php artisan queue:work
- Make exception types configurable via a dedicated config file (
config/exception-notifier.php
). - Add additional request metadata to Slack messages (e.g., headers, IP address, user agent).
- Support additional notification channels such as email, Discord, or Microsoft Teams.