4 回答

TA貢獻(xiàn)1776條經(jīng)驗(yàn) 獲得超12個(gè)贊
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貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊
在您的 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貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以在app/Http/Middlewares
文件夾中創(chuàng)建自定義中間件,擴(kuò)展基\Illuminate\Routing\Middleware\ThrottleRequests
類并覆蓋buildException
方法(此處為原始實(shí)現(xiàn))。
然后將throttle
中間件分配給您的自定義中間件類Kernel.php

TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊
我在 Laravel 的這個(gè)地址找到了它的文件: 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 關(guān)注
- 189 瀏覽
添加回答
舉報(bào)