Using Kohana Remote::get throws exception on http 500, but I need the response text

174 views Asked by At

I'm making a Remote::get request to an API which answers with http 500 when something goes wrong. The thing is that it also gives {errorCode: x} as a more detailed description of what went wrong in the response text. On some of the error codes I need to take different actions.

My problem is that Kohana throws an exception on http 500 responses and thus bakes in my easy parsable response text in a "wordy" error message in the exception object.

Is there some way to get the response text of the Remote::get on a http 500 response without having to parse a lengthy error description?

1

There are 1 answers

0
Piotr Dawidiuk On

Impossible. Take a look at source code

if ($code AND $code < 200 OR $code > 299)
{
    $error = $response;
}

...

if (isset($error))
{
    throw new Kohana_Exception('Error fetching remote :url [ status :code ] :error',
            array(':url' => $url, ':code' => $code, ':error' => $error));
}

Kohana_Exception doesn't help much

public function __construct($message, array $variables = NULL, $code = 0)
{
    // Set the message
    $message = __($message, $variables);

    // Pass the message to the parent
    parent::__construct($message, $code);
}

So it mixes all things into one message.

How about using different HTTP client? For example Guzzle - it is easier to retrieve the body on error.