Home / Snippets / How to Integrate Sentry with Laravel Nova
How to Integrate Sentry with Laravel Nova cover

How to Integrate Sentry with Laravel Nova

1.9K

2 years ago

0 comments

Sentry Homepage

In this short snippet, you will learn how to set up Sentry which is "an Application Monitoring and Error Tracking Software" to your Laravel Nova. The steps are quite straight forward so let's get started.

Step 1: Install Sentry Package


First, you have to install the Laravel Sentry Package available on GitHub using Composer.
composer require sentry/sentry-laravel

Step 2: Setup Sentry


Once you have done that now you can set up the Sentry by publishing the configuration file using the command below. Do note that the "--dsn=" is unique to your project so you need to register for an account from the Sentry website.
php artisan sentry:publish --dsn=https://[email protected]/0
It creates (config/sentry.php) and adds the DSN to your .env file.
SENTRY_LARAVEL_DSN=https://[email protected]/0

Step 3: Verify Sentry Setup With Artisan


To verify the setup you can run the sentry:test command below and it will send a test event to the Sentry dashboard.
php artisan sentry:test --transaction

Step 4: Configure Nova For Sentry


Next, you need to configure Nova to send all its errors to Sentry. Nova uses its own internal exception handler instead of using the default App\Exceptions\ExceptionHandler. If you need to integrate third-party error reporting tools with your Nova installation, you should use the Nova::report. Typically, this method should be invoked from the register method of your application's App\Providers\NovaServiceProvider class:
App\Providers\NovaServiceProvider;
The full code implementation should be as follows.
<?php

namespace App\Providers;

use Laravel\Nova\Nova;
use Laravel\Nova\Cards\Help;
use Illuminate\Support\Facades\Gate;
use Laravel\Nova\NovaApplicationServiceProvider;

class NovaServiceProvider extends NovaApplicationServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        Nova::report(function ($exception) {
            if (app()->bound('sentry')) {
                app('sentry')->captureException($exception);
           }
       });
    }
}
By now if there are any errors, they will automatically be sent to Sentry and you can view, manage and track from the dashboard. If this snippet is helpful to you do make sure to share it with your friends and cheers, happy coding!

Related Links

notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this