-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathShopwareHttpException.php
More file actions
119 lines (103 loc) · 3.08 KB
/
ShopwareHttpException.php
File metadata and controls
119 lines (103 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php declare(strict_types=1);
namespace Shopware\Core\Framework;
use Shopware\Core\Framework\Log\Package;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
/**
* @phpstan-type ErrorData array{
* status: string,
* code: string|null,
* title?: string,
* detail: string|\Stringable|null,
* template?: string,
* meta?: array{
* parameters?: array<string, mixed>,
* documentationLink?: string,
* trace?: array<int, mixed>,
* file?: string,
* line?: int,
* previous?: mixed
* },
* source?: array{pointer: string},
* trace?: array<int, mixed>|string
* }
*/
#[Package('framework')]
abstract class ShopwareHttpException extends HttpException implements ShopwareException
{
/**
* @var array<string, mixed>
*/
protected array $parameters = [];
/**
* @param array<string, mixed> $parameters
*/
public function __construct(
string $message,
array $parameters = [],
?\Throwable $e = null
) {
$this->parameters = $parameters;
$message = $this->parse($message, $parameters);
parent::__construct($this->getStatusCode(), $message, $e);
}
public function getStatusCode(): int
{
return Response::HTTP_INTERNAL_SERVER_ERROR;
}
/**
* @return \Generator<ErrorData>
*/
public function getErrors(bool $withTrace = false): \Generator
{
yield $this->getCommonErrorData($withTrace);
}
/**
* @return array<string, mixed>
*/
public function getParameters(): array
{
return $this->parameters;
}
/**
* @return mixed|null
*/
public function getParameter(string $key)
{
return $this->parameters[$key] ?? null;
}
/**
* @return array{status: string, code: string, title: string, detail: string, meta: array{parameters: array<string, mixed>}, trace?: array<int, mixed>}
*/
protected function getCommonErrorData(bool $withTrace = false): array
{
$error = [
'status' => (string) $this->getStatusCode(),
'code' => $this->getErrorCode(),
'title' => Response::$statusTexts[$this->getStatusCode()] ?? 'unknown status',
'detail' => $this->getMessage(),
'meta' => [
'parameters' => $this->getParameters(),
],
];
if ($withTrace) {
$error['trace'] = $this->getTrace();
}
return $error;
}
/**
* @param array<string, mixed> $parameters
*/
protected function parse(string $message, array $parameters = []): string
{
$regex = [];
foreach ($parameters as $key => $value) {
if (\is_array($value)) {
continue;
}
$formattedKey = (string) preg_replace('/[^a-z]/i', '', $key);
$regex[\sprintf('/\{\{(\s+)?(%s)(\s+)?\}\}/', $formattedKey)] = $value;
}
return (string) preg_replace(array_keys($regex), array_values($regex), $message);
}
}