|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under `AGPL-3.0-only, proprietary` license[s]. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/manganel |
|
7
|
|
|
* @license AGPL-3.0-only, proprietary |
|
8
|
|
|
* |
|
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
|
10
|
|
|
* @link https://maslosoft.com/manganel/ |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Maslosoft\Manganel\Helpers; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
use Elasticsearch\Common\Exceptions\BadRequest400Exception; |
|
17
|
|
|
use Exception; |
|
18
|
|
|
use function json_decode; |
|
19
|
|
|
use Maslosoft\Mangan\Events\Event; |
|
20
|
|
|
use Maslosoft\Manganel\Events\ErrorEvent; |
|
21
|
|
|
use Maslosoft\Manganel\Manganel; |
|
22
|
|
|
use function str_replace; |
|
23
|
|
|
|
|
24
|
|
|
class ExceptionHandler |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
8 |
|
public static function getDecorated(Manganel $manganel, Exception $exception, $params) |
|
28
|
|
|
{ |
|
29
|
|
|
// Throw previous exception, |
|
30
|
|
|
// as it holds more meaningful information |
|
31
|
8 |
|
$json = json_encode($params, JSON_PRETTY_PRINT); |
|
32
|
|
|
|
|
33
|
8 |
|
$msg = $exception->getMessage(); |
|
34
|
|
|
|
|
35
|
8 |
|
$decoded = json_decode($msg); |
|
36
|
8 |
|
if (!empty($decoded) && !empty($decoded->error->root_cause[0]->reason)) |
|
37
|
|
|
{ |
|
38
|
8 |
|
$msg = $decoded->error->root_cause[0]->reason; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
8 |
|
$prevMsg = ''; |
|
42
|
8 |
|
$previous = $exception->getPrevious(); |
|
43
|
8 |
|
if (!empty($previous)) |
|
44
|
|
|
{ |
|
45
|
|
|
$prevMsg = '(' . $previous->getMessage() . ')'; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$params = [ |
|
49
|
8 |
|
$msg . ' ' . $prevMsg, |
|
50
|
8 |
|
$manganel->indexId, |
|
51
|
8 |
|
$json |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
8 |
|
$message = vsprintf("Exception %s while querying `%s`: \n%s\n", $params); |
|
56
|
8 |
|
return new BadRequest400Exception($message, 400, $exception); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
8 |
|
public static function handled(Exception $e, $model = null, $eventName = null) |
|
60
|
|
|
{ |
|
61
|
8 |
|
if(empty($model)) |
|
62
|
|
|
{ |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
8 |
|
assert(!empty($eventName)); |
|
66
|
|
|
|
|
67
|
8 |
|
$evt = new ErrorEvent($model); |
|
68
|
8 |
|
$evt->exception = $e; |
|
69
|
8 |
|
if(Event::hasHandler($model, $eventName) && Event::handled($model, $eventName, $evt)) |
|
70
|
|
|
{ |
|
71
|
|
|
return true; |
|
72
|
|
|
} |
|
73
|
8 |
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
} |