1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Handler.php |
||
5 | * Copyright (c) 2018 [email protected] |
||
6 | * |
||
7 | * This file is part of Firefly III. |
||
8 | * |
||
9 | * Firefly III 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 | * Firefly III 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 Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||
21 | */ |
||
22 | |||
23 | /** @noinspection MultipleReturnStatementsInspection */ |
||
24 | |||
25 | declare(strict_types=1); |
||
26 | |||
27 | namespace FireflyIII\Exceptions; |
||
28 | |||
29 | use ErrorException; |
||
30 | use Exception; |
||
31 | use FireflyIII\Jobs\MailError; |
||
32 | use Illuminate\Auth\AuthenticationException; |
||
33 | use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
||
34 | use Illuminate\Validation\ValidationException as LaravelValidationException; |
||
35 | use League\OAuth2\Server\Exception\OAuthServerException; |
||
36 | use Request; |
||
37 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
38 | |||
39 | /** |
||
40 | * Class Handler |
||
41 | * |
||
42 | * @codeCoverageIgnore |
||
43 | */ |
||
44 | class Handler extends ExceptionHandler |
||
45 | { |
||
46 | /** |
||
47 | * Render an exception into an HTTP response. |
||
48 | * |
||
49 | * @param Request $request |
||
50 | * @param Exception $exception |
||
51 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
52 | * @SuppressWarnings(PHPMD.NPathComplexity) |
||
53 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
54 | * |
||
55 | * @return mixed |
||
56 | */ |
||
57 | public function render($request, Exception $exception) |
||
58 | { |
||
59 | if ($exception instanceof LaravelValidationException && $request->expectsJson()) { |
||
60 | // ignore it: controller will handle it. |
||
61 | return parent::render($request, $exception); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
62 | } |
||
63 | if ($exception instanceof NotFoundHttpException && $request->expectsJson()) { |
||
64 | // JSON error: |
||
65 | return response()->json(['message' => 'Resource not found', 'exception' => 'NotFoundHttpException'], 404); |
||
66 | } |
||
67 | |||
68 | if ($exception instanceof AuthenticationException && $request->expectsJson()) { |
||
69 | // somehow Laravel handler does not catch this: |
||
70 | return response()->json(['message' => 'Unauthenticated', 'exception' => 'AuthenticationException'], 401); |
||
71 | } |
||
72 | |||
73 | if ($exception instanceof OAuthServerException && $request->expectsJson()) { |
||
74 | // somehow Laravel handler does not catch this: |
||
75 | return response()->json(['message' => $exception->getMessage(), 'exception' => 'OAuthServerException'], 401); |
||
76 | } |
||
77 | |||
78 | if ($request->expectsJson()) { |
||
79 | $isDebug = config('app.debug', false); |
||
80 | if ($isDebug) { |
||
81 | return response()->json( |
||
82 | [ |
||
83 | 'message' => $exception->getMessage(), |
||
84 | 'exception' => \get_class($exception), |
||
85 | 'line' => $exception->getLine(), |
||
86 | 'file' => $exception->getFile(), |
||
87 | 'trace' => $exception->getTrace(), |
||
88 | ], 500 |
||
89 | ); |
||
90 | } |
||
91 | |||
92 | return response()->json(['message' => 'Internal Firefly III Exception. See log files.', 'exception' => \get_class($exception)], 500); |
||
93 | } |
||
94 | |||
95 | if ($exception instanceof FireflyException || $exception instanceof ErrorException || $exception instanceof OAuthServerException) { |
||
96 | $isDebug = config('app.debug'); |
||
97 | |||
98 | return response()->view('errors.FireflyException', ['exception' => $exception, 'debug' => $isDebug], 500); |
||
99 | } |
||
100 | |||
101 | return parent::render($request, $exception); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Report or log an exception. |
||
106 | * |
||
107 | * This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
||
108 | * |
||
109 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's five its fine. |
||
110 | * |
||
111 | * @param \Exception $exception |
||
112 | * |
||
113 | * @return mixed|void |
||
114 | * |
||
115 | * @throws Exception |
||
116 | */ |
||
117 | public function report(Exception $exception) |
||
118 | { |
||
119 | |||
120 | $doMailError = config('firefly.send_error_message'); |
||
121 | // if the user wants us to mail: |
||
122 | if (true === $doMailError |
||
123 | // and if is one of these error instances |
||
124 | && ($exception instanceof FireflyException || $exception instanceof ErrorException)) { |
||
125 | $userData = [ |
||
126 | 'id' => 0, |
||
127 | 'email' => '[email protected]', |
||
128 | ]; |
||
129 | if (auth()->check()) { |
||
130 | $userData['id'] = auth()->user()->id; |
||
131 | $userData['email'] = auth()->user()->email; |
||
132 | } |
||
133 | $data = [ |
||
134 | 'class' => \get_class($exception), |
||
135 | 'errorMessage' => $exception->getMessage(), |
||
136 | 'time' => date('r'), |
||
137 | 'stackTrace' => $exception->getTraceAsString(), |
||
138 | 'file' => $exception->getFile(), |
||
139 | 'line' => $exception->getLine(), |
||
140 | 'code' => $exception->getCode(), |
||
141 | 'version' => config('firefly.version'), |
||
142 | 'url' => Request::fullUrl(), |
||
143 | 'userAgent' => Request::userAgent(), |
||
144 | 'json' => Request::acceptsJson(), |
||
145 | ]; |
||
146 | |||
147 | // create job that will mail. |
||
148 | $ipAddress = Request::ip() ?? '0.0.0.0'; |
||
149 | $job = new MailError($userData, (string)config('firefly.site_owner'), $ipAddress, $data); |
||
150 | dispatch($job); |
||
151 | } |
||
152 | |||
153 | parent::report($exception); |
||
154 | } |
||
155 | } |
||
156 |