Skip to content

Commit

Permalink
Avoid constructing Error objects if we are going to skip notification
Browse files Browse the repository at this point in the history
  • Loading branch information
loopj committed Jul 1, 2015
1 parent aa6ab28 commit 142f0f8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 60 deletions.
25 changes: 12 additions & 13 deletions src/Bugsnag/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,22 +414,24 @@ public function notifyError($name, $message, array $metaData = null, $severity =
// Exception handler callback, should only be called internally by PHP's set_exception_handler
public function exceptionHandler($exception)
{
if(!$this->config->autoNotify) {
return;
}

$error = Bugsnag_Error::fromPHPException($this->config, $this->diagnostics, $exception);
$error->setSeverity("error");

if (!$error->shouldIgnore() && $this->config->autoNotify) {
$this->notify($error);
}
$this->notify($error);
}

// Exception handler callback, should only be called internally by PHP's set_error_handler
public function errorHandler($errno, $errstr, $errfile = '', $errline = 0)
{
$error = Bugsnag_Error::fromPHPError($this->config, $this->diagnostics, $errno, $errstr, $errfile, $errline);

if (!$error->shouldIgnore() && $this->config->autoNotify) {
$this->notify($error);
if(!$this->config->autoNotify || $this->config->shouldIgnoreErrorCode($errno)) {
return;
}

$error = Bugsnag_Error::fromPHPError($this->config, $this->diagnostics, $errno, $errstr, $errfile, $errline);
$this->notify($error);
}

// Shutdown handler callback, called when the PHP process has finished running
Expand All @@ -440,13 +442,10 @@ public function shutdownHandler()
$lastError = error_get_last();

// Check if a fatal error caused this shutdown
if (!is_null($lastError) && Bugsnag_ErrorTypes::isFatal($lastError['type'])) {
if (!is_null($lastError) && Bugsnag_ErrorTypes::isFatal($lastError['type']) && $this->config->autoNotify && !$this->config->shouldIgnoreErrorCode($lastError['type'])) {
$error = Bugsnag_Error::fromPHPError($this->config, $this->diagnostics, $lastError['type'], $lastError['message'], $lastError['file'], $lastError['line'], true);
$error->setSeverity("error");

if (!$error->shouldIgnore() && $this->config->autoNotify) {
$this->notify($error);
}
$this->notify($error);
}

// Flush any buffered errors
Expand Down
11 changes: 11 additions & 0 deletions src/Bugsnag/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ public function shouldNotify()
return is_null($this->notifyReleaseStages) || (is_array($this->notifyReleaseStages) && in_array($this->releaseStage, $this->notifyReleaseStages));
}

public function shouldIgnoreErrorCode($code)
{
if (isset($this->errorReportingLevel)) {
return !($this->errorReportingLevel & $code);
} else {
return !(error_reporting() & $code);
}

return false;
}

public function setProjectRoot($projectRoot)
{
$this->projectRoot = $projectRoot;
Expand Down
31 changes: 4 additions & 27 deletions src/Bugsnag/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class Bugsnag_Error
public $metaData = array();
public $config;
public $diagnostics;
public $code;
public $previous;
public $groupingHash;

Expand Down Expand Up @@ -74,21 +73,14 @@ public function setGroupingHash($groupingHash)

return $this;
}

public function setStacktrace($stacktrace)
{
$this->stacktrace = $stacktrace;

return $this;
}

public function setCode($code)
{
$this->code = $code;

return $this;
}

public function setSeverity($severity)
{
if (!is_null($severity)) {
Expand Down Expand Up @@ -132,8 +124,7 @@ public function setPHPError($code, $message, $file, $line, $fatal = false)
$this->setName(Bugsnag_ErrorTypes::getName($code))
->setMessage($message)
->setSeverity(Bugsnag_ErrorTypes::getSeverity($code))
->setStacktrace($stacktrace)
->setCode($code);
->setStacktrace($stacktrace);

return $this;
}
Expand All @@ -156,20 +147,6 @@ public function setPrevious($exception)
return $this;
}

public function shouldIgnore()
{
// Check if we should ignore errors of this type
if (isset($this->code)) {
if (isset($this->config->errorReportingLevel)) {
return !($this->config->errorReportingLevel & $this->code);
} else {
return !(error_reporting() & $this->code);
}
}

return false;
}

public function toArray()
{
$errorArray = array(
Expand All @@ -182,11 +159,11 @@ public function toArray()
'exceptions' => $this->exceptionArray(),
'metaData' => $this->cleanupObj($this->metaData),
);

if (isset($this->groupingHash)) {
$errorArray['groupingHash'] = $this->groupingHash;
}

return $errorArray;
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Bugsnag/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,18 @@ public function testNotifier()
$this->assertEquals($this->config->notifier['name'], "Bugsnag PHP (Official)");
$this->assertEquals($this->config->notifier['url'], "https://bugsnag.com");
}

public function testShouldIgnore()
{
$this->config->errorReportingLevel = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED;

$this->assertTrue($this->config->shouldIgnoreErrorCode(E_NOTICE));
}

public function testShouldNotIgnore()
{
$this->config->errorReportingLevel = E_ALL;

$this->assertfalse($this->config->shouldIgnoreErrorCode(E_NOTICE));
}
}
22 changes: 2 additions & 20 deletions tests/Bugsnag/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,6 @@ public function testMetaDataMerging()
$this->assertEquals($errorArray['metaData']["Testing"]["localArray"], "yo");
}

public function testShouldIgnore()
{
$this->config->errorReportingLevel = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED;

$this->error->setPHPError(E_NOTICE, "Broken", "file", 123);

$this->assertTrue($this->error->shouldIgnore());
}

public function testShouldNotIgnore()
{
$this->config->errorReportingLevel = E_ALL;

$this->error->setPHPError(E_NOTICE, "Broken", "file", 123);

$this->assertfalse($this->error->shouldIgnore());
}

public function testFiltering()
{
$this->error->setMetaData(array("Testing" => array("password" => "123456")));
Expand Down Expand Up @@ -129,15 +111,15 @@ public function testPreviousException()
$this->assertEquals($errorArray['exceptions'][1]['message'], 'secondly');
}
}

public function testErrorGroupingHash()
{
$this->error->setGroupingHash('herp#derp');

$errorArray = $this->error->toArray();
$this->assertEquals($errorArray['groupingHash'], 'herp#derp');
}

public function testErrorGroupingHashNotSet()
{
$errorArray = $this->error->toArray();
Expand Down

0 comments on commit 142f0f8

Please sign in to comment.