title: How to catch "too many attempt" exception in a middleware Laravel 5
body: <p>I am building my API and I successfuly managed to catch some errors on a middleware I set up around my routes like following :</p>
  
  <pre><code>Route::group(['middleware' =&gt; \App\Http\Middleware\ExceptionHandlerMiddleware::class], function() {
  
      Route::resource('/address', 'AddressController');
  
      Route::resource('/country', 'CountryController');
  
      Route::resource('/phone', 'PhoneController');
  
      Route::resource('/user', 'UserController');
  });
  </code></pre>
  
  <p>The middleware manage to catch the following exceptions :</p>
  
  <ul>
  <li><code>Illuminate\Database\Eloquent\ModelNotFoundException</code></li>
  <li><code>Illuminate\Validation\ValidationException</code></li>
  <li><code>Exception</code></li>
  </ul>
  
  <p>Which is great. I am also aware of a throttle mecanism that control the number of attempt in a route. So with postman I attacked my route <code>http://localhost:8000/api/user</code> until I get the <code>too many attemp</code> error.</p>
  
  <p>The exception is throwed in the file located at :</p>
  
  <pre><code>/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php
  </code></pre>
  
  <p>And I also managed to get the type of exception it throws thanks to this <a href="https://laracasts.com/discuss/channels/general-discussion/how-to-use-laravel-throttle" rel="nofollow noreferrer">forum topic</a> : <code>Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException</code>.</p>
  
  <p>So in the end my middleware looks like this :</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Middleware;
  
  use Closure;
  use Illuminate\Database\Eloquent\ModelNotFoundException;
  use Illuminate\Validation\ValidationException;
  use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
  use Exception;
  
  class ExceptionHandlerMiddleware
  {
      public function handle($request, Closure $next)
      {
          $output = $next($request);
  
          try {
              if( ! is_null( $output-&gt;exception ) ) {
                  throw new $output-&gt;exception;
              }
  
              return $output;
          }
          catch( TooManyRequestsHttpException $e ) {
              return response()-&gt;json('this string is never showed up', 429);
          }
          catch( ValidationException $e ) {           
              return response()-&gt;json('validation error' 400);
          }
          catch( ModelNotFoundException $e ) {            
              return response()-&gt;json('not found', 404);
          }
          catch( \Exception $e ) {            
              return response()-&gt;json('unknow', 500);
          }
      }
  }
  </code></pre>
  
  <p>You see the line <code>this string is never showed up</code> ? In fact it is never showed up, the original throttle exception from Illuminate always take the front.</p>
  
  <p><strong>QUESTION</strong></p>
  
  <p>How can I properly override the base error in a way that I could possibly (if possible) catch any exception without having to modify the illuminate file (in case of updates...) ?</p>
  
  <p>Runing laravel 5.4.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I cannot afford manually updating <code>app/Http/Exception</code> files because my app will be shipped as a Service Provider for my futures others project. Also, I do not prefer taking the risk to erase some previous configuration on these files, as other "basic" routes in <code>routes.php</code> may have their own exception catching procedures.</p>