1
|
|
|
<?php namespace Pz\Doctrine\Rest\Exceptions; |
2
|
|
|
|
3
|
|
|
use Symfony\Component\HttpFoundation\Response; |
4
|
|
|
|
5
|
|
|
class RestException extends \RuntimeException |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var array |
9
|
|
|
*/ |
10
|
|
|
protected $errors = []; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param \Exception $exception |
14
|
|
|
* @param bool $debug |
15
|
|
|
* |
16
|
|
|
* @return $this |
17
|
|
|
*/ |
18
|
|
|
public static function createFromException(\Exception $exception, $debug = false) |
19
|
|
|
{ |
20
|
|
|
$extra = $debug ? ['trace' => $exception->getTrace()] : []; |
21
|
|
|
|
22
|
|
|
return static::create(Response::HTTP_INTERNAL_SERVER_ERROR, 'Internal Server Error', $exception) |
23
|
|
|
->error('internal-error', [], $exception->getMessage(), $extra); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param int|array $id |
28
|
|
|
* @param string $resourceKey |
29
|
|
|
* @param string $message |
30
|
|
|
* |
31
|
|
|
* @return $this |
32
|
|
|
*/ |
33
|
1 |
|
public static function createNotFound($id, $resourceKey, $message = '') |
34
|
|
|
{ |
35
|
1 |
|
return static::create(Response::HTTP_NOT_FOUND, 'Entity not found.') |
36
|
1 |
|
->error('entity-not-found', ['type' => $resourceKey, 'id' => $id], $message); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param array $source |
41
|
|
|
* @param $message |
42
|
|
|
* |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
1 |
|
public static function createFilterError(array $source, $message) |
46
|
|
|
{ |
47
|
1 |
|
return static::create(Response::HTTP_BAD_REQUEST, 'Wrong filter input.') |
48
|
1 |
|
->error('filter-input', $source, $message); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return static |
53
|
|
|
*/ |
54
|
|
|
public static function missingRootData() |
55
|
|
|
{ |
56
|
|
|
return static::create(Response::HTTP_UNPROCESSABLE_ENTITY) |
57
|
|
|
->error('missing-root-data', [], "Missing `data` Member at document's top level."); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param int $httpStatus |
62
|
|
|
* @param string $message |
63
|
|
|
* @param \Exception|null $previous |
64
|
|
|
* |
65
|
|
|
* @return static |
66
|
|
|
*/ |
67
|
2 |
|
static public function create( |
68
|
|
|
$httpStatus = Response::HTTP_INTERNAL_SERVER_ERROR, |
69
|
|
|
$message = '', |
70
|
|
|
\Exception $previous = null |
71
|
|
|
) { |
72
|
2 |
|
return new static($httpStatus, $message, $previous); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* RestException constructor. |
77
|
|
|
* |
78
|
|
|
* @param int|string $httpStatus |
79
|
|
|
* @param string $message |
80
|
|
|
* @param \Exception|null $previous |
81
|
|
|
*/ |
82
|
3 |
|
public function __construct( |
83
|
|
|
$httpStatus = Response::HTTP_INTERNAL_SERVER_ERROR, |
84
|
|
|
$message = '', |
85
|
|
|
\Exception $previous = null |
86
|
|
|
) { |
87
|
3 |
|
parent::__construct($message, $httpStatus, $previous); |
|
|
|
|
88
|
3 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $attribute |
92
|
|
|
* @param $detail |
93
|
|
|
* |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function errorAttribute($attribute, $detail) |
97
|
|
|
{ |
98
|
|
|
return $this->error('attribute-error', ['attribute' => $attribute], $detail); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $relation |
103
|
|
|
* @param $detail |
104
|
|
|
* |
105
|
|
|
* @return RestException |
106
|
|
|
*/ |
107
|
|
|
public function errorRelation($relation, $detail) |
108
|
|
|
{ |
109
|
|
|
return $this->error('relation-error', ['relation' => $relation], $detail); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $applicationCode |
114
|
|
|
* @param array $source |
115
|
|
|
* @param string $detail |
116
|
|
|
* @param array $extra |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
2 |
|
public function error($applicationCode, $source, $detail, $extra = []) |
121
|
|
|
{ |
122
|
2 |
|
$this->errors[] = array_merge(['code' => $applicationCode, 'source' => $source, 'detail' => $detail] + $extra); |
123
|
|
|
|
124
|
2 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
3 |
|
public function errors() |
131
|
|
|
{ |
132
|
3 |
|
return $this->errors; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|