|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sarala\Exceptions; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
use Illuminate\Http\JsonResponse; |
|
10
|
|
|
use Illuminate\Http\Response; |
|
11
|
|
|
|
|
12
|
|
|
abstract class ApiException extends Exception implements JsonApiExceptionContract |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Get unique identifier for this particular occurrence |
|
16
|
|
|
* of the problem. |
|
17
|
|
|
*/ |
|
18
|
|
|
public function id(): string |
|
19
|
|
|
{ |
|
20
|
|
|
return ''; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Get application-specific error code. |
|
25
|
|
|
*/ |
|
26
|
|
|
public function code(): string |
|
27
|
|
|
{ |
|
28
|
|
|
return ''; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Get human-readable explanation specific to this |
|
33
|
|
|
* occurrence of the problem. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function detail(): string |
|
36
|
|
|
{ |
|
37
|
|
|
return ''; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get the URI that yield further details about this |
|
42
|
|
|
* particular occurrence of the problem. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function href(): string |
|
45
|
|
|
{ |
|
46
|
|
|
return ''; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get associated resources, which can be dereferenced |
|
51
|
|
|
* from the request document. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function links(): array |
|
54
|
|
|
{ |
|
55
|
|
|
return []; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get relative path to the relevant attribute within |
|
60
|
|
|
* the associated resource(s). |
|
61
|
|
|
*/ |
|
62
|
|
|
public function path(): string |
|
63
|
|
|
{ |
|
64
|
|
|
return ''; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get non-standard meta-information about the error. |
|
69
|
|
|
*/ |
|
70
|
|
|
public function meta(): array |
|
71
|
|
|
{ |
|
72
|
|
|
return []; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected function getResponseData(): array |
|
76
|
|
|
{ |
|
77
|
|
|
$error = array_merge([ |
|
78
|
|
|
'status' => (string) $this->status(), |
|
79
|
|
|
'title' => (string) $this->title(), |
|
80
|
|
|
], $this->getAvailableData()); |
|
81
|
|
|
|
|
82
|
|
|
return [ |
|
83
|
|
|
'errors' => [$error] |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function getApiResponse(): JsonResponse |
|
88
|
|
|
{ |
|
89
|
|
|
return response() |
|
|
|
|
|
|
90
|
|
|
->json($this->getResponseData(), $this->status()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function render(Request $request) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
return $this->getApiResponse(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function getAvailableData(): array |
|
99
|
|
|
{ |
|
100
|
|
|
$data = []; |
|
101
|
|
|
|
|
102
|
|
|
collect(['links', 'meta', 'id', 'href', 'code', 'detail', 'path'])->each(function ($key) use (&$data) { |
|
103
|
|
|
if (! empty($this->{$key}())) { |
|
104
|
|
|
$data[$key] = $this->{$key}(); |
|
105
|
|
|
} |
|
106
|
|
|
}); |
|
107
|
|
|
|
|
108
|
|
|
return $data; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: