Skip to content

A Laravel 12 proof-of-concept that sends Slack notifications for critical exceptions using the ->withExceptions() configuration approach introduced in Laravel 11.

Notifications You must be signed in to change notification settings

thekubera/laravel-error-notifier

Repository files navigation

Laravel Error Notifier

A Laravel 12 proof-of-concept that sends Slack notifications for critical exceptions using the ->withExceptions() configuration approach introduced in Laravel 11.

Features

  • 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 an ErrorException.
    • /test-query-error: Triggers a QueryException.

Installation

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

Dependencies

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.

Testing the Notifier

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.

Project Structure

  • 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.

Environment Configuration

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

Optional Improvements

  • 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.

About

A Laravel 12 proof-of-concept that sends Slack notifications for critical exceptions using the ->withExceptions() configuration approach introduced in Laravel 11.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages