master

laravel/framework

Last updated at: 29/12/2023 09:23

RequestException.php

TLDR

This file defines the RequestException class in the Illuminate\Http\Client namespace, which is a subclass of HttpClientException. The class represents an exception that is thrown when an HTTP request fails.

Classes

RequestException

The RequestException class extends the HttpClientException class and represents an exception that is thrown when an HTTP request fails. It has the following properties and methods:

Properties

  • $response: The response instance of type Illuminate\Http\Client\Response.

Methods

  • __construct($response): A constructor that creates a new instance of the exception. It takes a Response object as a parameter and sets the $response property of the class.
  • prepareMessage($response): A protected method that prepares the exception message. It takes a Response object as a parameter and returns a string containing the HTTP status code and a summary of the response body.
<?php

namespace Illuminate\Http\Client;

use GuzzleHttp\Psr7\Message;

class RequestException extends HttpClientException
{
    /**
     * The response instance.
     *
     * @var \Illuminate\Http\Client\Response
     */
    public $response;

    /**
     * Create a new exception instance.
     *
     * @param  \Illuminate\Http\Client\Response  $response
     * @return void
     */
    public function __construct(Response $response)
    {
        parent::__construct($this->prepareMessage($response), $response->status());

        $this->response = $response;
    }

    /**
     * Prepare the exception message.
     *
     * @param  \Illuminate\Http\Client\Response  $response
     * @return string
     */
    protected function prepareMessage(Response $response)
    {
        $message = "HTTP request returned status code {$response->status()}";

        $summary = Message::bodySummary($response->toPsrResponse());

        return is_null($summary) ? $message : $message .= ":\n{$summary}\n";
    }
}