* @copyright 2012-2019 PrestaShop SA * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ class ErrorHandlerMEP { public static function setErrorHandler() { if (!_PS_MODE_DEV_) { ini_set('display_errors', 'off'); } restore_error_handler(); set_error_handler(array(__CLASS__, 'errorHandler')); register_shutdown_function(array(__CLASS__, 'shutdown')); } public static function errorHandler($errno, $errstr, $errfile, $errline) { if (error_reporting() === 0) { return false; } if (!defined('E_RECOVERABLE_ERROR')) { define('E_RECOVERABLE_ERROR', 4096); } switch ($errno) { case E_RECOVERABLE_ERROR: case E_USER_ERROR: case E_ERROR: throw new Exception('Fatal error: ' . $errstr . ' in ' . $errfile . ' on line ' . $errline); //no break case E_USER_WARNING: case E_WARNING: // throw new Exception('Error: ' . $errstr . ' in ' . $errfile . ' on line ' . $errline); //no break case E_USER_NOTICE: case E_NOTICE: if (_PS_MODE_DEV_) { throw new Exception('Notice: ' . $errstr . ' in ' . $errfile . ' on line ' . $errline); } return true; default: throw new Exception('Unknown error: ' . $errstr . ' in ' . $errfile . ' on line ' . $errline); } } public static function shutdown() { $l = TransModMEP::getInstance(); if (function_exists('error_get_last')) { $error = error_get_last(); if ($error && $error['type'] === E_ERROR) { $message = $error['message']; $memory_regex = '/^Allowed memory size of (\d+) bytes exhausted \(tried to allocate (\d+) bytes\)$/u'; $time_regex = '/^Maximum execution time of (\d+) second exceeded/u'; if (preg_match($memory_regex, $message, $matches)) { $message = $l->l('Allowed memory size of', __FILE__) . ' '; $message .= self::convertMemory($matches[1]) . ' '; $message .= $l->l('exhausted', __FILE__) . ' ('; $message .= $l->l('tried to allocate', __FILE__) . ' '; $message .= self::convertMemory($matches[2]) . ' '; $message .= ')'; LoggerMEP::getInstance()->error($message); LoggerMEP::getInstance()->error( $l->l('Your web-server is too slow, not enough RAM.', __FILE__) ); LoggerMEP::getInstance()->error( $l->l('Try to reduce some of expert\'s settings.', __FILE__) ); } elseif (preg_match($time_regex, $message, $matches)) { $message = $l->l('Maximum execution time of', __FILE__) . ' '; $message .= (int)$matches[1] . ' '; $message .= $l->l('second exceeded', __FILE__); LoggerMEP::getInstance()->error($message); LoggerMEP::getInstance()->error( $l->l('Your web-server is too slow, increase PHP execution time limit.', __FILE__) ); LoggerMEP::getInstance()->error( $l->l('Try to reduce some of expert\'s settings.', __FILE__) ); } else { LoggerMEP::getInstance()->error($message); } die(Tools::jsonEncode(array( 'hasError' => LoggerMEP::getInstance()->hasError(), 'log' => LoggerMEP::getInstance()->getMessages() ))); } } exit; } public static $memory_units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB'); /** * @param $size * @return bool */ public static function convertMemory($size) { if (!$size) { return '0B'; } $i = floor(log($size, 1024)); $size = round($size / pow(1024, $i), 2); return $size . ' ' . self::$memory_units[(int)$i]; } }