'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?
Solution 1:[1]
^ 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
\Sentry\captureMessage("This handle a text message");
// this handle everything derives from \Exception
\Sentry\captureException($e);
Solution 2:[2]
This is in the docs: https://docs.sentry.io/platforms/php/#capturing-errors
You can use the Sentry\captureException
method:
try {
$this->functionFailsForSure();
} catch (\Throwable $exception) {
Sentry\captureException($exception);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mehrdad Dehghani |
Solution 2 | Martin Bean |