title: How to catch all exceptions that could arise in Symfony2 controller action?
body: <p>I am trying to build an REST web service that returns JSON for all calls. Normally this is pretty straightforward, I just do like this:</p>
  
  <pre class="lang-php prettyprint-override"><code>return new Response(json_encode($return_object));
  </code></pre>
  
  <p>My question is, how should I intercept exceptions in a global way? I want to do this, because if an exception happens anywhere in my application, I'd still like to return a JSON message to the client saying basically "Yo dawg, I heard you like exceptions". My thinking is that returning JSON in both success and failure cases will simplify the work that any client needs to do to implement my API.</p>
  
  <p>So far, the only thing I can think of is to write every controller action like this:</p>
  
  <pre class="lang-php prettyprint-override"><code>public function generateMemeAction($arg1, $arg2) {
    $return_object = array();
    try {
      // stuff to generate meme here
      $return_object['status'] = "GREAT SUCCESS!";
    } catch (Exception $e) {
      // epic fail
      $return_object['status'] = "UnluckyBrianException";
    }
    return new Response(json_encode($return_object));
  }
  </code></pre>
  
  <p>Which is all great and wonderful, but that try-catch block will be the same for every action in my app, and I will feel silly every time I have to edit around a bunch of copy pasta. Pro tips?</p>