Skip to content

Commit 8a9d125

Browse files
LuborRodRodion Liuborets
andauthored
SDK-1755:Add http exception response (#230)
* SDK-1755:Add http exception response * SDK-1755-Add-unit-tests * SDK-1755:Add abstract Yoti Exception * SDK-1755:Add response to exceptions * SDK-1755:Fix phpDoc * SDK-1755:Fix phpDoc * SDK-1755:Fix strict comparison * SDK-1755 Add response to exceptions Co-authored-by: Rodion Liuborets <[email protected]>
1 parent 3471bee commit 8a9d125

18 files changed

+250
-191
lines changed

src/Aml/Result.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yoti\Aml;
66

7+
use Psr\Http\Message\ResponseInterface;
78
use Yoti\Exception\AmlException;
89
use Yoti\Util\Json;
910

@@ -45,13 +46,14 @@ class Result
4546
* AmlResult constructor.
4647
*
4748
* @param array<string, bool> $result
49+
* @param ResponseInterface $response
4850
*
4951
* @throws \Yoti\Exception\AmlException
5052
*/
51-
public function __construct(array $result)
53+
public function __construct(array $result, ResponseInterface $response)
5254
{
5355
$this->rawResult = $result;
54-
$this->setAttributes();
56+
$this->setAttributes($response);
5557
}
5658

5759
/**
@@ -87,13 +89,15 @@ public function isOnWatchList(): bool
8789
/**
8890
* Set attribute values.
8991
*
92+
* @param ResponseInterface $response
93+
*
9094
* @throws \Yoti\Exception\AmlException
9195
*/
92-
private function setAttributes(): void
96+
private function setAttributes(ResponseInterface $response): void
9397
{
9498
$result = $this->rawResult;
9599
// Check if no attribute is missing from the result
96-
self::checkAttributes($result);
100+
self::checkAttributes($result, $response);
97101

98102
$this->onPepList = (bool) $result[self::ON_PEP_LIST_KEY];
99103
$this->onFraudList = (bool) $result[self::ON_FRAUD_LIST_KEY];
@@ -104,10 +108,11 @@ private function setAttributes(): void
104108
* Check if all the attributes are included in the result.
105109
*
106110
* @param array<string, bool> $result
111+
* @param ResponseInterface $response
107112
*
108113
* @throws \Yoti\Exception\AmlException
109114
*/
110-
public static function checkAttributes(array $result): void
115+
public static function checkAttributes(array $result, ResponseInterface $response): void
111116
{
112117
$expectedAttributes = [
113118
self::ON_PEP_LIST_KEY,
@@ -119,7 +124,7 @@ public static function checkAttributes(array $result): void
119124

120125
// Throw an error if any expected attribute is missing.
121126
if (count($missingAttr) > 0) {
122-
throw new AmlException('Missing attributes from the result: ' . implode(',', $missingAttr));
127+
throw new AmlException('Missing attributes from the result: ' . implode(',', $missingAttr), $response);
123128
}
124129
}
125130

src/Aml/Service.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ class Service
2121
private $sdkId;
2222

2323
/**
24-
* @var \Yoti\Util\PemFile
24+
* @var PemFile
2525
*/
2626
private $pemFile;
2727

2828
/**
29-
* @var \Yoti\Util\Config
29+
* @var Config
3030
*/
3131
private $config;
3232

3333
/**
3434
* @param string $sdkId
35-
* @param \Yoti\Util\PemFile $pemFile
36-
* @param \Yoti\Util\Config $config
35+
* @param PemFile $pemFile
36+
* @param Config $config
3737
*/
3838
public function __construct(string $sdkId, PemFile $pemFile, Config $config)
3939
{
@@ -43,11 +43,11 @@ public function __construct(string $sdkId, PemFile $pemFile, Config $config)
4343
}
4444

4545
/**
46-
* @param \Yoti\Aml\Profile $amlProfile
46+
* @param Profile $amlProfile
4747
*
48-
* @return \Yoti\Aml\Result
48+
* @return Result
4949
*
50-
* @throws \Yoti\Exception\AmlException
50+
* @throws AmlException
5151
*/
5252
public function performCheck(Profile $amlProfile): Result
5353
{
@@ -65,15 +65,15 @@ public function performCheck(Profile $amlProfile): Result
6565
$this->validateAmlResult($response);
6666

6767
// Set and return result
68-
return new Result(Json::decode((string) $response->getBody()));
68+
return new Result(Json::decode((string)$response->getBody()), $response);
6969
}
7070

7171
/**
7272
* Handle request result.
7373
*
74-
* @param \Psr\Http\Message\ResponseInterface $response
74+
* @param ResponseInterface $response
7575
*
76-
* @throws \Yoti\Exception\AmlException
76+
* @throws AmlException
7777
*/
7878
private function validateAmlResult(ResponseInterface $response): void
7979
{
@@ -84,13 +84,13 @@ private function validateAmlResult(ResponseInterface $response): void
8484
return;
8585
}
8686

87-
throw new AmlException($this->getErrorMessage($response));
87+
throw new AmlException($this->getErrorMessage($response), $response);
8888
}
8989

9090
/**
9191
* Get error message from the response.
9292
*
93-
* @param \Psr\Http\Message\ResponseInterface $response
93+
* @param ResponseInterface $response
9494
*
9595
* @return string
9696
*/
@@ -106,9 +106,9 @@ private function getErrorMessage(ResponseInterface $response): string
106106
return $statusCodeMessage;
107107
}
108108

109-
$jsonData = Json::decode((string) $response->getBody());
109+
$jsonData = Json::decode((string)$response->getBody());
110110

111-
$errorCode = isset($jsonData['code']) ? $jsonData['code'] : 'Error';
111+
$errorCode = $jsonData['code'] ?? 'Error';
112112

113113
// Throw the error message that's included in the response.
114114
if (isset($jsonData['errors'][0]['property']) && isset($jsonData['errors'][0]['message'])) {

src/DocScan/DocScanClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Yoti\DocScan\Session\Retrieve\Instructions\ContactProfileResponse;
1616
use Yoti\DocScan\Session\Retrieve\Instructions\InstructionsResponse;
1717
use Yoti\DocScan\Support\SupportedDocumentsResponse;
18+
use Yoti\Exception\PemFileException;
1819
use Yoti\Media\Media;
1920
use Yoti\Util\Config;
2021
use Yoti\Util\Env;
@@ -47,7 +48,7 @@ class DocScanClient
4748
* @param array<string, mixed> $options (optional)
4849
* SDK configuration options - {@see \Yoti\Util\Config} for available options.
4950
*
50-
* @throws \Yoti\Exception\PemFileException
51+
* @throws PemFileException
5152
*/
5253
public function __construct(
5354
string $sdkId,
@@ -102,7 +103,6 @@ public function deleteSession(string $sessionId): void
102103
$this->docScanService->deleteSession($sessionId);
103104
}
104105

105-
106106
/**
107107
* Retrieves media related to a Yoti Doc Scan session based
108108
* on the supplied media ID.

src/DocScan/Exception/DocScanException.php

Lines changed: 2 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -4,106 +4,8 @@
44

55
namespace Yoti\DocScan\Exception;
66

7-
use Psr\Http\Message\ResponseInterface;
8-
use Yoti\Util\Json;
7+
use Yoti\Exception\base\YotiException;
98

10-
class DocScanException extends \Exception
9+
class DocScanException extends YotiException
1110
{
12-
/**
13-
* @var ResponseInterface|null
14-
*/
15-
private $response;
16-
17-
/**
18-
* DocScanException constructor.
19-
* @param string $message
20-
* @param ResponseInterface|null $response
21-
* @param \Throwable|null $previous
22-
*/
23-
public function __construct($message = "", ?ResponseInterface $response = null, \Throwable $previous = null)
24-
{
25-
parent::__construct($this->formatMessage($message, $response), 0, $previous);
26-
27-
$this->response = $response;
28-
}
29-
30-
/**
31-
* Returns the HTTP response object returned
32-
* from the Doc Scan API.
33-
*
34-
* @return ResponseInterface|null
35-
*/
36-
public function getResponse(): ?ResponseInterface
37-
{
38-
return $this->response;
39-
}
40-
41-
/**
42-
* @param string $message
43-
* @param ResponseInterface|null $response
44-
*
45-
* @return string
46-
*/
47-
private function formatMessage(string $message, ?ResponseInterface $response): string
48-
{
49-
if (
50-
$response === null ||
51-
!$response->hasHeader('Content-Type') ||
52-
$response->getHeader('Content-Type')[0] !== 'application/json'
53-
) {
54-
return $message;
55-
}
56-
57-
$jsonData = Json::decode((string) $response->getBody(), false);
58-
$formattedResponse = $this->formatResponse($jsonData);
59-
if ($formattedResponse !== null) {
60-
return sprintf('%s - %s', $message, $formattedResponse);
61-
}
62-
63-
return $message;
64-
}
65-
66-
/**
67-
* @param \stdClass $jsonData
68-
*
69-
* @return string|null
70-
*/
71-
private function formatResponse(\stdClass $jsonData): ?string
72-
{
73-
if (!isset($jsonData->message) || !isset($jsonData->code)) {
74-
return null;
75-
}
76-
77-
$responseMessage = sprintf('%s - %s', $jsonData->code, $jsonData->message);
78-
79-
$propertyErrors = $this->formatPropertyErrors($jsonData);
80-
if (count($propertyErrors) > 0) {
81-
return sprintf('%s: %s', $responseMessage, implode(', ', $propertyErrors));
82-
}
83-
84-
return $responseMessage;
85-
}
86-
87-
/**
88-
* @param \stdClass $jsonData
89-
*
90-
* @return string[]
91-
*/
92-
private function formatPropertyErrors(\stdClass $jsonData): array
93-
{
94-
if (!isset($jsonData->errors) || !is_array($jsonData->errors)) {
95-
return [];
96-
}
97-
98-
return array_reduce(
99-
$jsonData->errors,
100-
function ($carry, $error): array {
101-
if (isset($error->property) && isset($error->message)) {
102-
$carry[] = sprintf('%s "%s"', $error->property, $error->message);
103-
}
104-
return $carry;
105-
},
106-
[]
107-
);
108-
}
10911
}

src/DocScan/Service.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public function fetchInstructionsContactProfile(string $sessionId): ContactProfi
380380

381381
self::assertResponseIsSuccess($response);
382382

383-
$result = Json::decode((string)$response->getBody());
383+
$result = Json::decode((string) $response->getBody());
384384

385385
return new ContactProfileResponse($result);
386386
}

src/Exception/ActivityDetailsException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Yoti\Exception;
66

7-
class ActivityDetailsException extends \Exception
7+
use Yoti\Exception\base\YotiException;
8+
9+
class ActivityDetailsException extends YotiException
810
{
911
}

src/Exception/AmlException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Yoti\Exception;
66

7-
class AmlException extends \Exception
7+
use Yoti\Exception\base\YotiException;
8+
9+
class AmlException extends YotiException
810
{
911
}

src/Exception/ShareUrlException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Yoti\Exception;
66

7-
class ShareUrlException extends \Exception
7+
use Yoti\Exception\base\YotiException;
8+
9+
class ShareUrlException extends YotiException
810
{
911
}

0 commit comments

Comments
 (0)