Take a look at http://symfony.com/doc/current/cookbook/controller/error_pages.html There is enough information to get you started.
In short, you should create app/Resources/TwigBundle/views/Exception/exception.json.twig and there you have access to the exception.message and error_code.
here's solution for you:
{% spaceless %}
{
"error":{
"code": {{ error_code }},
"message":{{ exception.message }}
}
}
{% endspaceless %}
Another solution is to use Exception Listener:
namespace Your\Namespace;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
class JsonExceptionListener
{
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$data = array(
'error' => array(
'code' => $exception->getCode(),
'message' => $exception->getMessage()
)
);
$response = new JsonResponse($data, $exception->getCode());
$event->setResponse($response);
}
}
update your services config:
json_exception_listener:
class: Your\Namespace\JsonExceptionListener
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException, priority: 200 }
cheers