c51ee11c by fehrlich

invalid response improvements

1 parent 9b1a0216
......@@ -4,16 +4,15 @@ namespace fehrlich\ImmoScoutAPI;
use function GuzzleHttp\json_decode;
use function GuzzleHttp\json_encode;
use fehrlich\ScoutAPI\exceptions\AuthException;
use fehrlich\ScoutAPI\exceptions\InvalidResponse;
use fehrlich\ScoutAPI\exceptions\InvalidTokenException;
use fehrlich\ImmoScoutAPI\exceptions\AuthException;
use fehrlich\ImmoScoutAPI\exceptions\InvalidResponse;
use fehrlich\ImmoScoutAPI\exceptions\InvalidTokenException;
use GuzzleHttp\Client;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Subscriber\Oauth\Oauth1;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
/**
......@@ -377,7 +376,16 @@ abstract class ImmoScoutAPI extends GuzzleClient
if (isset($json['common.messages']) && is_array($json['common.messages'])) {
foreach ($json['common.messages'] as $message) {
if (isset($message['message']) && isset($message['message']['messageCode'])) {
if (isset($message['message']) && isset($message['message'][0])) {
foreach ($message['message'] as $msg) {
if (isset($msg['messageCode'])) {
if (!isset($return[$msg['messageCode']])) {
$return[$msg['messageCode']] = [];
}
$return[$msg['messageCode']][] = $msg['message'];
}
}
} elseif (isset($message['message']) && isset($message['message']['messageCode'])) {
if (!isset($return[$message['message']['messageCode']])) {
$return[$message['message']['messageCode']] = [];
}
......@@ -387,15 +395,23 @@ abstract class ImmoScoutAPI extends GuzzleClient
}
$parsedErrors = [];
foreach ($return as $errorCode => $msgArr) {
foreach ($msgArr as $msg) {
$add = null;
switch ($errorCode) {
case 'ERROR_RESOURCE_VALIDATION':
preg_match('/MESSAGE: (.*?) :/', $msg, $matches);
if (isset($matches[1])) {
$add = $matches[1];
} else {
preg_match('/MESSAGE: (.*?)\]/', $msg, $matches);
if (isset($matches[1])) {
$add = $matches[1];
if ($matches[1] == 'RealEstate already exists for external id.') {
$parsedErrors['ERROR_RESOURCE_DUPLICATE'] = true;
}
}
}
break;
case 'MESSAGE_RESOURCE_CREATED':
......@@ -480,7 +496,7 @@ abstract class ImmoScoutAPI extends GuzzleClient
$msgs = $this->parseMessages($res);
if (!isset($msgs['parsed'][$expectedResponse])) {
$code = isset($msgs['parsed']['ERROR_RESOURCE_NOT_FOUND']) ? 404 : null;
throw new InvalidResponse('Did not get expected response: '.$res->getBody(), $code);
throw new InvalidResponse('Did not get expected response: '.$res->getBody(), $code, null, $res, $msgs);
}
if ($expectedResponse == 'MESSAGE_RESOURCE_CREATED') {
return $msgs['parsed'][$expectedResponse][0];
......@@ -506,7 +522,11 @@ abstract class ImmoScoutAPI extends GuzzleClient
private function parseGetResponse($res)
{
return json_decode((string) $res->getBody(), true);
try {
return json_decode((string) $res->getBody(), true);
} catch (Exception $ex) {
throw new InvalidResponse('invalid json response');
}
}
/**
......
<?php
namespace fehrlich\ScoutAPI\exceptions;
namespace fehrlich\ImmoScoutAPI\exceptions;
use Exception;
class InvalidResponse extends Exception
{
private $response;
private $msgs;
public function __construct($msg, $code = 0, $prev = null, $res = null, $msgs = null)
{
parent::__construct($msg, $code, $prev);
$this->msgs = $msgs;
$this->response = $res;
}
public function getResponse()
{
return $this->response;
}
public function getMessages()
{
return $this->msgs;
}
}
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!