Ayrıca ajax istekleri kullanırken (benim durumumda jquery mobile) özel istisnalar ve hata kodları ile uğraştım. İşte, hata ayıklama modunun üzerine yazmadan dahil olduğum çözüm. Geliştirme modunda ve ayrıca isteğe bağlı olarak üretim modunda özel hatalar atar.
AppExceptionRenderer.php: Eğer hata ayıklama modunda istisna görüntülemek istiyorsanız
<?php
App::uses('ExceptionRenderer', 'Error');
class AppExceptionRenderer extends ExceptionRenderer
{
public function test($error)
{
$this->_sendAjaxError($error);
}
private function _sendAjaxError($error)
{
//only allow ajax requests and only send response if debug is on
if ($this->controller->request->is('ajax') && Configure::read('debug') > 0)
{
$this->controller->response->statusCode(500);
$response['errorCode'] = $error->getCode();
$response['errorMessage'] = $error->getMessage();
$this->controller->set(compact('response'));
$this->controller->layout = false;
$this->_outputMessage('errorjson');
}
}
}
Sen Configure::read('debug') > 0
dışarı bırakabilir bunu birileri yardımcı olur. errorjson.ctp görünümü 'Error/errorjson'da bulunur.ctp ': Bu durumda
<?php
echo json_encode($response);
?>
benim istisna denir
TestException
ve olarak tanımlanır aşağıdaki gibidir: Ben özel bir hata kodu var
<?php
class TestException extends CakeException {
protected $_messageTemplate = 'Seems that %s is missing.';
public function __construct($message = null, $code = 2) {
if (empty($message)) {
$message = 'My custom exception.';
}
parent::__construct($message, $code);
}
}
Benim json cevabı için 2, $code = 2
. ajax yanıtı şu json verilerle bir hata 500 kullanacaklar:
{"errorCode":"2","errorMessage":"My custom exception."}
Açıkçası, ayrıca denetleyicisinden istisna gerekir:
throw new TestException();
ve istisna renderer dahil http://book.cakephp.org/2.0/en/development/exceptions.html#using-a-custom-renderer-with-exception-renderer-to-handle-application-exceptions
Bu biraz kapsam dışı olabilir, ancak JQuery'de ajax hata yanıtını işlemek için kullanıyorum:
$(document).ajaxError(function (event, jqXHR, ajaxSettings, thrownError) {
//deal with my json error
});
Bu, hata ayıklama modunda olmasını içerdiğinden umuyordum, bununla ilgili hiçbir fikrim yok gibi göründüğü için bunu doğru olarak işaretleyeceğim. Çaba için teşekkürler! –