Laravel 9 HTTP 客户端异常处理

Але*_*нер 0 exception laravel

我正在尝试捕获 HTTP 客户端操作期间发生的错误。如果启用了调试APP_DEBUG=true,那么我会收到错误跟踪,如果关闭,则会出现 json response "message": "Server Error"。但我需要捕获异常,这不起作用。尝试过 catch (\Illuminate\Http\Client\ConnectionException $e),但没有成功。我究竟做错了什么?

  public function ExampleMethod()
    {
        try {
                
            $response =
                Http::withBasicAuth(env('REMOTE_LOGIN'), env('REMOTE_PASSWORD'))
                    ->accept('application/json')
                    ->retry(3, 2000)->timeout(12)
                    ->withBody("dummy body content", "application/json")
                    ->post($host . $url);

            if ($response->ok()) {
               //Do something
            }


        } catch (Exception $e) {
                
                dd("CATCH IT");
        }
    }
Run Code Online (Sandbox Code Playgroud)

文档中有一个示例,域不存在,异常处理程序应该在某处工作,但它不起作用

 public function catchExceptins()
    {
        try {

         $url = "domain-is-not-exist.com";

         $response = Http::get($url);



            if ($response->ok()) {

               dd("200 OK");
            }

            //
            if($response->failed()){
                  dd("FAILED");
            }

            //Below are the handlers that should work, 
            //but they do not respond when there is no domain 
            //or for example if the server response is 505

            if($response->serverError()) {
                 dd("FAILED");
            }


            if($response->clientError()) {
                 dd("FAILED");
            }


            $response->throw(function($response, $e){
                 dd("FAILED");
            })->json();

   

        } catch (Exception $e) {
               
                dd($e);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Don*_*ash 5

Laravel 的 HTTP 客户端包装器提供了一种通过许多有用的方法来处理错误的机制。

public function ExampleMethod()
{
    try{                
        $response = Http::withBasicAuth(env('REMOTE_LOGIN'), env('REMOTE_PASSWORD'))
            ->accept('application/json')
            ->retry(3, 2000)->timeout(12)
            ->withBody("dummy body content", "application/json")
            ->post($host . $url);

        //Check for any error 400 or 500 level status code
        if($response->failed()){
            // process the failure
        }

        //Check if response has error with 500 level status code
         if($response->serverError()) {
            //process on server error
        }

        //Check if response has error with 400 level status code
        if($response->clientError()) {
            //process on client error
        }

        // It also allows to throw exceptions on the $response
        //If there's no error then the chain will continue and json() will be invoked
        $response->throw(function($response, $e){
            //do your thing
        })->json();
    }
    catch(\Exception $e) {
        //$e->getMessage() - will output "cURL error 6: Could not resolve host" in case of invalid domain
    }
            
}
Run Code Online (Sandbox Code Playgroud)

Laravel 文档 - Http 客户端 - 异常处理