Skip to content

Commit 41d4f30

Browse files
author
Vítězslav Dvořák
committed
Enhances error handling and response tracking in RO class
Improves the management of success states, error messages, and operation tracking within the RO class. Adds properties for storing messages and operation names, ensuring clearer handling of API responses. Refines the success method to incorporate new success logic based on response data, enhancing overall robustness. Updates error parsing to better handle response structures, leading to more informative error reporting.
1 parent 4c78690 commit 41d4f30

2 files changed

Lines changed: 57 additions & 17 deletions

File tree

src/AbraFlexi/RO.php

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,14 @@ class RO extends \Ease\Sand implements \Stringable
323323
*/
324324
protected bool $ignoreNotFound = false;
325325

326+
/**
327+
* Action Messages.
328+
*
329+
* @var array<string>
330+
*/
331+
protected array $messages = [];
332+
protected ?bool $success = null;
333+
326334
/**
327335
* Array of errors caused by last request.
328336
*
@@ -337,6 +345,11 @@ class RO extends \Ease\Sand implements \Stringable
337345
*/
338346
protected ?array $responseStats = null;
339347

348+
/**
349+
* Performed Operation name.
350+
*/
351+
protected ?string $operation = null;
352+
340353
/**
341354
* List of Error500 reports sent.
342355
*
@@ -401,6 +414,10 @@ public function __toString()
401414
*/
402415
public function __unserialize(array $data): void
403416
{
417+
foreach ($data as $key => $value) {
418+
$this->setupProperty($data, $key);
419+
}
420+
404421
$this->curlInit();
405422
}
406423

@@ -997,11 +1014,13 @@ public function addDefaultUrlParams(string $urlRaw)
9971014
public function performRequest(
9981015
string $urlSuffix = '',
9991016
string $method = 'GET',
1000-
$format = null,
1017+
string $format = '',
10011018
) {
1019+
$this->success = null;
10021020
$this->rowCount = null;
10031021
$this->responseStats = [];
10041022
$this->errors = [];
1023+
$this->messages = [];
10051024

10061025
if (preg_match('/^http/', $urlSuffix)) {
10071026
$url = $urlSuffix;
@@ -1248,6 +1267,16 @@ public function parseResponse($responseDecoded, $responseCode)
12481267
{
12491268
$mainResult = null;
12501269

1270+
if (!empty($responseDecoded) && \is_array($responseDecoded) && \array_key_exists('success', $responseDecoded)) {
1271+
$this->operation = \array_key_exists('operation', $responseDecoded) ? $responseDecoded['operation'] : 'get';
1272+
1273+
$this->success = $responseDecoded['success'] === 'ok';
1274+
1275+
$this->messages = \array_key_exists('messages', $responseDecoded) && \is_array($responseDecoded['messages']) ? $responseDecoded['messages'] : [];
1276+
1277+
$this->parseError($responseDecoded);
1278+
}
1279+
12511280
switch ($responseCode) {
12521281
case 201: // We do not care about Success Write here
12531282
case 202: // Accept eg. unsent mails sent
@@ -1328,10 +1357,6 @@ public function parseResponse($responseDecoded, $responseCode)
13281357
// no break
13291358
case 400: // Bad Request parameters
13301359
default: // Something goes wrong
1331-
if (!empty($responseDecoded) && \is_array($responseDecoded)) {
1332-
$this->parseError($responseDecoded);
1333-
}
1334-
13351360
if ($this->throwException) {
13361361
$errors = $this->getErrors();
13371362

@@ -1354,7 +1379,9 @@ public function parseResponse($responseDecoded, $responseCode)
13541379
public function parseError(array $responseDecoded)
13551380
{
13561381
if (\array_key_exists('success', $responseDecoded)) {
1357-
$this->errors = [['message' => \array_key_exists('message', $responseDecoded) ? $responseDecoded['message'] : '']];
1382+
if (\array_key_exists('message', $responseDecoded) && \strlen($responseDecoded['message'])) {
1383+
$this->errors = [['message' => $responseDecoded['message']]];
1384+
}
13581385
} else {
13591386
$this->addStatusMessage('Unparsed error: '.$this->lastCurlResponse, 'error');
13601387
}
@@ -1373,9 +1400,9 @@ public function parseError(array $responseDecoded)
13731400
*
13741401
* @return int HTTP Response CODE
13751402
*/
1376-
public function doCurlRequest($url, $method, $format = null)
1403+
public function doCurlRequest(string $url, string $method, string $format = '')
13771404
{
1378-
if (null === $format) {
1405+
if (empty($format)) {
13791406
$format = $this->format;
13801407
}
13811408

@@ -1434,12 +1461,10 @@ public function doCurlRequest($url, $method, $format = null)
14341461

14351462
/**
14361463
* Return last response success status.
1437-
*
1438-
* @return bool
14391464
*/
1440-
public function success()
1465+
public function success(): bool
14411466
{
1442-
return $this->lastResponseCode === 200;
1467+
return ($this->lastResponseCode === 200) && $this->success;
14431468
}
14441469

14451470
/**
@@ -2888,6 +2913,19 @@ public function getLastOperationType(): string
28882913
return implode(',', array_keys(array_filter($this->responseStats)));
28892914
}
28902915

2916+
/**
2917+
* @return array<int, string> Description
2918+
*/
2919+
public function getMessages(): array
2920+
{
2921+
return $this->messages;
2922+
}
2923+
2924+
public function getOperation(): string
2925+
{
2926+
return $this->operation;
2927+
}
2928+
28912929
/**
28922930
* Last operation errors.
28932931
*

src/AbraFlexi/RW.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,11 @@ public function parseResponse($responseDecoded, $responseCode)
159159
*/
160160
public function parseError(array $responseDecoded)
161161
{
162-
if (\array_key_exists('results', $responseDecoded)) {
162+
if (\array_key_exists('errors', $responseDecoded) && \is_array($responseDecoded['errors'])) {
163+
$this->errors = $responseDecoded['errors'];
164+
}
165+
166+
if (\array_key_exists('results', $responseDecoded) && \is_array($responseDecoded['results'])) {
163167
if (\array_key_exists(0, $responseDecoded['results'])) {
164168
foreach ($responseDecoded['results'] as $result) {
165169
if (\array_key_exists('request-id', $result)) {
@@ -514,12 +518,10 @@ public function sync($data = null)
514518

515519
/**
516520
* Last operation was successfull ?
517-
*
518-
* @return bool
519521
*/
520-
public function success()
522+
public function success(): bool
521523
{
522-
$this->curlInfo['http_method'] === 'POST' ? $this->lastResponseCode === 201 : parent::success();
524+
return $this->curlInfo['http_method'] === 'POST' ? $this->lastResponseCode === 201 : parent::success();
523525
}
524526

525527
/**

0 commit comments

Comments
 (0)