1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Tfboe\FmLib\Exceptions; |
5
|
|
|
|
6
|
|
|
use Exception; |
7
|
|
|
use Illuminate\Validation\ValidationException; |
8
|
|
|
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Handler |
12
|
|
|
* @package Tfboe\FmLib\Exceptions |
13
|
|
|
*/ |
14
|
|
|
class Handler extends ExceptionHandler |
15
|
|
|
{ |
16
|
|
|
//<editor-fold desc="Fields"> |
17
|
|
|
/** |
18
|
|
|
* A list of the exception types that should not be reported. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $dontReport = [ |
23
|
|
|
ValidationException::class, |
24
|
|
|
]; |
25
|
|
|
//</editor-fold desc="Fields"> |
26
|
|
|
|
27
|
|
|
//<editor-fold desc="Public Methods"> |
28
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection */ |
29
|
|
|
/** |
30
|
|
|
* Render an exception into an HTTP response. |
31
|
|
|
* |
32
|
|
|
* @param \Illuminate\Http\Request $request |
33
|
|
|
* @param \Exception $exception |
34
|
|
|
* @param bool $printTrace if true a trace will be appended to the exception message |
35
|
|
|
* @return \Illuminate\Http\Response |
36
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
37
|
|
|
*/ |
38
|
|
|
public function render($request, Exception $exception, $printTrace = false) |
39
|
|
|
{ |
40
|
|
|
//don't throw html exceptions always render using json |
41
|
|
|
$statusCode = $this->getExceptionHTTPStatusCode($exception); |
42
|
|
|
|
43
|
|
|
return response()->json( |
|
|
|
|
44
|
|
|
$this->getJsonMessage($exception, $statusCode, $printTrace), |
45
|
|
|
$statusCode |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
//</editor-fold desc="Public Methods"> |
49
|
|
|
|
50
|
|
|
//<editor-fold desc="Protected Methods"> |
51
|
|
|
/** |
52
|
|
|
* Extracts the status code of an exception |
53
|
|
|
* @param Exception $exception the exception to extract from |
54
|
|
|
* @return int|mixed the status code or 500 if no status code found |
55
|
|
|
*/ |
56
|
|
|
protected function getExceptionHTTPStatusCode(Exception $exception) |
57
|
|
|
{ |
58
|
|
|
// Not all Exceptions have a http status code |
59
|
|
|
// We will give Error 500 if none found |
60
|
|
|
if ($exception instanceof ValidationException) { |
61
|
|
|
return $exception->getResponse()->getStatusCode(); |
62
|
|
|
} |
63
|
|
|
return method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : |
|
|
|
|
64
|
|
|
($exception->getCode() > 0 ? $exception->getCode() : 500); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Gets the exception name of an exception which is used by clients to identify the type of the error. |
69
|
|
|
* @param Exception $exception the exception whose name we want |
70
|
|
|
* @return string the exception name |
71
|
|
|
*/ |
72
|
|
|
protected function getExceptionName(Exception $exception): string |
73
|
|
|
{ |
74
|
|
|
if ($exception instanceof AuthenticationException) { |
75
|
|
|
return ExceptionNames::AUTHENTICATION_EXCEPTION; |
76
|
|
|
} |
77
|
|
|
if ($exception instanceof DuplicateException) { |
78
|
|
|
return ExceptionNames::DUPLICATE_EXCEPTION; |
79
|
|
|
} |
80
|
|
|
if ($exception instanceof UnorderedPhaseNumberException) { |
81
|
|
|
return ExceptionNames::UNORDERED_PHASE_NUMBER_EXCEPTION; |
82
|
|
|
} |
83
|
|
|
if ($exception instanceof ReferenceException) { |
84
|
|
|
return ExceptionNames::REFERENCE_EXCEPTION; |
85
|
|
|
} |
86
|
|
|
if ($exception instanceof PlayerAlreadyExists) { |
87
|
|
|
return ExceptionNames::PLAYER_ALREADY_EXISTS_EXCEPTION; |
88
|
|
|
} |
89
|
|
|
if ($exception instanceof ValidationException) { |
90
|
|
|
return ExceptionNames::VALIDATION_EXCEPTION; |
91
|
|
|
} |
92
|
|
|
return ExceptionNames::INTERNAL_EXCEPTION; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Extracts the status and the message from the given exception and status code |
97
|
|
|
* @param Exception $exception the raised exception |
98
|
|
|
* @param string|null $statusCode the status code or null if unknown |
99
|
|
|
* @param bool $printTrace if true a trace will be appended to the exception message |
100
|
|
|
* @return array containing the infos status and message |
101
|
|
|
*/ |
102
|
|
|
protected function getJsonMessage(Exception $exception, $statusCode = null, $printTrace = false) |
103
|
|
|
{ |
104
|
|
|
|
105
|
|
|
$result = method_exists($exception, 'getJsonMessage') ? $exception->getJsonMessage() : |
|
|
|
|
106
|
|
|
['message' => $exception->getMessage()]; |
107
|
|
|
|
108
|
|
|
if ($exception instanceof ValidationException) { |
109
|
|
|
$result["errors"] = $exception->errors(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (!array_key_exists('status', $result)) { |
113
|
|
|
$result['status'] = $statusCode !== null ? $statusCode : "false"; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (!array_key_exists('name', $result)) { |
117
|
|
|
$result['name'] = $this->getExceptionName($exception); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ($printTrace || env('APP_DEBUG') === true) { |
121
|
|
|
//attach trace back |
122
|
|
|
$result['trace'] = $exception->getTraceAsString(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $result; |
126
|
|
|
} |
127
|
|
|
//</editor-fold desc="Protected Methods"> |
128
|
|
|
} |
129
|
|
|
|
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: