RestApiClient: handle non-json errors

This commit is contained in:
Thomas Gelf 2015-09-29 19:01:25 +02:00
parent a0c418a952
commit 9ea08647dc
1 changed files with 10 additions and 3 deletions

View File

@ -2,6 +2,8 @@
namespace Icinga\Module\Director\Core;
use Exception;
class RestApiClient
{
protected $version = 'v1';
@ -59,7 +61,8 @@ class RestApiClient
'user_agent' => 'Icinga Web 2.0 - Director',
'method' => strtoupper($method),
'content' => $body,
'header' => $headers
'header' => $headers,
'ignore_errors' => true
),
'ssl' => array(
'verify_peer' => false,
@ -70,10 +73,14 @@ class RestApiClient
);
$context = stream_context_create($opts);
$res = file_get_contents($this->url($url), false, $context);
if (substr(array_shift($http_response_header), 0, 10) !== 'HTTP/1.1 2') {
throw new Exception($res);
}
if ($raw) {
return file_get_contents($this->url($url), false, $context);
return $res;
} else {
return RestApiResponse::fromJsonResult(file_get_contents($this->url($url), false, $context));
return RestApiResponse::fromJsonResult($res);
}
}