Skip to content

Instantly share code, notes, and snippets.

@nebiros
Created January 15, 2010 18:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nebiros/278292 to your computer and use it in GitHub Desktop.
Save nebiros/278292 to your computer and use it in GitHub Desktop.
Display AJAX errors on Zend Framework
<?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;
}
}
<!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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment