From ffa29d1bc210fb076f7538c63c8ee83b6b3a43fa Mon Sep 17 00:00:00 2001 From: Jean-Tiare Le Bigot Date: Mon, 27 Jun 2016 10:24:47 +0200 Subject: [PATCH] documentation: catching an exception Signed-off-by: Jean-Tiare Le Bigot --- README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/README.md b/README.md index 0ca929b..9b82378 100644 --- a/README.md +++ b/README.md @@ -162,8 +162,42 @@ $webHosting = $conn->get('/hosting/web/'); foreach ($webHosting as $webHosting) { echo "One of our web hosting: " . $webHosting . "\n"; +} +?> ``` +How to print API error details? +------------------------------- + +Under the hood, ```php-ovh``` uses [GuzzlePHP 6](http://docs.guzzlephp.org/en/latest/quickstart.html) by default to issue API requests. If everything goes well, it will return the response directly as shown in the examples above. If there is an error like a missing endpoint or object (404), an authentication or authorization error (401 or 403) or a parameter error, the Guzzle will raise a ``GuzzleHttp\Exception\ClientException`` exception. For server-side errors (5xx), it will raise a ``GuzzleHttp\Exception\ServerException`` exception. + +You can get the error details with a code like: + +```php +get('/me')['firstname']; +} catch (GuzzleHttp\Exception\ClientException $e) { + $response = $e->getResponse(); + $responseBodyAsString = $response->getBody()->getContents(); + echo $responseBodyAsString; +} +?> +``` + + How to build the documentation? -------------------------------