Skip to content

Commit 7a3d222

Browse files
committed
Initial import
1 parent c00ed22 commit 7a3d222

8 files changed

+270
-1
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
composer.lock
3+
.idea

LICENSE LICENSE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 slashtrace
3+
Copyright (c) 2018 Victor Stanciu
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# SlashTrace - Awesome error handler - Raygun handler
2+
3+
This is the [Raygun](https://raygun.com/) handler for [SlashTrace](https://github.com/slashtrace/slashtrace).
4+
Use it to send your errors and exceptions to your Raygun account.
5+
6+
## Usage
7+
8+
1. Install using Composer:
9+
10+
```
11+
composer require slashtrace/slashtrace-raygun
12+
```
13+
14+
2. Hook it into SlashTrace:
15+
16+
```PHP
17+
use SlashTrace\SlashTrace;
18+
use SlashTrace\Raygun\RaygunHandler;
19+
20+
$handler = new RaygunHandler("Your Raygun API key");
21+
22+
$slashtrace = new SlashTrace();
23+
$slashtrace->addHandler($handler);
24+
```
25+
26+
Alternatively, you can pass in a pre-configured Raygun client when you instantiate the handler:
27+
28+
```
29+
$raygun = new Raygun4php\RaygunClient("Your Raygun API key");
30+
$handler = new RaygunHandler($raygun);
31+
32+
$slashtrace->addHandler($handler);
33+
```
34+
35+
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.

composer.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "slashtrace/slashtrace-raygun",
3+
"description": "Raygun handler for SlashTrace",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Victor Stanciu",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": ">= 5.6",
14+
"slashtrace/slashtrace": "^1.0",
15+
"mindscape/raygun4php": "1.*"
16+
},
17+
"require-dev": {
18+
"phpunit/phpunit": "^6.0"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"SlashTrace\\Raygun\\": "src/"
23+
}
24+
},
25+
"autoload-dev": {
26+
"psr-4": {
27+
"SlashTrace\\Raygun\\Tests\\": "tests/"
28+
}
29+
}
30+
}

phpunit.xml.dist

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="tests/bootstrap.php"
3+
colors="true"
4+
verbose="true"
5+
processIsolation="false"
6+
convertErrorsToExceptions="false"
7+
convertNoticesToExceptions="false"
8+
convertWarningsToExceptions="false">
9+
<testsuites>
10+
<testsuite>
11+
<directory>./tests</directory>
12+
</testsuite>
13+
</testsuites>
14+
<filter>
15+
<whitelist>
16+
<directory>./src</directory>
17+
</whitelist>
18+
</filter>
19+
</phpunit>

src/RaygunHandler.php

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace SlashTrace\Raygun;
4+
5+
use SlashTrace\Context\User;
6+
use SlashTrace\EventHandler\EventHandler;
7+
use SlashTrace\EventHandler\EventHandlerException;
8+
9+
use Raygun4php\RaygunClient;
10+
use Exception;
11+
12+
class RaygunHandler implements EventHandler
13+
{
14+
/** @var RaygunClient */
15+
private $raygun;
16+
17+
public function __construct($raygun)
18+
{
19+
$this->raygun = $raygun instanceof RaygunClient ? $raygun : new RaygunClient($raygun);
20+
}
21+
22+
/**
23+
* @param Exception $exception
24+
* @return int
25+
* @throws EventHandlerException
26+
*/
27+
public function handleException($exception)
28+
{
29+
try {
30+
$this->raygun->SendException($exception);
31+
} catch (Exception $e) {
32+
throw new EventHandlerException($e->getMessage(), $e->getCode(), $e);
33+
}
34+
return EventHandler::SIGNAL_CONTINUE;
35+
}
36+
37+
/**
38+
* @param User $user
39+
* @return void
40+
*/
41+
public function setUser(User $user)
42+
{
43+
$this->raygun->SetUser((string) $user->getId() ?: $user->getEmail(), null, $user->getName(), $user->getEmail());
44+
}
45+
46+
/**
47+
* @param string $title
48+
* @param array $data
49+
* @return void
50+
*/
51+
public function recordBreadcrumb($title, array $data = [])
52+
{
53+
// Breadcrumbs are currently not supported by the Raygun PHP SDK
54+
}
55+
56+
/**
57+
* @param string $release
58+
* @return void
59+
*/
60+
public function setRelease($release)
61+
{
62+
$this->raygun->SetVersion($release);
63+
}
64+
65+
/**
66+
* @param string $path
67+
* @return void
68+
*/
69+
public function setApplicationPath($path)
70+
{
71+
// Local application path is currently not supported by the Raygun PHP SDK
72+
}
73+
}

tests/RaygunHandlerTest.php

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace SlashTrace\Raygun\Tests;
4+
5+
use SlashTrace\Context\User;
6+
use SlashTrace\EventHandler\EventHandlerException;
7+
use SlashTrace\Raygun\RaygunHandler;
8+
9+
use PHPUnit\Framework\TestCase;
10+
use Raygun4php\RaygunClient;
11+
12+
use Exception;
13+
14+
class RaygunHandlerTest extends TestCase
15+
{
16+
public function testExceptionIsPassedToRaygunClient()
17+
{
18+
$exception = new Exception();
19+
20+
$raygun = $this->createMock(RaygunClient::class);
21+
$raygun->expects($this->once())
22+
->method("SendException")
23+
->with($exception);
24+
25+
$handler = new RaygunHandler($raygun);
26+
$handler->handleException($exception);
27+
}
28+
29+
public function testRaygunExceptionsAreHandled()
30+
{
31+
$originalException = new Exception();
32+
$raygunException = new Exception();
33+
34+
$raygun = $this->createMock(RaygunClient::class);
35+
$raygun->expects($this->once())
36+
->method("SendException")
37+
->with($originalException)
38+
->willThrowException($raygunException);
39+
40+
$handler = new RaygunHandler($raygun);
41+
try {
42+
$handler->handleException($originalException);
43+
$this->fail("Expected exception: " . EventHandlerException::class);
44+
} catch (EventHandlerException $e) {
45+
$this->assertSame($raygunException, $e->getPrevious());
46+
}
47+
}
48+
49+
public function testUserIsPassedToRaygunClient()
50+
{
51+
$user = new User();
52+
$user->setId(12345);
53+
$user->setEmail("[email protected]");
54+
$user->setName("Philip J. Fry");
55+
56+
$raygun = $this->createMock(RaygunClient::class);
57+
$raygun->expects($this->once())
58+
->method("SetUser")
59+
->with(
60+
$user->getId(),
61+
null,
62+
$user->getName(),
63+
$user->getEmail()
64+
);
65+
66+
$handler = new RaygunHandler($raygun);
67+
$handler->setUser($user);
68+
}
69+
70+
public function testPartialUserData()
71+
{
72+
$user = new User();
73+
$user->setEmail("[email protected]");
74+
75+
$raygun = $this->createMock(RaygunClient::class);
76+
$raygun->expects($this->once())
77+
->method("SetUser")
78+
->with($user->getEmail(), null, null, $user->getEmail());
79+
80+
$handler = new RaygunHandler($raygun);
81+
$handler->setUser($user);
82+
}
83+
84+
public function testReleaseIsPassedToSentryClient()
85+
{
86+
$release = "1.0.0";
87+
88+
$raygun = $this->createMock(RaygunClient::class);
89+
$raygun->expects($this->once())
90+
->method("SetVersion")
91+
->with($release);
92+
93+
$handler = new RaygunHandler($raygun);
94+
$handler->setRelease($release);
95+
}
96+
}

tests/bootstrap.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
$autoload_paths = [
4+
__DIR__ . "/../vendor/autoload.php",
5+
__DIR__ . "/../../../autoload.php",
6+
];
7+
8+
foreach ($autoload_paths as $path) {
9+
if (file_exists($path)) {
10+
require_once $path;
11+
return;
12+
}
13+
}

0 commit comments

Comments
 (0)