Detailed Error Exceptions in Magento Ecommerce
By default the Magento exception printing leaves out a significant part of every exception.
Alan Barber on our team repurposed some code to make error handling work more easily in Magento:
By modifying app/Mage.php , you can create a more detailed error report. This is very useful in discovering SQL errors, as the detailed exception will print the full query.
To implement this, navigate to {magento_root}/app/Mage.php
Replace
public static function printException
with the following code.
public static function printException(Exception $e, $extra = ”)
{
if (self::$_isDeveloperMode) {
print ‘<pre>’;
if (!empty($extra)) {
print $extra . “nn”;
}
print $e->getMessage() . “nn”;
self::detailedExceptionDump($e);
print ‘</pre>’;
}
else {
self::getConfig()->createDirIfNotExists(self::getBaseDir(‘var’) . DS . ‘report’);
$reportId = intval(microtime(true) * rand(100, 1000));
$reportFile = self::getBaseDir(‘var’) . DS . ‘report’ . DS . $reportId;
$reportData = array(
!empty($extra) ? $extra . “nn” : ” . $e->getMessage(),
self::detailedExceptionDump($e)
);
$reportData = serialize($reportData);
file_put_contents($reportFile, $reportData);
chmod($reportFile, 0777);
$storeCode = ‘default’;
try {
$storeCode = self::app()->getStore()->getCode();
}
catch (Exception $e) {}
$baseUrl = rtrim(str_replace(”, ‘/’, dirname($_SERVER[‘SCRIPT_NAME’])), ‘/’);
$reportUrl = $baseUrl . ‘/report/?id=’
. $reportId . ‘&s=’ . $storeCode;
if (!headers_sent()) {
header(‘Location: ‘ . $reportUrl);
}
else {
print ‘<script type=”text/javascript”>’;
print “window.location.href = ‘{$reportUrl}’;”;
print ‘</script>’;
}
}
die();
}
public static function detailedExceptionDump(Exception $e, $mode=’html’) {
$exceptionDumpHeader = “nnDETAILED EXCEPTION DUMPnn”;
$exceptionDumpHeader .= “Class: ” . get_class($e) . “nn”;
$exceptionDumpHeader .= “Message: ” . $e->getMessage();
$exceptionDump=$exceptionDumpHeader;
$vObject=new Varien_Object;
foreach ($e->getTrace() as $n => $t) {
if(ob_start()){
//recursive magento debug function, recursion killer
var_dump($vObject->debug($t));
$dump = ob_get_clean();
}else{
$dump = ‘ob_start() failed in ‘ . __METHOD__;
}
if(‘html’!=$mode){
$dump = html_entity_decode(strip_tags($dump));
}
$exceptionDump .= “nn————n#$nn$dumpn”;
}
$file=’detailed_exception.log’;
self::log(html_entity_decode(strip_tags($exceptionDump)), null, $file);
$length=strlen($exceptionDump);
if($length > 1000000){
$exceptionDump = $exceptionDumpHeader . “nnOVERSIZE EXCEPTION: $lengthnnHas been logged to $file nn” . $e->getTraceAsString();
}
return $exceptionDump;
}
And you’re done! Next time you get an exception, it should be the detailed version.
This class will also print exceptions to a detailed_exception.log file in
{magento_root}/var/log/
Need help moving or migrating from Magento Professional or Magento Enterprise to Magento’s free community version? We can help! Call Customer Paradigm’s team of Magento Developers – 303.473.4400 or visit here: http://www.CustomerParadigm.com/Magento