Ajax Call Exceptions Display Web Page Content in Javascript Console

Notice: This thread is very old.
dqj
Member | 11
+
0
-

My Nette app is an API for use by Angular, so all uses on the server side of my app are AJAX service calls. Unfortunately, if there is an error, a web page is sent to the ajax calling routine.

Is there a way to override the default handling and present the exception as json data in the output?

greeny
Member | 405
+
0
-

I think it shall be enough to modify your ErrorPresenter (which works the same way as normal presenters, except it is called only on error).

	public function run(Nette\Application\Request $request)
	{
		$exception = $request->getParameter('exception');
		if ($exception instanceof Nette\Application\BadRequestException) {
			return new Responses\ForwardResponse($request->setPresenterName('Error4xx'));
		}
		$this->logger->log($exception, ILogger::EXCEPTION);

		// this part is added
		if ($this->isAjax()) {
			$this->sendJson([
				"error" => $exception->getMessage(),
				"code" => $exception->getCode(),
				"type" => $exception::class,
			]);
		}
		// end of added part

		return new Responses\CallbackResponse(function () {
			require __DIR__ . '/templates/Error/500.phtml';
		});
	}

(based on Nette/Web-Project's Error presenter)

dqj
Member | 11
+
0
-

Thank you. I haven't been able to get the ErrorPresenter class to engage, somehow. It seems to work if I do not set enableDebugger(), but then it raises a whole new set of problems too difficult to untangle. It is going to be too much to take on until I understand Nette better.

Last edited by dqj (2015-10-18 18:41)

David Matějka
Moderator | 6445
+
0
-

In the debug mode, Nette does not use ErrorPresenter by default, but you can enable it to test its funcionality.

config.neon:

application:
	catchExceptions: true