Skip to content

Commit bbdc328

Browse files
author
Denis Korolev
committed
fix ответа с ошибкой. Вместо массива приходит иногда текст
1 parent e5e3cfe commit bbdc328

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

src/AuthApi.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public function auth(AuthRequest $request): AuthResponse
2626
);
2727

2828
$response = $this->send($req);
29-
29+
$data = $this->prepareResponse($response);
3030
/** @var AuthResponse $body */
31-
$body = $this->serializer->deserialize($response->getBody()->getContents(), AuthResponse::class, 'json');
31+
$body = $this->serializer->deserialize($data, AuthResponse::class, 'json');
3232
$body->statusCode = $response->getStatusCode();
3333

3434
return $body;
@@ -47,9 +47,9 @@ public function logOut(LogOutRequest $request): LogOutResponse
4747
);
4848

4949
$response = $this->send($req);
50-
50+
$data = $this->prepareResponse($response);
5151
/** @var LogOutResponse $body */
52-
$body = $this->serializer->deserialize($response->getBody()->getContents(), LogOutResponse::class, 'json');
52+
$body = $this->serializer->deserialize($data, LogOutResponse::class, 'json');
5353
$body->statusCode = $response->getStatusCode();
5454

5555
return $body;

src/BaseClient.php

+17
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,21 @@ protected function send(RequestInterface $request): ResponseInterface
5151

5252
return $response;
5353
}
54+
55+
protected function prepareResponse(ResponseInterface $response): string
56+
{
57+
if ($response->getStatusCode() > 300) {
58+
$array = json_decode($response->getBody()->getContents(), true);
59+
60+
if (isset($array['error']) && is_string($array['error'])) {
61+
$error = $array['error'];
62+
$array['error'] = ['message' => $error];
63+
}
64+
$data = json_encode($array, JSON_UNESCAPED_UNICODE);
65+
} else {
66+
$data = $response->getBody()->getContents();
67+
}
68+
69+
return $data;
70+
}
5471
}

src/ContractorApi.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function getInfo(GetInfoRequest $request): GetInfoResponse
2424
);
2525

2626
$response = $this->send($req);
27-
$data = $response->getBody()->getContents();
27+
$data = $this->prepareResponse($response);
2828

2929
if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300 && $request->params->memberParam->extendedFields) {
3030
$array = json_decode($data, true);

src/DocumentApi.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ public function sendDocument(SendDocumentRequest $request): SendDocumentResponse
2828
);
2929

3030
$response = $this->send($req);
31-
$data = $response->getBody()->getContents();
32-
31+
$data = $this->prepareResponse($response);
3332
/** @var SendDocumentResponse $body */
3433
$body = $this->serializer->deserialize($data, SendDocumentResponse::class, 'json');
3534
$body->statusCode = $response->getStatusCode();
@@ -50,7 +49,7 @@ public function makeAction(MakeActionRequest $request): MakeActionResponse
5049
);
5150

5251
$response = $this->send($req);
53-
$data = $response->getBody()->getContents();
52+
$data = $this->prepareResponse($response);
5453

5554
/** @var MakeActionResponse $body */
5655
$body = $this->serializer->deserialize($data, MakeActionResponse::class, 'json');
@@ -72,7 +71,7 @@ public function listOfChanges(ListOfChangesRequest $listOfChangesRequest): ListO
7271
);
7372

7473
$response = $this->send($req);
75-
$data = $response->getBody()->getContents();
74+
$data = $this->prepareResponse($response);
7675

7776
/** @var ListOfChangesResponse $body */
7877
$body = $this->serializer->deserialize($data, ListOfChangesResponse::class, 'json');

0 commit comments

Comments
 (0)