Completed
Push — master ( 1c7026...6247dd )
by Peter
14:53
created

ExceptionDecorator::getDecorated()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 0
cts 23
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 4
nop 3
crap 20
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: peter
5
 * Date: 15.06.18
6
 * Time: 14:48
7
 */
8
9
namespace Maslosoft\Manganel\Helpers;
10
11
12
use Elasticsearch\Common\Exceptions\BadRequest400Exception;
13
use Exception;
14
use function json_decode;
15
use Maslosoft\Manganel\Manganel;
16
use function str_replace;
17
18
class ExceptionDecorator
19
{
20
	public static function getDecorated(Manganel $manganel, BadRequest400Exception $exception, $params)
21
	{
22
		// Throw previous exception,
23
		// as it holds more meaningful information
24
		$json = json_encode($params, JSON_PRETTY_PRINT);
25
26
		$msg = $exception->getMessage();
27
28
		$decoded = json_decode($msg);
29
		if(!empty($decoded) && !empty($decoded->error->root_cause[0]->reason))
30
		{
31
			$msg = $decoded->error->root_cause[0]->reason;
32
		}
33
34
		$prevMsg = '';
35
		$previous = $exception->getPrevious();
36
		if(!empty($previous))
37
		{
38
			$prevMsg = '(' . $previous->getMessage() . ')';
39
		}
40
41
		$params = [
42
			$msg . ' ' . $prevMsg,
43
			$manganel->indexId,
44
			$json
45
		];
46
47
48
		$message = vsprintf("Exception %s while querying `%s`: \n%s\n", $params);
49
		return new BadRequest400Exception($message, 400, $exception);
50
	}
51
}