Skip to content

Commit

Permalink
PHP 7: when there is a fatal error in API request processing, display…
Browse files Browse the repository at this point in the history
… the original error in the api response (#10406)

* PHP 7: when there is a fatal error in API request processing, display  the original error

* PHP 7 compatibility
  • Loading branch information
mattab committed Aug 23, 2016
1 parent 26e10c2 commit 5dd7d84
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 16 deletions.
7 changes: 6 additions & 1 deletion core/API/ApiRenderer.php
Expand Up @@ -39,7 +39,12 @@ public function renderSuccess($message)
return 'Success:' . $message;
}

public function renderException($message, \Exception $exception)
/**
* @param $message
* @param Exception|\Throwable $exception
* @return mixed
*/
public function renderException($message, $exception)
{
return $message;
}
Expand Down
10 changes: 7 additions & 3 deletions core/API/ResponseBuilder.php
Expand Up @@ -138,10 +138,10 @@ public function getResponseException($e)
}

/**
* @param Exception $e
* @param Exception|\Throwable $e
* @return Exception
*/
private function decorateExceptionWithDebugTrace(Exception $e)
private function decorateExceptionWithDebugTrace($e)
{
// If we are in tests, show full backtrace
if (defined('PIWIK_PATH_TEST_TO_ROOT')) {
Expand All @@ -157,7 +157,11 @@ private function decorateExceptionWithDebugTrace(Exception $e)
return $e;
}

private function formatExceptionMessage(Exception $exception)
/**
* @param Exception|\Throwable $exception
* @return string
*/
private function formatExceptionMessage($exception)
{
$message = $exception->getMessage();
if (\Piwik_ShouldPrintBackTraceWithMessage()) {
Expand Down
2 changes: 1 addition & 1 deletion core/Tracker/Response.php
Expand Up @@ -175,7 +175,7 @@ protected function getMessageFromException($e)
return $e->getMessage();
}

protected function logExceptionToErrorLog(Exception $e)
protected function logExceptionToErrorLog($e)
{
error_log(sprintf("Error in Piwik (tracker): %s", str_replace("\n", " ", $this->getMessageFromException($e))));
}
Expand Down
7 changes: 6 additions & 1 deletion plugins/API/Renderer/Console.php
Expand Up @@ -16,7 +16,12 @@
class Console extends ApiRenderer
{

public function renderException($message, \Exception $exception)
/**
* @param $message
* @param Exception|\Throwable $exception
* @return string
*/
public function renderException($message, $exception)
{
self::sendHeader();

Expand Down
7 changes: 6 additions & 1 deletion plugins/API/Renderer/Csv.php
Expand Up @@ -23,7 +23,12 @@ public function renderSuccess($message)
return "message\n" . $message;
}

public function renderException($message, \Exception $exception)
/**
* @param $message
* @param Exception|\Throwable $exception
* @return string
*/
public function renderException($message, $exception)
{
Common::sendHeader('Content-Type: text/html; charset=utf-8', true);
return 'Error: ' . $message;
Expand Down
7 changes: 6 additions & 1 deletion plugins/API/Renderer/Html.php
Expand Up @@ -16,7 +16,12 @@
class Html extends ApiRenderer
{

public function renderException($message, \Exception $exception)
/**
* @param $message
* @param Exception|\Throwable $exception
* @return string
*/
public function renderException($message, $exception)
{
Common::sendHeader('Content-Type: text/plain; charset=utf-8', true);

Expand Down
7 changes: 6 additions & 1 deletion plugins/API/Renderer/Json.php
Expand Up @@ -31,7 +31,12 @@ public function renderSuccess($message)
return $this->applyJsonpIfNeeded($result);
}

public function renderException($message, \Exception $exception)
/**
* @param $message
* @param Exception|\Throwable $exception
* @return string
*/
public function renderException($message, $exception)
{
$exceptionMessage = str_replace(array("\r\n", "\n"), "", $message);

Expand Down
8 changes: 7 additions & 1 deletion plugins/API/Renderer/Original.php
Expand Up @@ -18,7 +18,13 @@ public function renderSuccess($message)
return true;
}

public function renderException($message, \Exception $exception)
/**
* @param $message
* @param \Exception|\Throwable $exception
* @throws \Exception|\Throwable
* @return void
*/
public function renderException($message, $exception)
{
throw $exception;
}
Expand Down
7 changes: 6 additions & 1 deletion plugins/API/Renderer/Php.php
Expand Up @@ -23,7 +23,12 @@ public function renderSuccess($message)
return $this->serializeIfNeeded($success);
}

public function renderException($message, \Exception $exception)
/**
* @param $message
* @param Exception|\Throwable $exception
* @return string
*/
public function renderException($message, $exception)
{
$message = array('result' => 'error', 'message' => $message);

Expand Down
7 changes: 6 additions & 1 deletion plugins/API/Renderer/Rss.php
Expand Up @@ -16,7 +16,12 @@
class Rss extends ApiRenderer
{

public function renderException($message, \Exception $exception)
/**
* @param $message
* @param \Exception|\Throwable $exception
* @return string
*/
public function renderException($message, $exception)
{
self::sendHeader('plain');

Expand Down
7 changes: 6 additions & 1 deletion plugins/API/Renderer/Xml.php
Expand Up @@ -24,7 +24,12 @@ public function renderSuccess($message)
"</result>";
}

public function renderException($message, \Exception $exception)
/**
* @param $message
* @param \Exception|\Throwable $exception
* @return string
*/
public function renderException($message, $exception)
{
return '<?xml version="1.0" encoding="utf-8" ?>' . "\n" .
"<result>\n" .
Expand Down
Expand Up @@ -13,7 +13,7 @@

class Response extends \Piwik\Plugins\BulkTracking\Tracker\Response
{
protected function logExceptionToErrorLog(Exception $e)
protected function logExceptionToErrorLog($e)
{
// prevent from writing to console in tests
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/BulkTracking/tests/Unit/ResponseTest.php
Expand Up @@ -15,7 +15,7 @@

class TestResponse extends Response {

protected function logExceptionToErrorLog(Exception $e)
protected function logExceptionToErrorLog($e)
{
// prevent console from outputting the error_log message
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPUnit/Unit/Tracker/ResponseTest.php
Expand Up @@ -16,7 +16,7 @@

class TestResponse extends Response {

protected function logExceptionToErrorLog(Exception $e)
protected function logExceptionToErrorLog($e)
{
// prevent console from outputting the error_log message
}
Expand Down

0 comments on commit 5dd7d84

Please sign in to comment.