title: Laravel api class exception handling
body: <p>I have a Laravel 4.2 application with the following route:</p>
  
  <pre><code>Route::group(['prefix' =&gt; 'api/v1'], function()
  {
      Route::resource('service_logs', 'ServiceLogsController', [
          'only' =&gt; ['index', 'store', 'show']
      ]);
  });
  </code></pre>
  
  <p>The <code>ServiceLogsController</code> controller extends from <code>ApiController</code>, which looks something like this <em>cut down version</em>:</p>
  
  <pre><code>class ApiController extends \BaseController {
  
      protected $statusCode = 200;
  
      public function getStatusCode()
      {
          return $this-&gt;statusCode;
      }
  
      public function setStatusCode($statusCode)
      {
          $this-&gt;statusCode = $statusCode;
  
          return $this;
      }
  
      public function respondInternalError($message = 'Internal Error!')
      {
          return $this-&gt;setStatusCode(500)-&gt;respondWithError($message);
      }
  
      public function respondWithError($message)
      {
          return Response::json([
              'error' =&gt; [
                  'message' =&gt; $message,
                  'status_code' =&gt; $this-&gt;getStatusCode()
              ]
          ], $this-&gt;getStatusCode());
      }
  
      // ...
  
  }
  </code></pre>
  
  <p>What I'd like to do is, when ever an un-caught exception occurs, I want to call the <code>respondInternalError()</code> method on my <code>ApiController</code>, so that the API consumer has a consistent response rather than nothing or html <code>whoops</code> error.</p>
  
  <p>To achieve this, I tried adding the following code in my <code>app/start/global.php</code> </p>
  
  <pre><code>App::error(function(Exception $exception, $code)
  {
      Log::error($exception);
  
      if (Request::is('api/*'))
      {
          App::make('ApiController')-&gt;respondInternalError('Uncaught api exception error occurred - ' . $exception-&gt;getMessage());
      }
  });
  </code></pre>
  
  <p>and to test it, I tried to make a POST request to the following url: /api/v1/service_logs/123.</p>
  
  <p>this will not work, because that URL is a GET url, so Laravel throws the correct <code>method not allowed exception</code>. However, it's not getting caught.</p>
  
  <p>Any idea how to implement a catch all exception handler per controller class basis?</p>
  
  <hr>
  
  <p><strong>Update</strong> <em>Slightly improved working "global" api exception handler</em></p>
  
  <pre><code>App::error(function(Exception $exception, $code)
  {
      Log::error($exception);
  
      if (Request::is('api/*'))
      {
          $errorName = Symfony\Component\HttpFoundation\Response::$statusTexts[$code];
  
          return App::make('ApiController')
              -&gt;setStatusCode($code)
              -&gt;respondWithError($errorName . ' error has occurred.');
      }
  });
  </code></pre>
  
  <p>When an error occurs, you get this now (testing done in Chrome + Postman):</p>
  
  <p><img src="https://i.stack.imgur.com/9agGl.png" alt="enter image description here"></p>