Home > Blockchain >  Laravel 9 HTTP client exception handling
Laravel 9 HTTP client exception handling

Time:06-07

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

CodePudding user response:

Laravel's HTTP client wrapper does not throw exceptions on client or server errors. However it has a mechanism for handling errors with a bunch of useful methods.

public function ExampleMethod()
{
                
    $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();
            
}

Laravel Docs - Http Client - Exception Handling

CodePudding user response:

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

  • Related