Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added checkIgnore config option. (#45) #82

Merged
merged 4 commits into from
May 25, 2016
Merged
Show file tree
Hide file tree
Changes from 3 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
80 changes: 79 additions & 1 deletion src/rollbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,36 @@ public static function flush() {
}
}

class RollbarException {
private $level;
private $message;
private $err;
private $custom;

public function __construct($level, $message, Exception $err = null, $custom = null) {
$this->level = $level;
$this->message = $message;
$this->err = $err;
$this->custom = $custom;
}

public function getLevel() {
return $this->level;
}

public function getMessage() {
return $this->message;
}

public function getException() {
return $this->err;
}

public function getCustom() {
return $this->custom;
}
}

// Send errors that have these levels
if (!defined('ROLLBAR_INCLUDED_ERRNO_BITMASK')) {
define('ROLLBAR_INCLUDED_ERRNO_BITMASK', E_ERROR | E_WARNING | E_PARSE | E_CORE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR);
Expand Down Expand Up @@ -108,6 +138,7 @@ class RollbarNotifier {
public $person = null;
public $person_fn = null;
public $root = '';
public $checkIgnore = null;
public $scrub_fields = array('passwd', 'pass', 'password', 'secret', 'confirm_password',
'password_confirmation', 'auth_token', 'csrf_token');
public $shift_function = true;
Expand All @@ -118,7 +149,7 @@ class RollbarNotifier {

private $config_keys = array('access_token', 'base_api_url', 'batch_size', 'batched', 'branch',
'capture_error_backtraces', 'code_version', 'environment', 'error_sample_rates', 'handler',
'agent_log_location', 'host', 'logger', 'included_errno', 'person', 'person_fn', 'root',
'agent_log_location', 'host', 'logger', 'included_errno', 'person', 'person_fn', 'root', 'checkIgnore',
'scrub_fields', 'shift_function', 'timeout', 'report_suppressed', 'use_error_reporting', 'proxy');

// cached values for request/server/person data
Expand Down Expand Up @@ -236,6 +267,35 @@ public function queueSize() {
return count($this->_queue);
}

/**
* Run the checkIgnore function and determine whether to send the Exception to the API or not.
*
* @param bool $isUncaught
* @param RollbarException $caller_args [level, message, err, custom]
* @param array $payload Data being sent to the API
* @return bool
*/
protected function _shouldIgnore($isUncaught, RollbarException $caller_args, array $payload)
{
try {
if (is_callable($this->checkIgnore)
&& call_user_func_array($this->checkIgnore, array($isUncaught,$caller_args,$payload))
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm. So RollbarException seems to be duplicating a lot of stuff that's already present in $payload. Is there a notable difference?

Copy link
Author

Choose a reason for hiding this comment

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

I literally just copied the functionality and tried to ensure it was consistent with the JS version. I agree it doesn't really make sense; I wasn't sure how to handle the two cases where there is not an exception:

  • Logging a message
  • Logging a PHP error

) {
$this->log_info('This item was not sent to Rollbar because it was ignored. '
. 'This can happen if a custom checkIgnore() function was used.');

return true;
}
} catch (Exception $e) {
// Disable the custom checkIgnore and report errors in the checkIgnore function
$this->checkIgnore = null;
$this->log_error("Removing custom checkIgnore(). Error while calling custom checkIgnore function:\n"
. $e->getMessage());
}

return false;
}

/**
* @param Exception $exc
*/
Expand Down Expand Up @@ -276,6 +336,12 @@ protected function _report_exception(Exception $exc, $extra_data = null, $payloa
array_walk_recursive($data, array($this, '_sanitize_utf8'));

$payload = $this->build_payload($data);

// Determine whether to send the request to the API.
if ($this->_shouldIgnore(true, new RollbarException($data['level'], $exc->getMessage(), $exc), $payload)) {
return;
}

$this->send_payload($payload);

return $data['uuid'];
Expand Down Expand Up @@ -408,6 +474,12 @@ protected function _report_php_error($errno, $errstr, $errfile, $errline) {
array_walk_recursive($data, array($this, '_sanitize_utf8'));

$payload = $this->build_payload($data);

// Determine whether to send the request to the API.
if ($this->_shouldIgnore(true, new RollbarException($level, $errstr), $payload)) {
return;
}

$this->send_payload($payload);

return $data['uuid'];
Expand Down Expand Up @@ -449,6 +521,12 @@ protected function _report_message($message, $level, $extra_data, $payload_data)
array_walk_recursive($data, array($this, '_sanitize_utf8'));

$payload = $this->build_payload($data);

// Determine whether to send the request to the API.
if ($this->_shouldIgnore(true, new RollbarException($level, $message), $payload)) {
return;
}

$this->send_payload($payload);

return $data['uuid'];
Expand Down
23 changes: 23 additions & 0 deletions tests/RollbarNotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,29 @@ public function testSimpleException() {
$this->assertValidUUID($uuid);
}

public function testCheckIgnore() {
$config = self::$simpleConfig;
$config['checkIgnore'] = function ($isUncaught, $caller_args, $payload) {
if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Baiduspider') !== false) {
// ignore baidu spider
return true;
}

// no other ignores
return false;
};

$notifier = new RollbarNotifier($config);

// Should ignore this exception.
$_SERVER = array('HTTP_USER_AGENT' => 'Baiduspider');
$this->assertNull($notifier->report_exception(new Exception("test exception")));

// Shouldn't ignore this exception.
$_SERVER = array();
$this->assertValidUUID($notifier->report_exception(new Exception("test exception")));
}

public function testFlush() {
$config = self::$simpleConfig;
$config['batched'] = true;
Expand Down