1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Storgman - Student Organizations Management |
5
|
|
|
* Copyright (C) 2014, Dejan Angelov <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This file is part of Storgman. |
8
|
|
|
* |
9
|
|
|
* Storgman is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU General Public License as published by |
11
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
12
|
|
|
* (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* Storgman is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU General Public License |
20
|
|
|
* along with Storgman. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
* @package Storgman |
23
|
|
|
* @copyright Copyright (C) 2014, Dejan Angelov <[email protected]> |
24
|
|
|
* @license https://github.com/angelov/storgman/blob/master/LICENSE |
25
|
|
|
* @author Dejan Angelov <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
namespace Angelov\Storgman\Core\Exceptions; |
29
|
|
|
|
30
|
|
|
use Angelov\Storgman\Core\Exceptions\ResourceNotFoundException; |
31
|
|
|
use Exception; |
32
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
33
|
|
|
use Illuminate\Contracts\Validation\ValidationException; |
34
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
35
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
36
|
|
|
use Illuminate\Http\JsonResponse; |
37
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
38
|
|
|
|
39
|
|
|
class Handler extends ExceptionHandler |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* A list of the exception types that should not be reported. |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $dontReport = [ |
47
|
|
|
AuthorizationException::class, |
48
|
|
|
HttpException::class, |
49
|
|
|
ModelNotFoundException::class, |
50
|
|
|
ValidationException::class, |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Report or log an exception. |
55
|
|
|
* |
56
|
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
57
|
|
|
* |
58
|
|
|
* @param \Exception $e |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public function report(Exception $e) |
62
|
|
|
{ |
63
|
|
|
return ExceptionHandler::report($e); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Render an exception into an HTTP response. |
68
|
|
|
* |
69
|
|
|
* @param \Illuminate\Http\Request $request |
70
|
|
|
* @param \Exception $e |
71
|
|
|
* @return \Illuminate\Http\Response |
72
|
|
|
*/ |
73
|
|
|
public function render($request, Exception $e) |
74
|
|
|
{ |
75
|
|
|
if ($this->isHttpException($e)) { |
76
|
|
|
return $this->renderHttpException($e); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($e instanceof ResourceNotFoundException) { |
80
|
|
|
$data = []; |
81
|
|
|
$data['status'] = 'warning'; |
82
|
|
|
$data['message'] = 'There was something wrong with your request.'; |
83
|
|
|
|
84
|
|
|
return new JsonResponse($data); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return ExceptionHandler::render($request, $e); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.