|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OrcaServices\NovaApi\Parser; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
7
|
|
|
use GuzzleHttp\Exception\ServerException; |
|
8
|
|
|
use OrcaServices\NovaApi\Exception\NovaApiUnauthorizedException; |
|
9
|
|
|
use OrcaServices\NovaApi\Result\NovaApiErrorList; |
|
10
|
|
|
use OrcaServices\NovaApi\Xml\XmlDocument; |
|
11
|
|
|
use UnexpectedValueException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class. |
|
15
|
|
|
*/ |
|
16
|
|
|
final class NovaApiErrorParser |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var NovaApiSoapErrorParser |
|
20
|
|
|
*/ |
|
21
|
|
|
private $novaSoapError; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param NovaApiSoapErrorParser $novaSoapError The novaSoapError |
|
27
|
|
|
*/ |
|
28
|
13 |
|
public function __construct( |
|
29
|
|
|
NovaApiSoapErrorParser $novaSoapError |
|
30
|
|
|
) { |
|
31
|
13 |
|
$this->novaSoapError = $novaSoapError; |
|
32
|
13 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Create human readable exception from exception. |
|
36
|
|
|
* |
|
37
|
|
|
* @param Exception $exception The Exception object |
|
38
|
|
|
* |
|
39
|
|
|
* @return UnexpectedValueException|NovaApiUnauthorizedException |
|
40
|
|
|
*/ |
|
41
|
|
|
public function createGeneralException(Exception $exception) |
|
42
|
|
|
{ |
|
43
|
|
|
$message = $this->getExceptionMessage($exception); |
|
44
|
|
|
|
|
45
|
|
|
if ($exception instanceof ServerException && $exception->getCode() === 401) { |
|
46
|
|
|
return new NovaApiUnauthorizedException($message, $exception->getCode(), $exception); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return new UnexpectedValueException($message, $exception->getCode(), $exception); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get Client Exception Message as string. |
|
54
|
|
|
* |
|
55
|
|
|
* @param Exception $exception Exception |
|
56
|
|
|
* |
|
57
|
|
|
* @return string message as string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getExceptionMessage(Exception $exception): string |
|
60
|
|
|
{ |
|
61
|
|
|
$errors = new NovaApiErrorList(); |
|
62
|
|
|
|
|
63
|
|
|
if ($exception instanceof ClientException) { |
|
64
|
|
|
$message = sprintf('Client error [%s] %s', $exception->getCode(), $exception->getMessage()); |
|
65
|
|
|
$errors = $errors->withError('0', $message); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if ($exception instanceof ServerException) { |
|
69
|
|
|
$message = sprintf('Server error [%s] %s', $exception->getCode(), $exception->getMessage()); |
|
70
|
|
|
$errors = $errors->withError('0', $message); |
|
71
|
|
|
|
|
72
|
|
|
$response = $exception->getResponse(); |
|
73
|
|
|
$body = $response ? (string)$response->getBody() : ''; |
|
74
|
|
|
|
|
75
|
|
|
if (strpos($body, '<?xml') === 0 || strpos($body, '<SOAP') === 0) { |
|
76
|
|
|
$xmlError = XmlDocument::createFromXmlString($body); |
|
77
|
|
|
$errors = $this->novaSoapError->getSoapErrors($xmlError, $errors); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// Default exceptions |
|
82
|
|
|
$errors = $errors->withError($exception->getCode(), $exception->getMessage()); |
|
83
|
|
|
|
|
84
|
|
|
$message = ''; |
|
85
|
|
|
foreach ($errors->getErrors() as $index => $error) { |
|
86
|
|
|
$message .= sprintf("%s. %s\n", (int)$index + 1, $error->getMessage()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return trim($message); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|