1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Soluble\Japha\Bridge\Driver\Pjb62\Proxy; |
6
|
|
|
|
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Soluble\Japha\Bridge\Driver\Pjb62; |
9
|
|
|
use Soluble\Japha\Bridge\Exception; |
10
|
|
|
use Soluble\Japha\Bridge\Driver\Pjb62\Client; |
11
|
|
|
|
12
|
|
|
class DefaultThrowExceptionProxyFactory extends Pjb62\ThrowExceptionProxyFactory |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var LoggerInterface |
16
|
|
|
*/ |
17
|
|
|
protected $logger; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $defaultException = 'JavaException'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $msgPatternsMapping = [ |
28
|
|
|
'NoSuchMethodException' => '/(php.java.bridge.NoSuchProcedureException)|(Cause: java.lang.NoSuchMethodException)/', |
29
|
|
|
'ClassNotFoundException' => '/Cause: java.lang.ClassNotFoundException/', |
30
|
|
|
//'InvalidArgumentException' => '/^Invoke failed(.*)php.java.bridge.NoSuchProcedureException/', |
31
|
|
|
'SqlException' => '/^Invoke failed(.*)java.sql.SQLException/', |
32
|
|
|
'NoSuchFieldException' => '/Cause: java.lang.NoSuchFieldException/', |
33
|
|
|
//'NullPointerException' => '/Cause: java.lang.NullPointerException/' |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Client $client |
38
|
|
|
* @param LoggerInterface $logger |
39
|
|
|
*/ |
40
|
25 |
|
public function __construct(Client $client, LoggerInterface $logger) |
41
|
|
|
{ |
42
|
25 |
|
parent::__construct($client); |
43
|
25 |
|
$this->logger = $logger; |
44
|
25 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Pjb62\Exception\JavaException $result |
48
|
|
|
* |
49
|
|
|
* @throws Exception\JavaExceptionInterface |
50
|
|
|
*/ |
51
|
22 |
|
public function checkResult(Pjb62\Exception\JavaException $result): void |
52
|
|
|
{ |
53
|
22 |
|
throw $this->getExceptionFromResult($result); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Pjb62\Exception\JavaException $result |
58
|
|
|
* |
59
|
|
|
* @return Exception\JavaExceptionInterface |
60
|
|
|
*/ |
61
|
22 |
|
private function getExceptionFromResult(Pjb62\Exception\JavaException $result): Exception\JavaExceptionInterface |
62
|
|
|
{ |
63
|
22 |
|
$message = (string) $result->__get('message')->__toString(); |
64
|
|
|
|
65
|
22 |
|
$exceptionClass = $this->defaultException; |
66
|
|
|
|
67
|
22 |
|
foreach ($this->msgPatternsMapping as $key => $pattern) { |
68
|
22 |
|
if (preg_match($pattern, $message)) { |
69
|
19 |
|
$exceptionClass = (string) $key; |
70
|
19 |
|
break; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
22 |
|
$cls = '\\Soluble\\Japha\\Bridge\\Exception\\'.$exceptionClass; |
75
|
|
|
|
76
|
|
|
// Public message, mask any login/passwords |
77
|
22 |
|
$message = preg_replace('/user=([^&\ ]+)|password=([^&\ ]+)/', '****', $message); |
78
|
22 |
|
$stackTrace = $result->getCause()->__toString(); |
|
|
|
|
79
|
22 |
|
$code = $result->getCode(); |
80
|
|
|
|
81
|
22 |
|
$driverException = null; |
82
|
22 |
|
if ($result instanceof \Exception) { |
|
|
|
|
83
|
22 |
|
$driverException = $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Getting original class name from cause |
87
|
22 |
|
preg_match('/Cause: ([^:]+):/', $message, $matches); |
88
|
22 |
|
if (count($matches) > 1) { |
89
|
22 |
|
$javaExceptionClass = $matches[1]; |
90
|
|
|
} else { |
91
|
|
|
$javaExceptionClass = 'Unknown java exception class'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Getting cause from message |
95
|
22 |
|
$tmp = explode('Cause: ', $message); |
96
|
22 |
|
if (count($tmp) > 1) { |
97
|
22 |
|
array_shift($tmp); |
98
|
22 |
|
$cause = trim(implode(', ', $tmp)); |
99
|
|
|
} else { |
100
|
|
|
$cause = $message; |
101
|
|
|
} |
102
|
22 |
|
$e = new $cls( |
103
|
22 |
|
$message, |
104
|
22 |
|
$cause, |
105
|
22 |
|
$stackTrace, |
106
|
22 |
|
$javaExceptionClass, |
107
|
22 |
|
$code, |
108
|
22 |
|
$driverException, |
109
|
22 |
|
null |
110
|
|
|
); |
111
|
|
|
|
112
|
22 |
|
$this->logException($e, $exceptionClass); |
113
|
|
|
|
114
|
22 |
|
return $e; |
115
|
|
|
} |
116
|
|
|
|
117
|
22 |
|
private function logException(\Throwable $e, string $exceptionClass): void |
118
|
|
|
{ |
119
|
22 |
|
$this->logger->error(sprintf( |
120
|
22 |
|
'[soluble-japha] Encountered exception %s: %s, code %s (%s)', |
121
|
22 |
|
$exceptionClass, |
122
|
22 |
|
$e->getMessage(), |
123
|
22 |
|
$e->getCode() ?? '?', |
124
|
22 |
|
get_class($e) |
125
|
|
|
)); |
126
|
22 |
|
} |
127
|
|
|
} |
128
|
|
|
|