4 回答
TA贡献1776条经验 获得超12个赞
use Symfony\Component\HttpKernel\Exception\HttpException;
if($exception instanceof HttpException && $exception->getStatusCode() == 429) {
return response()->json([
'message' => 'Too Many Attempts',
'code' => 429
], 429)->withHeaders($exception->getHeaders());
}
TA贡献1827条经验 获得超4个赞
在您的 Laravel 异常处理程序中,您可以在渲染之前处理该异常并将该异常替换为您的自定义异常。
在app/Exceptions/Handler.php
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
*/
public function render($request, Exception $exception)
{
if($exception instanceof ThrottleRequestsException) {
return parent::render(
$request, new ThrottleRequestsException(
'Your message',
$exception->getPrevious(),
$exception->getHeaders(),
$exception->getCode()
)
);
}
return parent::render($request, $exception);
}
TA贡献1845条经验 获得超8个赞
我在 Laravel 的这个地址找到了它的文件: vendor\laravel\framework\src\llluminate\Routing\Middleware\ThrottleRequests.php
protected function buildException($key, $maxAttempts)
{
$retryAfter = $this->getTimeUntilNextRetry($key);
$headers = $this->getHeaders(
$maxAttempts,
$this->calculateRemainingAttempts($key, $maxAttempts, $retryAfter),
$retryAfter
);
return new ThrottleRequestsException(
'You can change message here and put your message!', null, $headers
);
}
- 4 回答
- 0 关注
- 147 浏览
添加回答
举报