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

Trigger errors from AJAX methods in non-production mode. #3267

Merged
merged 7 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions module/VuFind/src/VuFind/Controller/AjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ class AjaxController extends AbstractActionController implements TranslatorAware
*/
public function __construct(PluginManager $am)
{
// Add notices to a key in the output
set_error_handler([static::class, 'storeError']);
// Prevent errors, notices etc. from being displayed so that they don't mess
// with the output (only in production mode):
if ('production' === APPLICATION_ENV) {
ini_set('display_errors', '0');
}
$this->ajaxManager = $am;
}

Expand Down
37 changes: 1 addition & 36 deletions module/VuFind/src/VuFind/Controller/AjaxResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
use VuFind\AjaxHandler\AjaxHandlerInterface as Ajax;
use VuFind\AjaxHandler\PluginManager;

use function count;

/**
* Trait to allow AJAX response generation.
*
Expand All @@ -49,15 +47,6 @@
*/
trait AjaxResponseTrait
{
/**
* Array of PHP errors captured during execution. Add this code to your
* constructor in order to populate the array:
* set_error_handler([static::class, 'storeError']);
*
* @var array
*/
protected static $php_errors = [];

/**
* AJAX Handler plugin manager
*
Expand All @@ -80,14 +69,7 @@ protected function formatContent($type, $data, $httpCode)
switch ($type) {
case 'application/javascript':
case 'application/json':
$output = ['data' => $data];
if (
'development' == APPLICATION_ENV
&& count(self::$php_errors) > 0
) {
$output['php_errors'] = self::$php_errors;
}
return json_encode($output);
return json_encode(compact('data'));
case 'text/plain':
return ((null !== $httpCode && $httpCode >= 400) ? 'ERROR ' : 'OK ')
. $data;
Expand Down Expand Up @@ -174,21 +156,4 @@ protected function callAjaxMethod($method, $type = 'application/json')
Ajax::STATUS_HTTP_BAD_REQUEST
);
}

/**
* Store the errors for later, to be added to the output
*
* @param string $errno Error code number
* @param string $errstr Error message
* @param string $errfile File where error occurred
* @param string $errline Line number of error
*
* @return bool Always true to cancel default error handling
*/
public static function storeError($errno, $errstr, $errfile, $errline)
{
self::$php_errors[] = "ERROR [$errno] - " . $errstr . "<br>\n"
. ' Occurred in ' . $errfile . ' on line ' . $errline . '.';
return true;
}
}