1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DMT\WebservicesNl\Client\Exception; |
4
|
|
|
|
5
|
|
|
use DMT\CommandBus\Validator\ValidationException; |
6
|
|
|
use DMT\WebservicesNl\Client\Exception\Client\InputException; |
7
|
|
|
use DMT\WebservicesNl\Client\Exception\Server\Unavailable\InternalErrorException; |
8
|
|
|
use GuzzleHttp\Exception\RequestException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ExceptionHandler |
12
|
|
|
* |
13
|
|
|
* @package DMT\WebservicesNl\Client |
14
|
|
|
*/ |
15
|
|
|
class ExceptionHandler |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param \Throwable $exception |
19
|
|
|
* |
20
|
|
|
* @throws ServerException|ExceptionInterface |
21
|
|
|
*/ |
22
|
28 |
|
public function throwServiceException(\Throwable $exception) |
23
|
|
|
{ |
24
|
28 |
|
$exceptionClass = ExceptionInterface::class; |
25
|
|
|
|
26
|
28 |
|
if (method_exists($exception, 'getFaultCode')) { |
27
|
26 |
|
$exceptionClass = $this->getServiceException($exception->getFaultCode()); |
28
|
|
|
} |
29
|
28 |
|
if ($exceptionClass !== ExceptionInterface::class) { |
30
|
24 |
|
throw new $exceptionClass($exception->getMessage()); |
31
|
|
|
} |
32
|
|
|
|
33
|
4 |
|
throw new ServerException('Unknown error occurred', 0, $exception); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ValidationException $exception |
38
|
|
|
* |
39
|
|
|
* @throws InputException |
40
|
|
|
*/ |
41
|
4 |
|
public function throwServiceExceptionFromViolationException(ValidationException $exception) |
42
|
|
|
{ |
43
|
4 |
|
$message = 'Invalid input given'; |
44
|
|
|
|
45
|
4 |
|
if ($exception->getViolations()->count() === 1) { |
46
|
2 |
|
$message = $exception->getViolations()->get(0)->getMessage(); |
47
|
|
|
} |
48
|
|
|
|
49
|
4 |
|
throw new InputException($message, 0, $exception); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param RequestException $exception |
54
|
|
|
* |
55
|
|
|
* @throws InternalErrorException|ExceptionInterface |
56
|
|
|
*/ |
57
|
25 |
|
public function throwServiceExceptionFromRequestException(RequestException $exception) |
58
|
|
|
{ |
59
|
25 |
|
$exceptionClass = ExceptionInterface::class; |
60
|
|
|
|
61
|
25 |
|
if ($exception->hasResponse() && $exception->getResponse()->hasHeader('X-WS-ErrorCode')) { |
62
|
24 |
|
$exceptionClass = $this->getServiceException($exception->getResponse()->getHeaderLine('X-WS-ErrorCode')); |
63
|
|
|
} |
64
|
25 |
|
if ($exceptionClass !== ExceptionInterface::class) { |
65
|
22 |
|
throw new $exceptionClass(); |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
throw new ServerException('Unknown error occurred', 0, $exception); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $faultCode |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
50 |
|
protected function getServiceException(string $faultCode = 'Server'): string |
76
|
|
|
{ |
77
|
50 |
|
$codes = explode('.', $faultCode); |
78
|
50 |
|
$currentException = ExceptionInterface::class; |
79
|
|
|
|
80
|
50 |
|
foreach ($codes as $code) { |
81
|
50 |
|
$namespace = preg_replace('~\\\?Exception(Interface)?$~', '', $currentException); |
82
|
50 |
|
$childException = $namespace . '\\' . $code . 'Exception'; |
83
|
|
|
|
84
|
50 |
|
if (!class_exists($childException)) { |
85
|
6 |
|
break; |
86
|
|
|
} |
87
|
46 |
|
$currentException = $childException; |
88
|
|
|
} |
89
|
|
|
|
90
|
50 |
|
return $currentException; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|