【问题标题】:How to catch 500 error in Controller using Laravel如何使用 Laravel 在控制器中捕获 500 错误
【发布时间】:2019-03-30 05:34:57
【问题描述】:

我需要连接一个 API,所以我写了一个函数:

try {
    $res4 = $client3->post('https://api.example.co.uk/Book', [
        'headers' => [
            'Accept' => 'application/json',
            'Content-Type' => 'application/json',
            'Authorization' => 'Bearer ajhsdbjhasdbasdbasd',
        ],
        'json' => [
            'custFirstName' => $FirstName,
            'custLastName' => $Surname,
            'custPhone' => $Mobile,
            'custEmail' => $Email,
        ]
    ]);
} catch (GuzzleHttp\Exception\ClientException $e) {
    $response = $e->getResponse();
    $result = json_decode($response->getBody()->getContents());
    $item->update(['status' => 'Problems at step3']);
    Mail::raw('Problem at STEP 3', function ($message) use ($serial) {
        $message->from('asd.asd@gmail.com', 'asd.asd@gmail.com');
        $message->subject('We got a problem etc.');
        $message->to('john.smith@gmail.com');
    });
}

如您所见,我需要调用 API,但在 API 关闭的情况下,我会编写 catch 函数。

但是现在当 API 关闭并且 API 返回“500 内部错误”时,这个函数就崩溃了......

我的问题是为什么 catch 不处理它?

我如何处理错误 - 当 API 关闭或请求错误时...为什么 catch{} 不起作用?

更新:这是我的 laravel.log

[2018-10-25 14:51:04] local.ERROR: GuzzleHttp\Exception\ServerException: Server error: `POST https://api.example.co.uk/Book` resulted in a `500 Internal Server Error` response:
{"message":"An error has occured. Please contact support."}
 in /home/public_html/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:107
Stack trace:
#0 /home/public_html/vendor/guzzlehttp/guzzle/src/Middleware.php(65): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response))
#1 /home/public_html/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response))

【问题讨论】:

  • 请查看文件夹/storage/log/laravel.log中的laravel日志
  • 它可能不是ClientException 而是捕获根异常Exception
  • 检查您的日志并在此处发布完整的错误。
  • 500内部错误不是顺便抛出的异常。
  • 从 API 我收到错误:RequestException.php 第 107 行中的 ServerException:服务器错误:POST https://api.exampe.co.uk/Book 导致 500 Internal Server Error 响应:{“消息”:“发生错误。请联系支持."}

标签: php laravel error-handling guzzle guzzle6


【解决方案1】:

这里的问题是命名空间,而不是:

} catch (GuzzleHttp\Exception\ClientException $e) {

你应该使用:

} catch (\GuzzleHttp\Exception\ClientException $e) {

否则 PHP 假定该类在当前命名空间中,所以实际上当您使用 GuzzleHttp\Exception\ClientException 时,实际上您可能使用了 App\Http\Controllers\GuzzleHttp\Exception\ClientException,而 Guzzle 显然不会抛出此类异常。

【讨论】:

  • 不,这不是解决方案,因为我又遇到了同样的错误。我想要的只是从 API 捕获错误并更新 $item,还将 ALERT 电子邮件发送到某个电子邮件地址
  • @AleksPer 哦,好吧,所以在错误日志中你有GuzzleHttp\Exception\ServerException,所以你可以用这个替换。但是,由于您可能有不同的异常,最好用} catch (\Exception $e) { 替换以真正捕获此处可能发生的任何异常。
  • catch (\Exception $e) { 将捕获 API 上的任何错误?我试过了,现在看来效果很好
  • @AleksPer 是的,它应该捕获出现的任何异常。但是,如果您使用的是 PHP 7,更好的是使用 \Throwable 以防万一 - 请参阅 php.net/manual/en/class.throwable.php
  • 这对我来说适用于第 3 方库的例外情况。
【解决方案2】:

触发的异常是 ServerException 实例,catch 块试图捕获 ClientException。

} catch (GuzzleHttp\Exception\ServerException $e) {

【讨论】:

    【解决方案3】:

    在你的 app/exceptions/handler.php 文件中,像这样更新渲染方法。

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception) {
        if ($exception instanceof \GuzzleHttp\Exception\ClientException) {
            return your_response();
        }
        return parent::render($request, $exception);
    }
    

    这种方法对我有用。

    【讨论】:

      猜你喜欢
      • 2018-09-25
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 2012-11-18
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多