0

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);
        }
    }
2
  • have you tried to catch \Exception $e Commented Jun 8, 2022 at 2:42
  • 1
    @ChinhNguyen Thanks this works too! Perhaps I had problems with caching requests in the client or in Laravel, but your way always works. Commented Jun 8, 2022 at 21:00

2 Answers 2

5
+250

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for your help, I also read the documentation before posting the question. I don't understand Laravel very well and I'm doing something wrong. Updated my question. The request refers to a domain that does not exist and no exception handler is executed.
@АлександрИнженер If request is made to domain whch cannot be resolved, you should get a Illuminate\Http\Client\ConnectionException exception thrown automatically. Have updated answer
Perhaps I had problems with the Laravel cache or with the client through which I sent requests to Laravel (Insomnia http client), because essentially the same code did not work for me for several days and now everything works fine. Thank you.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.