1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pterodactyl\Exceptions; |
4
|
|
|
|
5
|
|
|
use Log; |
6
|
|
|
use Exception; |
7
|
|
|
use DisplayException; |
|
|
|
|
8
|
|
|
use Illuminate\Auth\AuthenticationException; |
9
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
10
|
|
|
|
11
|
|
|
class Handler extends ExceptionHandler |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* A list of the exception types that should not be reported. |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $dontReport = [ |
19
|
|
|
\Illuminate\Auth\AuthenticationException::class, |
20
|
|
|
\Illuminate\Auth\Access\AuthorizationException::class, |
21
|
|
|
\Symfony\Component\HttpKernel\Exception\HttpException::class, |
22
|
|
|
\Illuminate\Database\Eloquent\ModelNotFoundException::class, |
23
|
|
|
\Illuminate\Session\TokenMismatchException::class, |
24
|
|
|
\Illuminate\Validation\ValidationException::class, |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Report or log an exception. |
29
|
|
|
* |
30
|
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
31
|
|
|
* |
32
|
|
|
* @param \Exception $exception |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function report(Exception $exception) |
36
|
|
|
{ |
37
|
|
|
return parent::report($exception); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Render an exception into an HTTP response. |
42
|
|
|
* |
43
|
|
|
* @param \Illuminate\Http\Request $request |
44
|
|
|
* @param \Exception $exception |
45
|
|
|
* @return \Illuminate\Http\Response |
46
|
|
|
*/ |
47
|
|
|
public function render($request, Exception $exception) |
48
|
|
|
{ |
49
|
|
|
if ($request->expectsJson() || $request->isJson() || $request->is(...config('pterodactyl.json_routes'))) { |
50
|
|
|
$exception = $this->prepareException($exception); |
51
|
|
|
|
52
|
|
|
if (config('app.debug')) { |
53
|
|
|
$report = [ |
54
|
|
|
'code' => (! $this->isHttpException($exception)) ?: $exception->getStatusCode(), |
55
|
|
|
'message' => class_basename($exception) . ' in ' . $exception->getFile() . ' on line ' . $exception->getLine(), |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$response = response()->json([ |
60
|
|
|
'error' => (config('app.debug')) ? $exception->getMessage() : 'An unhandled exception was encountered with this request.', |
61
|
|
|
'exception' => ! isset($report) ?: $report, |
62
|
|
|
], ($this->isHttpException($exception)) ? $exception->getStatusCode() : 500, [], JSON_UNESCAPED_SLASHES); |
63
|
|
|
|
64
|
|
|
parent::report($exception); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return (isset($response)) ? $response : parent::render($request, $exception); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Convert an authentication exception into an unauthenticated response. |
72
|
|
|
* |
73
|
|
|
* @param \Illuminate\Http\Request $request |
74
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception |
75
|
|
|
* @return \Illuminate\Http\Response |
76
|
|
|
*/ |
77
|
|
|
protected function unauthenticated($request, AuthenticationException $exception) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
if ($request->expectsJson()) { |
80
|
|
|
return response()->json(['error' => 'Unauthenticated.'], 401); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return redirect()->guest(route('auth.login')); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: