Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ public function __construct(Configuration $config, ResolverInterface $resolver =
$this->http = new HttpClient($config, $guzzle ?: static::makeGuzzle());
$this->sessionTracker = new SessionTracker($config);

$this->pipeline->pipe(new NotificationSkipper($config));
$this->pipeline->pipe(new BreadcrumbData($this->recorder));
$this->pipeline->pipe(new SessionData($this));
$this->registerMiddleware(new NotificationSkipper($config));
$this->registerMiddleware(new BreadcrumbData($this->recorder));
$this->registerMiddleware(new SessionData($this));

register_shutdown_function([$this, 'flush']);
}
Expand Down Expand Up @@ -178,7 +178,7 @@ public function getConfig()
*/
public function registerCallback(callable $callback)
{
$this->pipeline->pipe(new CallbackBridge($callback));
$this->registerMiddleware(new CallbackBridge($callback));

return $this;
}
Expand All @@ -200,6 +200,24 @@ public function registerDefaultCallbacks()
return $this;
}

/**
* Register a middleware object to the pipeline.
*
* @param callable $middleware
*
* @return $this
*/
public function registerMiddleware(callable $middleware)
{
if (is_callable($middleware)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need to check this. The type annotation already raises an error.

$this->pipeline->pipe($middleware);
} else {
syslog(LOG_WARNING, 'Middleware '.get_class($middleware).' could not be added to the pipeline');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is not reachable, and should be deleted.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct, I was just verifying the earlier versions of PHP had the same behaviour

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They should do. Callable was introduced in php 5.4 as a typehint, and the behavior has remained the same since.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what kind of error is thrown in earlier versions of PHP, in PHP 7 it's quite explicitly a TypeError, any idea for PHP 5.6?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

return $this;
}

/**
* Record the given breadcrumb.
*
Expand Down Expand Up @@ -277,27 +295,21 @@ public function notifyError($name, $message, callable $callback = null)
*/
public function notify(Report $report, callable $callback = null)
{
$initialUnhandled = $report->getUnhandled();
$initialSeverity = $report->getSeverity();
$initialReason = $report->getSeverityReason();
$this->pipeline->execute($report, function ($report) use ($callback, $initialUnhandled, $initialSeverity, $initialReason) {
$this->pipeline->execute($report, function ($report) use ($callback) {
if ($callback) {
if ($callback($report) === false) {
$resolvedReport = null;

$bridge = new CallbackBridge($callback);
$bridge($report, function ($report) use ($resolvedReport) {
$resolvedReport = $report;
});
if ($resolvedReport) {
$report = $resolvedReport;
} else {
return;
}
}

$report->setUnhandled($initialUnhandled);
if ($report->getSeverity() != $initialSeverity) {
// Severity has been changed via callbacks -> severity reason should be userCallbackSetSeverity
$report->setSeverityReason([
'type' => 'userCallbackSetSeverity',
]);
} else {
// Otherwise we ensure the original severity reason is preserved
$report->setSeverityReason($initialReason);
}

$this->http->queue($report);
});

Expand Down
15 changes: 15 additions & 0 deletions src/Middleware/CallbackBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,24 @@ public function __construct(callable $callback)
*/
public function __invoke(Report $report, callable $next)
{
$initialUnhandled = $report->getUnhandled();
$initialSeverity = $report->getSeverity();
$initialReason = $report->getSeverityReason();

$callback = $this->callback;

if ($callback($report) !== false) {
$report->setUnhandled($initialUnhandled);
if ($report->getSeverity() != $initialSeverity) {
// Severity has been changed via callbacks -> severity reason should be userCallbackSetSeverity
$report->setSeverityReason([
'type' => 'userCallbackSetSeverity',
]);
} else {
// Otherwise we ensure the original severity reason is preserved
$report->setSeverityReason($initialReason);
}

$next($report);
}
}
Expand Down
98 changes: 98 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,104 @@ public function testMetaDataWorks()
$this->assertSame(['foo' => 'baz'], $report->getMetaData());
}

public function testCustomMiddlewareWorks()
{
$this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle);

$this->client->registerMiddleware(function ($report, callable $next) {
$report->setMetaData(['middleware' => 'registered']);
$next($report);
});

$this->client->notify($report = Report::fromNamedError($this->config, 'Name'));

$this->assertSame(['middleware' => 'registered'], $report->getMetaData());
}

public function testMiddlewareCanModifySeverity()
{
$this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle);

$this->client->registerMiddleware(function ($report, callable $next) {
$report->setSeverity('info');
$next($report);
});

$report = Report::fromNamedError($this->config, 'Name');
$report->setSeverity('error');

$this->client->notify($report);

$this->assertSame('info', $report->getSeverity());
}

public function testMiddlewareCanModifyUnhandled()
{
$this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle);

$this->client->registerMiddleware(function ($report, callable $next) {
$report->setUnhandled(true);
$next($report);
});

$report = Report::fromNamedError($this->config, 'Name');
$report->setUnhandled(false);

$this->client->notify($report);

$this->assertSame(true, $report->getUnhandled());
}

public function testMiddlewareCanModifySeverityReason()
{
$this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle);

$this->client->registerMiddleware(function ($report, callable $next) {
$report->setSeverityReason([
'type' => 'right',
]);
$next($report);
});

$report = Report::fromNamedError($this->config, 'Name');
$report->setSeverityReason([
'type' => 'wrong',
]);

$this->client->notify($report);

$this->assertSame(['type' => 'right'], $report->getSeverityReason());
}

public function testNotOverriddenByCallbacks()
{
$this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle);

$this->client->registerMiddleware(function ($report, callable $next) {
$report->setUnhandled(true);
$report->setSeverityReason([
'type' => 'right',
]);
$next($report);
});

$report = Report::fromNamedError($this->config, 'Name');
$report->setUnhandled(false);
$report->setSeverityReason([
'type' => 'wrong',
]);

$this->client->notify($report, function ($report) {
$report->setUnhandled(false);
$report->setSeverityReason([
'type' => 'wrong',
]);
});

$this->assertSame(['type' => 'right'], $report->getSeverityReason());
$this->assertSame(true, $report->getUnhandled());
}

public function testBreadcrumbsWorks()
{
$this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle);
Expand Down
27 changes: 27 additions & 0 deletions tests/Middleware/CallbackBridgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,31 @@ public function testSkips()
echo 'reached';
});
}

public function maintainsReportData()
{
$report = Report::fromPHPThrowable(
$this->config,
new Exception('Oh no')
);
$report->setSeverity('error');
$report->setUnhandled(true);
$report->setSeverityReason([
'type' => 'unhandledException',
]);

$middleware = new CallbackBridge(function ($report) {
$report->setSeverity('info');
$report->setUnhandled(false);
$report->setSeverityReason([
'type' => 'not my',
]);
});

$middleware($report, function ($report) {
$this->assertSame('error', $report->getSeverity());
$this->assertSame(true, $report->getUnhandled());
$this->assertSame(['type' => 'unhandledException'], $report->getSeverityReason());
});
}
}