firefly-iii/app/Exceptions/Handler.php

173 lines
5.8 KiB
PHP
Raw Normal View History

<?php
2018-05-11 03:08:34 -05:00
2017-10-21 01:40:00 -05:00
/**
* Handler.php
2018-05-11 03:08:34 -05:00
* Copyright (c) 2018 thegrumpydictator@gmail.com
2017-10-21 01:40:00 -05:00
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-10-21 01:40:00 -05:00
*/
2017-09-14 10:40:02 -05:00
2018-05-11 03:08:34 -05:00
declare(strict_types=1);
namespace FireflyIII\Exceptions;
2015-02-05 21:39:52 -06:00
2017-09-12 15:14:43 -05:00
use ErrorException;
2015-02-05 21:39:52 -06:00
use Exception;
2017-09-12 15:14:43 -05:00
use FireflyIII\Jobs\MailError;
2018-02-17 07:14:26 -06:00
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
2018-02-17 07:14:26 -06:00
use Illuminate\Validation\ValidationException;
use League\OAuth2\Server\Exception\OAuthServerException;
2017-09-12 15:14:43 -05:00
use Request;
2018-02-10 02:57:56 -06:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2018-02-17 07:14:26 -06:00
2017-12-17 07:30:53 -06:00
/**
* Class Handler
*/
2015-02-11 00:35:10 -06:00
class Handler extends ExceptionHandler
{
/**
2017-09-12 15:14:43 -05:00
* A list of the inputs that are never flashed for validation exceptions.
2015-02-11 00:35:10 -06:00
*
* @var array
*/
2017-09-12 15:14:43 -05:00
protected $dontFlash
= [
'password',
'password_confirmation',
];
2015-02-11 00:35:10 -06:00
/**
2017-09-12 15:14:43 -05:00
* A list of the exception types that are not reported.
*
2017-09-09 14:57:45 -05:00
* @var array
2015-02-11 00:35:10 -06:00
*/
2017-09-12 15:14:43 -05:00
protected $dontReport
= [
];
2016-02-11 07:17:11 -06:00
/**
2017-09-12 15:14:43 -05:00
* Render an exception into an HTTP response.
2016-02-11 07:17:11 -06:00
*
2017-11-15 05:25:49 -06:00
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
2016-02-11 07:17:11 -06:00
*
2017-09-12 15:14:43 -05:00
* @return \Illuminate\Http\Response
2016-02-11 07:17:11 -06:00
*/
2017-09-12 15:14:43 -05:00
public function render($request, Exception $exception)
2016-02-11 07:17:11 -06:00
{
2018-02-13 11:24:06 -06:00
if ($exception instanceof ValidationException && $request->expectsJson()) {
// ignore it: controller will handle it.
return parent::render($request, $exception);
}
2018-02-10 02:57:56 -06:00
if ($exception instanceof NotFoundHttpException && $request->expectsJson()) {
// JSON error:
return response()->json(['message' => 'Resource not found', 'exception' => 'NotFoundHttpException'], 404);
}
2018-02-17 07:14:26 -06:00
if ($exception instanceof AuthenticationException && $request->expectsJson()) {
// somehow Laravel handler does not catch this:
2018-02-17 07:14:26 -06:00
return response()->json(['message' => 'Unauthenticated', 'exception' => 'AuthenticationException'], 401);
}
if ($exception instanceof OAuthServerException && $request->expectsJson()) {
// somehow Laravel handler does not catch this:
return response()->json(['message' => $exception->getMessage(), 'exception' => 'OAuthServerException'], 401);
}
if ($request->expectsJson()) {
2018-03-04 01:22:32 -06:00
$isDebug = config('app.debug', false);
if ($isDebug) {
return response()->json(
[
'message' => $exception->getMessage(),
2018-04-27 23:23:13 -05:00
'exception' => \get_class($exception),
'line' => $exception->getLine(),
'file' => $exception->getFile(),
'trace' => $exception->getTrace(),
], 500
);
}
2018-04-27 23:23:13 -05:00
return response()->json(['message' => 'Internal Firefly III Exception. See log files.', 'exception' => \get_class($exception)], 500);
2018-02-10 02:57:56 -06:00
}
if ($exception instanceof FireflyException || $exception instanceof ErrorException || $exception instanceof OAuthServerException) {
2017-09-12 15:14:43 -05:00
$isDebug = env('APP_DEBUG', false);
return response()->view('errors.FireflyException', ['exception' => $exception, 'debug' => $isDebug], 500);
}
return parent::render($request, $exception);
2015-02-11 00:35:10 -06:00
}
2016-09-15 23:40:45 -05:00
/**
2017-09-12 15:14:43 -05:00
* Report or log an exception.
2016-10-06 22:44:21 -05:00
*
2017-09-12 15:14:43 -05:00
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
2017-09-16 00:41:03 -05:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's five its fine.
2017-09-12 15:14:43 -05:00
*
2017-11-15 05:25:49 -06:00
* @param \Exception $exception
2017-11-17 22:46:19 -06:00
*
* @return mixed|void
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws Exception
2016-09-15 23:40:45 -05:00
*/
2017-09-12 15:14:43 -05:00
public function report(Exception $exception)
2016-09-15 23:40:45 -05:00
{
2018-05-26 06:14:51 -05:00
2017-09-12 15:14:43 -05:00
$doMailError = env('SEND_ERROR_MESSAGE', true);
if (
(
$exception instanceof FireflyException
|| $exception instanceof ErrorException
|| $exception instanceof OAuthServerException
2018-05-26 06:14:51 -05:00
|| $exception instanceof AuthenticationException
)
&& $doMailError) {
2018-05-26 06:14:51 -05:00
2017-09-12 15:14:43 -05:00
$userData = [
'id' => 0,
'email' => 'unknown@example.com',
];
if (auth()->check()) {
$userData['id'] = auth()->user()->id;
$userData['email'] = auth()->user()->email;
}
$data = [
2018-04-27 23:23:13 -05:00
'class' => \get_class($exception),
2017-09-12 15:14:43 -05:00
'errorMessage' => $exception->getMessage(),
'time' => date('r'),
'stackTrace' => $exception->getTraceAsString(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'code' => $exception->getCode(),
'version' => config('firefly.version'),
2018-05-26 06:14:51 -05:00
'url' => Request::fullUrl(),
'userAgent' => Request::userAgent(),
'json' => Request::acceptsJson(),
2017-09-12 15:14:43 -05:00
];
// create job that will mail.
$ipAddress = Request::ip() ?? '0.0.0.0';
$job = new MailError($userData, env('SITE_OWNER', ''), $ipAddress, $data);
dispatch($job);
}
parent::report($exception);
2016-09-15 23:40:45 -05:00
}
2015-02-05 21:39:52 -06:00
}