|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Laravel Platfourm package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Longman\Platfourm\Foundation\Exceptions; |
|
12
|
|
|
|
|
13
|
|
|
use App; |
|
14
|
|
|
use Exception; |
|
15
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
16
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
|
17
|
|
|
use Illuminate\Foundation\Validation\ValidationException; |
|
18
|
|
|
use Slack; |
|
19
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
20
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
21
|
|
|
|
|
22
|
|
|
class Handler extends ExceptionHandler |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* A list of the exception types that should not be reported. |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $dontReport = [ |
|
30
|
|
|
AuthorizationException::class, |
|
31
|
|
|
HttpException::class, |
|
32
|
|
|
ModelNotFoundException::class, |
|
33
|
|
|
ValidationException::class, |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
public function sendToSlack(Exception $e) |
|
37
|
|
|
{ |
|
38
|
|
|
if (!App::environment('production')) { |
|
39
|
|
|
return false; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$endpoint = config('slack.endpoint'); |
|
43
|
|
|
if (strlen($endpoint) < 40) { |
|
44
|
|
|
return false; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
Slack::send($e->getMessage()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Render the given HttpException. |
|
52
|
|
|
* |
|
53
|
|
|
* @param \Symfony\Component\HttpKernel\Exception\HttpException $e |
|
54
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function renderHttpException(HttpException $e) |
|
57
|
|
|
{ |
|
58
|
|
|
$status = $e->getStatusCode(); |
|
59
|
|
|
|
|
60
|
|
|
if (view()->exists("common.errors.{$status}")) { |
|
|
|
|
|
|
61
|
|
|
return response()->view("common.errors.{$status}", ['exception' => $e], $status, $e->getHeaders()); |
|
62
|
|
|
} else { |
|
63
|
|
|
return $this->convertExceptionToResponse($e); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Render an exception into an HTTP response. |
|
69
|
|
|
* |
|
70
|
|
|
* @param \Illuminate\Http\Request $request |
|
71
|
|
|
* @param \Exception $e |
|
72
|
|
|
* @return \Illuminate\Http\Response |
|
73
|
|
|
*/ |
|
74
|
|
|
public function render($request, Exception $e) |
|
75
|
|
|
{ |
|
76
|
|
|
if ($e instanceof ModelNotFoundException) { |
|
77
|
|
|
$e = new NotFoundHttpException($e->getMessage(), $e); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if ($this->shouldRenderAsJson($request, $e)) { |
|
81
|
|
|
return $this->renderAsJson($request, $e); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return parent::render($request, $e); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function shouldRenderAsJson($request, Exception $e) |
|
88
|
|
|
{ |
|
89
|
|
|
if ($e instanceof ValidationException) { |
|
|
|
|
|
|
90
|
|
|
return false; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $request->wantsJson(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected function renderAsJson($request, Exception $e) |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
// Define the response |
|
99
|
|
|
$response = [ |
|
100
|
|
|
'errors' => 'Sorry, something went wrong.' |
|
101
|
|
|
]; |
|
102
|
|
|
|
|
103
|
|
|
// If the app is in debug mode |
|
104
|
|
|
if (config('app.debug')) { |
|
105
|
|
|
// Add the exception class name, message and stack trace to response |
|
106
|
|
|
$response['exception'] = (new \ReflectionClass($e))->getName(); |
|
|
|
|
|
|
107
|
|
|
$response['message'] = $e->getMessage(); |
|
108
|
|
|
$response['trace'] = $e->getTrace(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// Default response of 400 |
|
112
|
|
|
$status = 400; |
|
113
|
|
|
|
|
114
|
|
|
// If this exception is an instance of HttpException |
|
115
|
|
|
if ($this->isHttpException($e)) { |
|
116
|
|
|
// Grab the HTTP status code from the Exception |
|
117
|
|
|
$status = $e->getStatusCode(); |
|
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// Return a JSON response with the response array and status code |
|
121
|
|
|
return response()->json($response, $status); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
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: