Laravel - 捕获自定义中止消息
Laravel - catch the custom abort message
我正在使用中止函数从我的服务层发送自定义异常消息。
if($max_users < $client->users()->count()){
return abort(401, "Max User Limit Exceeded");
}
但是我如何在我的控制器中捕获这条消息,它在不同的应用程序中
try{
$clientRequest = $client->request(
'POST',
"api/saveClientDetails",
[
'headers' => [
'accept' => 'application/json',
'authorization' => 'Bearer ' . $user['tokens']->access_token
],
'form_params' => $data,
]
);
} catch ( \GuzzleHttp\Exception\ClientException $clientException ){
switch($clientException->getCode()){
case 401:
\Log::info($clientException->getCode());
\Log::info($clientException->getMessage());
abort(401);
break;
default:
abort(500);
break;
}
}
以上代码为消息打印以下内容:
但它打印
Client error: `POST http://project-service.dev/api/saveClientDetails` resulted in a `401 Unauthorized` response:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="robots" content="noindex,nofollow (truncated...)
好吧,当您应该捕获 Symfony HttpException 时,您正在尝试捕获 Guzzle 异常。也许尝试这样的事情:
catch(\Symfony\Component\HttpKernel\Exception\HttpException $e)
{
\Log::info($e->getMessage());
}
根据您的评论,我尝试了以下方法:
public function test()
{
try
{
$this->doAbort();
}
catch (\Symfony\Component\HttpKernel\Exception\HttpException $e)
{
dd($e->getStatusCode(), $e->getMessage());
}
}
public function doAbort()
{
abort(401, 'custom error message');
}
输出为:
401
"custom error message"
根据您的评论,这是我的工作方式。
Route::get('api', function() {
return response()->json([
'success' => false,
'message' => 'An error occured'
], 401);
});
Route::get('test', function() {
$client = new \GuzzleHttp\Client();
try
{
$client->request('GET', 'http://app.local/api');
}
catch (\Exception $e)
{
$response = $e->getResponse();
dd($response->getStatusCode(), (string) $response->getBody());
}
});
这将输出状态代码和正确的错误消息。如果您使用 abort
,它仍然会 return 完整的 HTML 响应。更好的方法是 return 格式良好的 JSON 响应。
让我知道它现在是否适合你:)
我正在使用中止函数从我的服务层发送自定义异常消息。
if($max_users < $client->users()->count()){
return abort(401, "Max User Limit Exceeded");
}
但是我如何在我的控制器中捕获这条消息,它在不同的应用程序中
try{
$clientRequest = $client->request(
'POST',
"api/saveClientDetails",
[
'headers' => [
'accept' => 'application/json',
'authorization' => 'Bearer ' . $user['tokens']->access_token
],
'form_params' => $data,
]
);
} catch ( \GuzzleHttp\Exception\ClientException $clientException ){
switch($clientException->getCode()){
case 401:
\Log::info($clientException->getCode());
\Log::info($clientException->getMessage());
abort(401);
break;
default:
abort(500);
break;
}
}
以上代码为消息打印以下内容:
但它打印
Client error: `POST http://project-service.dev/api/saveClientDetails` resulted in a `401 Unauthorized` response:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="robots" content="noindex,nofollow (truncated...)
好吧,当您应该捕获 Symfony HttpException 时,您正在尝试捕获 Guzzle 异常。也许尝试这样的事情:
catch(\Symfony\Component\HttpKernel\Exception\HttpException $e)
{
\Log::info($e->getMessage());
}
根据您的评论,我尝试了以下方法:
public function test()
{
try
{
$this->doAbort();
}
catch (\Symfony\Component\HttpKernel\Exception\HttpException $e)
{
dd($e->getStatusCode(), $e->getMessage());
}
}
public function doAbort()
{
abort(401, 'custom error message');
}
输出为:
401
"custom error message"
根据您的评论,这是我的工作方式。
Route::get('api', function() {
return response()->json([
'success' => false,
'message' => 'An error occured'
], 401);
});
Route::get('test', function() {
$client = new \GuzzleHttp\Client();
try
{
$client->request('GET', 'http://app.local/api');
}
catch (\Exception $e)
{
$response = $e->getResponse();
dd($response->getStatusCode(), (string) $response->getBody());
}
});
这将输出状态代码和正确的错误消息。如果您使用 abort
,它仍然会 return 完整的 HTML 响应。更好的方法是 return 格式良好的 JSON 响应。
让我知道它现在是否适合你:)