Laravel 9 HTTP client exception handling

I'm trying to catch errors that occur during HTTP client operations. If debugging is enabled APP_DEBUG=true then I get an error trace, if it is off, then it comes json response "message": "Server Error". But I need to catch exceptions, it doesn't work. Tried catch (\Illuminate\Http\Client\ConnectionException $e), but it didn't work. What am I doing wrong?

  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");
        }
    }

There is an example from the documentation, the domain does not exist, and an exception handler should work somewhere, but it does not work

 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);
        }
    }

Upvotes: 0

Views: 5177

Answers (2)

Donkarnash
Donkarnash

Reputation: 12835

Laravel's HTTP client wrapper offers a mechanism for handling errors with a bunch of useful methods.

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
    }
            
}

Laravel Docs - Http Client - Exception Handling

Upvotes: 5

bmckay
bmckay

Reputation: 29

When you set APP_DEBUG=false, it just shows a generic error to the end user for security, but should give you the detailed error inside of the Laravel logs. 'All' APP_DEBUG=true does, is make the development process easier by displaying the log on the front end.

Your Laravel logs should be inside of "/storage/logs".

https://laravel.com/docs/9.x/configuration#debug-mode

https://laravel.com/docs/9.x/errors#configuration

Upvotes: 0

Related Questions