firefly-iii/app/Exceptions/Handler.php

118 lines
3.5 KiB
PHP
Raw Normal View History

<?php
2017-10-21 01:40:00 -05:00
/**
* Handler.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
2017-09-14 10:40:02 -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;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
2017-09-12 15:14:43 -05:00
use Request;
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
{
2017-09-12 15:14:43 -05:00
if ($exception instanceof FireflyException || $exception instanceof ErrorException) {
$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-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
{
2017-09-12 15:14:43 -05:00
$doMailError = env('SEND_ERROR_MESSAGE', true);
if (($exception instanceof FireflyException || $exception instanceof ErrorException) && $doMailError) {
$userData = [
'id' => 0,
'email' => 'unknown@example.com',
];
if (auth()->check()) {
$userData['id'] = auth()->user()->id;
$userData['email'] = auth()->user()->email;
}
$data = [
'class' => get_class($exception),
'errorMessage' => $exception->getMessage(),
'time' => date('r'),
'stackTrace' => $exception->getTraceAsString(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'code' => $exception->getCode(),
'version' => config('firefly.version'),
];
// 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
}