Skip to content
Advertisement

Sentry + Laravel: how to log an already catched Exception?

We’re using Laravel ( 5.x to 7.x ) in a lot of project and all integrated with Sentry.

In some edge case we need to catch some recoverable exceptions to allow page flow to go on using fallbacks, but we also would like to log these catched exception to sentry.

I cannot find a documented or undocumented way to manually log to Sentry.

Is there a way to log to sentry an already catched exception?

Advertisement

Answer

^ this is the way to do this.

If you want to use the Laravel container it could look more like this:

try {
    // your code that could throw an exception
} catch (Throwable $e) {
    if (app()->bound('sentry')) {
        app('sentry')->captureException($e);
    }
}

You can also report use which should also log the exception to your log files:

try {
    // your code that could throw an exception
} catch (Throwable $e) {
    report($e); // this is a Laravel helper, not from Sentry
}

Also, you could manually use the Sentry’s helpers – the source code is here

SentrycaptureMessage("This handle a text message");

// this handle everything derives from Exception
SentrycaptureException($e);
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement