public
Last active

Display AJAX errors on Zend Framework

  • Download Gist
ErrorController.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
<?php
 
class ErrorController extends Zend_Controller_Action
{
public function errorAction()
{
/** Other suff */
 
if ( true === $this->getRequest()->isXmlHttpRequest() )
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
 
$return = array();
$return["error"]["exception"]["title"] = $this->view->message;
$return["error"]["exception"]["message"] = $errors->exception->getMessage();
$return["error"]["exception"]["trace"] = $errors->exception->getTraceAsString();
$return["error"]["request"] = ( string ) var_export( $errors->request->getParams(), true );
$this->getResponse()->setHeader( "Exception", Zend_Json::encode( $return ) )
->sendResponse();
return;
}
 
$this->view->exception = $errors->exception;
$this->view->request = $errors->request;
}
}
layout.phtml
HTML+PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
$.ajaxSetup( {
"error": function ( XMLHttpRequest, textStatus, errorThrown ) {
// $.unblockUI();
// alert( "ERROR: " + XMLHttpRequest.statusText + " -- " + textStatus + " -- " + errorThrown );
var exception = eval( "(" + XMLHttpRequest.getResponseHeader( "Exception" ) + ")" );
 
$( "#exceptionTitle" ).html( exception.error.exception.title );
$( "#exceptionMessage" ).html( exception.error.exception.message );
 
// IE doesn't preserves leading whitespace when innerHTML is used.
if ( jQuery.support.leadingWhitespace )
{
$( "#exceptionTrace" ).html( exception.error.exception.trace );
$( "#exceptionParams" ).html( exception.error.request );
}
else // IE, lovelly :}
{
$( "#exceptionTrace" ).hide().after( "<pre>" + exception.error.exception.trace + "</pre>" );
$( "#exceptionParams" ).hide().after( "<pre>" + exception.error.request + "</pre>" );
}
 
$.blockUI( {
"message": $( "#exceptionContainer" ),
"css": {
"border": "3px solid #004898",
"padding": "5px",
"top": ( $( window ).height() - 500 ) / 2 + "px",
"left": ( $( window ).width() - 500 ) / 2 + "px",
"width": "500px"
}
} );
 
$( ".blockOverlay" ).attr( "title", "Haga clic ac√° para desbloquear esta pantalla" )
.click( $.unblockUI );
}
} );
</script>
</head>
<body>
<div id="exceptionContainer" style="display: none; width: 100%; overflow: auto; text-align: left;">
<h1 class="errors">Se ha producido un error</h1>
<h2 id="exceptionTitle"></h2>
<?php if ( "development" == APPLICATION_ENV ): ?>
<h3>Información del error:</h3>
<h3 class="errors">Traza:</h3>
<p><b>Mensaje:</b> <span id="exceptionMessage"></span></p>
<div style="overflow: auto; width: 400px; height: 100px;">
<pre id="exceptionTrace"></pre>
</div>
<h3 class="errors">Parametros enviados:</h3>
<div style="overflow: auto; width: 400px; height: 100px;">
<pre id="exceptionParams"></pre>
</div>
<?php endif ?>
</div>
</body>
</html>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.