Skip to content

Commit

Permalink
Introduce captureUnhandledException (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive authored Nov 10, 2022
1 parent 8ce9684 commit f919570
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Add new method to report unhandled exceptions from the Laravel error handler (#608)

## 3.0.1

- Remove incorrect checks if performance tracing should be enabled and rely on the transaction sampling decision instead (#600)
Expand Down
31 changes: 31 additions & 0 deletions src/Sentry/Laravel/Integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
namespace Sentry\Laravel;

use Illuminate\Routing\Route;
use Sentry\EventHint;
use Sentry\EventId;
use Sentry\ExceptionMechanism;
use Sentry\SentrySdk;
use Sentry\Tracing\TransactionSource;
use Throwable;
use function Sentry\addBreadcrumb;
use function Sentry\captureEvent;
use function Sentry\configureScope;
use Sentry\Breadcrumb;
use Sentry\Event;
Expand Down Expand Up @@ -162,4 +167,30 @@ public static function sentryBaggageMeta(): string

return sprintf('<meta name="baggage" content="%s"/>', $span->toBaggage());
}

/**
* Capture a unhandled exception and report it to Sentry.
*
* @param \Throwable $throwable
*
* @return \Sentry\EventId|null
*/
public static function captureUnhandledException(Throwable $throwable): ?EventId
{
$client = SentrySdk::getCurrentHub()->getClient();

// When Sentry is not configured, because for example no DSN
// is set the client can be null. If that is the case we cannot
// transmit the event so, exit early to prevent doing useless work
if ($client === null) {
return null;
}

$hint = EventHint::fromArray([
'exception' => $throwable,
'mechanism' => new ExceptionMechanism(ExceptionMechanism::TYPE_GENERIC, false),
]);

return $client->captureEvent(Event::createEvent(), $hint);
}
}

0 comments on commit f919570

Please sign in to comment.