Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
Ability to turn off the shifting of backtrace args
Browse files Browse the repository at this point in the history
 Added ability to pass in the PHP error context. If present this will be added as the vars for the bottom stack frame.
  • Loading branch information
JonathanO committed Jan 19, 2013
1 parent e1f1a91 commit 002284b
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 18 deletions.
16 changes: 8 additions & 8 deletions lib/Raven/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ public function getIdent($ident)
* Deprecated
*/
public function message($message, $params=array(), $level=self::INFO,
$stack=false)
$stack=false, $vars = null)
{
return $this->captureMessage($message, $params, $level, $stack);
return $this->captureMessage($message, $params, $level, $stack, $vars);
}

/**
Expand All @@ -152,7 +152,7 @@ public function exception($exception)
* Log a message to sentry
*/
public function captureMessage($message, $params=array(), $level_or_options=array(),
$stack=false)
$stack=false, $vars = null)
{
// Gracefully handle messages which contain formatting characters, but were not
// intended to be used with formatting.
Expand All @@ -178,13 +178,13 @@ public function captureMessage($message, $params=array(), $level_or_options=arra
'params' => $params,
);

return $this->capture($data, $stack);
return $this->capture($data, $stack, $vars);
}

/**
* Log an exception to sentry
*/
public function captureException($exception, $culprit_or_options=null, $logger=null)
public function captureException($exception, $culprit_or_options=null, $logger=null, $vars=null)
{
if (in_array(get_class($exception), $this->exclude)) {
return null;
Expand Down Expand Up @@ -235,7 +235,7 @@ public function captureException($exception, $culprit_or_options=null, $logger=n

array_unshift($trace, $frame_where_exception_thrown);

return $this->capture($data, $trace);
return $this->capture($data, $trace, $vars);
}

/**
Expand Down Expand Up @@ -308,7 +308,7 @@ protected function get_extra_data()
return array();
}

public function capture($data, $stack)
public function capture($data, $stack, $vars = null)
{
$event_id = $this->uuid4();

Expand Down Expand Up @@ -339,7 +339,7 @@ public function capture($data, $stack)
if (!empty($stack)) {
if (!isset($data['sentry.interfaces.Stacktrace'])) {
$data['sentry.interfaces.Stacktrace'] = array(
'frames' => Raven_Stacktrace::get_stack_info($stack, $this->trace, $this->shift_vars),
'frames' => Raven_Stacktrace::get_stack_info($stack, $this->trace, $this->shift_vars, $vars),
);
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Raven/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public function __construct($client)
$this->client = $client;
}

public function handleException($e, $isError = false)
public function handleException($e, $isError = false, $vars = null)
{
$e->event_id = $this->client->getIdent($this->client->captureException($e));
$e->event_id = $this->client->getIdent($this->client->captureException($e, null, null, $vars));

if (!$isError && $this->call_existing_exception_handler && $this->old_exception_handler) {
call_user_func($this->old_exception_handler, $e);
Expand All @@ -49,7 +49,7 @@ public function handleError($code, $message, $file='', $line=0, $context=array()
if (!error_reporting()) { return; }

$e = new ErrorException($message, 0, $code, $file, $line);
$this->handleException($e, true);
$this->handleException($e, true, $context);

if ($this->call_existing_error_handler && $this->old_error_handler) {
call_user_func($this->old_error_handler, $code, $message, $file, $line, $context);
Expand Down
19 changes: 12 additions & 7 deletions lib/Raven/Stacktrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Raven_Stacktrace
'require_once',
);

public static function get_stack_info($frames, $trace=false, $shiftvars=true)
public static function get_stack_info($frames, $trace=false, $shiftvars=true, $errcontext = null)
{
/**
* PHP's way of storing backstacks seems bass-ackwards to me
Expand Down Expand Up @@ -60,14 +60,19 @@ public static function get_stack_info($frames, $trace=false, $shiftvars=true)
$module .= ':' . $nextframe['class'];
}

if ($trace) {
if ($shiftvars) {
$vars = self::get_frame_context($nextframe);
if (empty($result) && isset($errcontext)) {
// If we've been given an error context that can be used as the vars for the first frame.
$vars = $errcontext;
} else {
if ($trace) {
if ($shiftvars) {
$vars = self::get_frame_context($nextframe);
} else {
$vars = self::get_caller_frame_context($frame);
}
} else {
$vars = self::get_caller_frame_context($frame);
$vars = array();
}
} else {
$vars = array();
}

$result[] = array(
Expand Down
88 changes: 88 additions & 0 deletions test/Raven/Tests/StacktraceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,94 @@ public function testSimpleUnshiftedTrace()
$this->assertEquals('a_test($foo);', $frame["context_line"]);


}

public function testShiftedCaptureVars()
{
$stack = array(
array(
"file" => dirname(__FILE__) . "/resources/a.php",
"line" => 11,
"function" => "a_test",
"args"=> array(
"friend",
),
),
array(
"file" => dirname(__FILE__) . "/resources/b.php",
"line" => 3,
"args"=> array(
"/tmp/a.php",
),
"function" => "include_once",
),
);

$vars = array(
"foo" => "bar",
"baz" => "zoom"
);

$frames = Raven_Stacktrace::get_stack_info($stack, true, true, $vars);

$frame = $frames[0];
$this->assertEquals('b.php', $frame["module"]);
$this->assertEquals(3, $frame["lineno"]);
$this->assertNull($frame["function"]);
$this->assertEquals("include_once '/tmp/a.php';", $frame["context_line"]);
$this->assertEquals(array(), $frame['vars']);
$frame = $frames[1];
$this->assertEquals('a.php', $frame["module"]);
$this->assertEquals(11, $frame["lineno"]);
$this->assertEquals('include_once', $frame["function"]);
$this->assertEquals('a_test($foo);', $frame["context_line"]);
$this->assertEquals($vars, $frame['vars']);


}

public function testUnshiftedCaptureVars()
{
$stack = array(
array(
"file" => dirname(__FILE__) . "/resources/a.php",
"line" => 11,
"function" => "a_test",
"args"=> array(
"friend",
),
),
array(
"file" => dirname(__FILE__) . "/resources/b.php",
"line" => 3,
"args"=> array(
"/tmp/a.php",
),
"function" => "include_once",
),
);

$vars = array(
"foo" => "bar",
"baz" => "zoom"
);

$frames = Raven_Stacktrace::get_stack_info($stack, true, false, $vars);

$frame = $frames[0];
$this->assertEquals('b.php', $frame["module"]);
$this->assertEquals(3, $frame["lineno"]);
$this->assertNull($frame["function"]);
$this->assertEquals(array('param1' => '/tmp/a.php'), $frame['vars']);
$this->assertEquals("include_once '/tmp/a.php';", $frame["context_line"]);
$frame = $frames[1];
$this->assertEquals('a.php', $frame["module"]);
$this->assertEquals(11, $frame["lineno"]);
$this->assertEquals('include_once', $frame["function"]);
$this->assertEquals($vars, $frame['vars']);
$this->assertEquals('a_test($foo);', $frame["context_line"]);


}

public function testDoesFixFrameInfo()
Expand Down

0 comments on commit 002284b

Please sign in to comment.