0

I am PUTing to a restful blog API. There is simple error checking in the API. If the entry_name or entry_body is less than 8 characters, he response is as follows:

{
  "status":"failure",
 "message":{
        "entry_name":"The entry_name field must be at least 8 characters in length.",
        "entry_body": The entry_body field must be at least 8 characters in length." 
        }
}

In my web page I get this:

Type: GuzzleHttp\Exception\ClientException

Message: Client error: `PUT https://www.example.com/api/v1/Blog/blog`   
resulted in a `400 Bad Request` response: {"status":"failure","message":
{"entry_name":"The entry_name field must be at least 8 characters in 
length.","entry_body" (truncated...)

I don't understand how I can catch the exception prior to guzzle spilling out the error like above.

I want to test for failure and if failure i want to display the message(s).

This is the code I have to catch exceptions:

This is my code:

         try {
              $response = $client->request('PUT', $theUrl);
              $theBody = $response->getBody();
        } catch (RequestException $e) {
            echo $e;
        }   

but it sails right past the above block :-(

spreaderman
  • 918
  • 2
  • 10
  • 39
  • try-catch -> http://php.net/manual/en/language.exceptions.php – Federkun Nov 13 '16 at 09:07
  • Just pasted a bit of my code which I am using but it doesnt see to catch the exception. – spreaderman Nov 13 '16 at 09:09
  • is `use GuzzleHttp\Exception\RequestException;` present? – Federkun Nov 13 '16 at 09:13
  • I see in composer only this: "require-dev": { "guzzlehttp/guzzle": "~5.0", "mockery/mockery": "~0.8", "phpunit/phpunit": "~4.0" }, "suggest": { "guzzlehttp/guzzle": "Allows for implementation of the Guzzle HTTP client" }, Is GuzzleHttp\Exception\RequestException; not in above? – spreaderman Nov 13 '16 at 09:23
  • let me rephrase: did you _import_ the `RequestException` class from the correct _namespace_? – Federkun Nov 13 '16 at 09:27
  • that must be the problem then but that is a but beyond me. I tried " use GuzzleHttp\Exception\RequestException; but get an error as follows: Parse error: syntax error, unexpected 'use' (truncated) – spreaderman Nov 13 '16 at 09:39
  • put it after ` – Federkun Nov 13 '16 at 09:42
  • it is well inside php – spreaderman Nov 13 '16 at 09:44
  • put it _immediately_ after ` – Federkun Nov 13 '16 at 09:45
  • Thanks. No parse errors or other errors by the script doesn't catch the error. – spreaderman Nov 13 '16 at 09:55
  • Hi! Does this help at all? http://stackoverflow.com/questions/19748105/handle-guzzle-exception-and-get-http-body I don't see that RequestException is a catchable exception in Guzzle 6. Can you use GuzzleHttp\Exception\ClientException? – tjfo Nov 14 '16 at 14:32

1 Answers1

2

If you don't want Guzzle 6 to throw exceptions for 4xx and 5xx at all, you need to create a handler stack WITHOUT the http_errors middleware which is added to the stack by default:

$handlerStack = new \GuzzleHttp\HandlerStack(\GuzzleHttp\choose_handler());

$handlerStack->push(\GuzzleHttp\Middleware::redirect(), 'allow_redirects');
$handlerStack->push(\GuzzleHttp\Middleware::cookies(), 'cookies');
$handlerStack->push(\GuzzleHttp\Middleware::prepareBody(), 'prepare_body');

$config = ['handler' => $handlerStack]);

$client = new \GuzzleHttp\Client($config);
mark.sagikazar
  • 1,032
  • 8
  • 19