-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c00ed22
commit 7a3d222
Showing
8 changed files
with
270 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/vendor/ | ||
composer.lock | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# SlashTrace - Awesome error handler - Raygun handler | ||
|
||
This is the [Raygun](https://raygun.com/) handler for [SlashTrace](https://github.com/slashtrace/slashtrace). | ||
Use it to send your errors and exceptions to your Raygun account. | ||
|
||
## Usage | ||
|
||
1. Install using Composer: | ||
|
||
``` | ||
composer require slashtrace/slashtrace-raygun | ||
``` | ||
|
||
2. Hook it into SlashTrace: | ||
|
||
```PHP | ||
use SlashTrace\SlashTrace; | ||
use SlashTrace\Raygun\RaygunHandler; | ||
|
||
$handler = new RaygunHandler("Your Raygun API key"); | ||
|
||
$slashtrace = new SlashTrace(); | ||
$slashtrace->addHandler($handler); | ||
``` | ||
|
||
Alternatively, you can pass in a pre-configured Raygun client when you instantiate the handler: | ||
|
||
``` | ||
$raygun = new Raygun4php\RaygunClient("Your Raygun API key"); | ||
$handler = new RaygunHandler($raygun); | ||
$slashtrace->addHandler($handler); | ||
``` | ||
|
||
Read the [SlashTrace](https://github.com/slashtrace/slashtrace) docs to see how to capture errors and exceptions, and how to attach additional data to your events. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "slashtrace/slashtrace-raygun", | ||
"description": "Raygun handler for SlashTrace", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Victor Stanciu", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">= 5.6", | ||
"slashtrace/slashtrace": "^1.0", | ||
"mindscape/raygun4php": "1.*" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^6.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"SlashTrace\\Raygun\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"SlashTrace\\Raygun\\Tests\\": "tests/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="tests/bootstrap.php" | ||
colors="true" | ||
verbose="true" | ||
processIsolation="false" | ||
convertErrorsToExceptions="false" | ||
convertNoticesToExceptions="false" | ||
convertWarningsToExceptions="false"> | ||
<testsuites> | ||
<testsuite> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory>./src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace SlashTrace\Raygun; | ||
|
||
use SlashTrace\Context\User; | ||
use SlashTrace\EventHandler\EventHandler; | ||
use SlashTrace\EventHandler\EventHandlerException; | ||
|
||
use Raygun4php\RaygunClient; | ||
use Exception; | ||
|
||
class RaygunHandler implements EventHandler | ||
{ | ||
/** @var RaygunClient */ | ||
private $raygun; | ||
|
||
public function __construct($raygun) | ||
{ | ||
$this->raygun = $raygun instanceof RaygunClient ? $raygun : new RaygunClient($raygun); | ||
} | ||
|
||
/** | ||
* @param Exception $exception | ||
* @return int | ||
* @throws EventHandlerException | ||
*/ | ||
public function handleException($exception) | ||
{ | ||
try { | ||
$this->raygun->SendException($exception); | ||
} catch (Exception $e) { | ||
throw new EventHandlerException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
return EventHandler::SIGNAL_CONTINUE; | ||
} | ||
|
||
/** | ||
* @param User $user | ||
* @return void | ||
*/ | ||
public function setUser(User $user) | ||
{ | ||
$this->raygun->SetUser((string) $user->getId() ?: $user->getEmail(), null, $user->getName(), $user->getEmail()); | ||
} | ||
|
||
/** | ||
* @param string $title | ||
* @param array $data | ||
* @return void | ||
*/ | ||
public function recordBreadcrumb($title, array $data = []) | ||
{ | ||
// Breadcrumbs are currently not supported by the Raygun PHP SDK | ||
} | ||
|
||
/** | ||
* @param string $release | ||
* @return void | ||
*/ | ||
public function setRelease($release) | ||
{ | ||
$this->raygun->SetVersion($release); | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* @return void | ||
*/ | ||
public function setApplicationPath($path) | ||
{ | ||
// Local application path is currently not supported by the Raygun PHP SDK | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace SlashTrace\Raygun\Tests; | ||
|
||
use SlashTrace\Context\User; | ||
use SlashTrace\EventHandler\EventHandlerException; | ||
use SlashTrace\Raygun\RaygunHandler; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Raygun4php\RaygunClient; | ||
|
||
use Exception; | ||
|
||
class RaygunHandlerTest extends TestCase | ||
{ | ||
public function testExceptionIsPassedToRaygunClient() | ||
{ | ||
$exception = new Exception(); | ||
|
||
$raygun = $this->createMock(RaygunClient::class); | ||
$raygun->expects($this->once()) | ||
->method("SendException") | ||
->with($exception); | ||
|
||
$handler = new RaygunHandler($raygun); | ||
$handler->handleException($exception); | ||
} | ||
|
||
public function testRaygunExceptionsAreHandled() | ||
{ | ||
$originalException = new Exception(); | ||
$raygunException = new Exception(); | ||
|
||
$raygun = $this->createMock(RaygunClient::class); | ||
$raygun->expects($this->once()) | ||
->method("SendException") | ||
->with($originalException) | ||
->willThrowException($raygunException); | ||
|
||
$handler = new RaygunHandler($raygun); | ||
try { | ||
$handler->handleException($originalException); | ||
$this->fail("Expected exception: " . EventHandlerException::class); | ||
} catch (EventHandlerException $e) { | ||
$this->assertSame($raygunException, $e->getPrevious()); | ||
} | ||
} | ||
|
||
public function testUserIsPassedToRaygunClient() | ||
{ | ||
$user = new User(); | ||
$user->setId(12345); | ||
$user->setEmail("[email protected]"); | ||
$user->setName("Philip J. Fry"); | ||
|
||
$raygun = $this->createMock(RaygunClient::class); | ||
$raygun->expects($this->once()) | ||
->method("SetUser") | ||
->with( | ||
$user->getId(), | ||
null, | ||
$user->getName(), | ||
$user->getEmail() | ||
); | ||
|
||
$handler = new RaygunHandler($raygun); | ||
$handler->setUser($user); | ||
} | ||
|
||
public function testPartialUserData() | ||
{ | ||
$user = new User(); | ||
$user->setEmail("[email protected]"); | ||
|
||
$raygun = $this->createMock(RaygunClient::class); | ||
$raygun->expects($this->once()) | ||
->method("SetUser") | ||
->with($user->getEmail(), null, null, $user->getEmail()); | ||
|
||
$handler = new RaygunHandler($raygun); | ||
$handler->setUser($user); | ||
} | ||
|
||
public function testReleaseIsPassedToSentryClient() | ||
{ | ||
$release = "1.0.0"; | ||
|
||
$raygun = $this->createMock(RaygunClient::class); | ||
$raygun->expects($this->once()) | ||
->method("SetVersion") | ||
->with($release); | ||
|
||
$handler = new RaygunHandler($raygun); | ||
$handler->setRelease($release); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
$autoload_paths = [ | ||
__DIR__ . "/../vendor/autoload.php", | ||
__DIR__ . "/../../../autoload.php", | ||
]; | ||
|
||
foreach ($autoload_paths as $path) { | ||
if (file_exists($path)) { | ||
require_once $path; | ||
return; | ||
} | ||
} |