如何在中间件 Laravel 5 中捕获 "too many attempt" 异常
How to catch "too many attempt" exception in a middleware Laravel 5
我正在构建我的 API,我成功地在我围绕我的路由设置的中间件上捕获了一些错误,如下所示:
Route::group(['middleware' => \App\Http\Middleware\ExceptionHandlerMiddleware::class], function() {
Route::resource('/address', 'AddressController');
Route::resource('/country', 'CountryController');
Route::resource('/phone', 'PhoneController');
Route::resource('/user', 'UserController');
});
中间件设法捕获以下异常:
Illuminate\Database\Eloquent\ModelNotFoundException
Illuminate\Validation\ValidationException
Exception
太棒了。我也知道控制路线尝试次数的油门机制。所以我和邮递员一起攻击我的路线 http://localhost:8000/api/user
直到我得到 too many attemp
错误。
在位于 :
的文件中抛出异常
/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php
而且我还设法得到它抛出的异常类型,这要归功于 forum topic : Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException
.
所以最后我的中间件看起来像这样:
<?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->exception ) ) {
throw new $output->exception;
}
return $output;
}
catch( TooManyRequestsHttpException $e ) {
return response()->json('this string is never showed up', 429);
}
catch( ValidationException $e ) {
return response()->json('validation error' 400);
}
catch( ModelNotFoundException $e ) {
return response()->json('not found', 404);
}
catch( \Exception $e ) {
return response()->json('unknow', 500);
}
}
}
你看到行 this string is never showed up
了吗?事实上它从来没有出现过,原来来自 Illuminate 的 throttle exception 总是走在前面。
问题
我怎样才能以一种我可能(如果可能)捕获任何异常而不必修改 illuminate 文件(在更新的情况下...)的方式正确地覆盖基本错误?
运行laravel 5.4.
编辑
我负担不起手动更新 app/Http/Exception
文件,因为我的应用程序将作为我的未来其他项目的服务提供商提供。此外,我不喜欢冒险删除这些文件上的一些先前配置,因为 routes.php
中的其他 "basic" 路由可能有自己的异常捕获程序。
实现该目标的最佳方法是使用 app\Exceptions\Handler.php
public function render($request, Exception $exception)
{
if ($this->isHttpException($exception)) {
if (request()->expectsJson()) {
switch ($exception->getStatusCode()) {
case 404:
return response()->json(['message' => 'Invalid request or url.'], 404);
break;
case '500':
return response()->json(['message' => 'Server error. Please contact admin.'], 500);
break;
default:
return $this->renderHttpException($exception);
break;
}
}
} else if ($exception instanceof ModelNotFoundException) {
if (request()->expectsJson()) {
return response()->json(['message' =>$exception->getMessage()], 404);
}
} {
return parent::render($request, $exception);
}
return parent::render($request, $exception);
}
在此演示中,您可以添加更多异常 } else if ($exception instanceof ModelNotFoundException) {
并解决它们。
我正在构建我的 API,我成功地在我围绕我的路由设置的中间件上捕获了一些错误,如下所示:
Route::group(['middleware' => \App\Http\Middleware\ExceptionHandlerMiddleware::class], function() {
Route::resource('/address', 'AddressController');
Route::resource('/country', 'CountryController');
Route::resource('/phone', 'PhoneController');
Route::resource('/user', 'UserController');
});
中间件设法捕获以下异常:
Illuminate\Database\Eloquent\ModelNotFoundException
Illuminate\Validation\ValidationException
Exception
太棒了。我也知道控制路线尝试次数的油门机制。所以我和邮递员一起攻击我的路线 http://localhost:8000/api/user
直到我得到 too many attemp
错误。
在位于 :
的文件中抛出异常/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php
而且我还设法得到它抛出的异常类型,这要归功于 forum topic : Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException
.
所以最后我的中间件看起来像这样:
<?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->exception ) ) {
throw new $output->exception;
}
return $output;
}
catch( TooManyRequestsHttpException $e ) {
return response()->json('this string is never showed up', 429);
}
catch( ValidationException $e ) {
return response()->json('validation error' 400);
}
catch( ModelNotFoundException $e ) {
return response()->json('not found', 404);
}
catch( \Exception $e ) {
return response()->json('unknow', 500);
}
}
}
你看到行 this string is never showed up
了吗?事实上它从来没有出现过,原来来自 Illuminate 的 throttle exception 总是走在前面。
问题
我怎样才能以一种我可能(如果可能)捕获任何异常而不必修改 illuminate 文件(在更新的情况下...)的方式正确地覆盖基本错误?
运行laravel 5.4.
编辑
我负担不起手动更新 app/Http/Exception
文件,因为我的应用程序将作为我的未来其他项目的服务提供商提供。此外,我不喜欢冒险删除这些文件上的一些先前配置,因为 routes.php
中的其他 "basic" 路由可能有自己的异常捕获程序。
实现该目标的最佳方法是使用 app\Exceptions\Handler.php
public function render($request, Exception $exception)
{
if ($this->isHttpException($exception)) {
if (request()->expectsJson()) {
switch ($exception->getStatusCode()) {
case 404:
return response()->json(['message' => 'Invalid request or url.'], 404);
break;
case '500':
return response()->json(['message' => 'Server error. Please contact admin.'], 500);
break;
default:
return $this->renderHttpException($exception);
break;
}
}
} else if ($exception instanceof ModelNotFoundException) {
if (request()->expectsJson()) {
return response()->json(['message' =>$exception->getMessage()], 404);
}
} {
return parent::render($request, $exception);
}
return parent::render($request, $exception);
}
在此演示中,您可以添加更多异常 } else if ($exception instanceof ModelNotFoundException) {
并解决它们。