Files
firefly-iii/app/Exceptions/Handler.php

129 lines
3.8 KiB
PHP
Raw Normal View History

<?php
2016-05-20 11:59:54 +02:00
/**
* Handler.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
2016-05-20 11:59:54 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Exceptions;
2015-02-06 04:39:52 +01:00
2016-02-11 08:11:26 +01:00
use ErrorException;
2015-02-06 04:39:52 +01:00
use Exception;
use FireflyIII\Jobs\MailError;
use Illuminate\Auth\Access\AuthorizationException;
2016-09-16 06:40:45 +02:00
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
2016-09-16 06:40:45 +02:00
use Illuminate\Session\TokenMismatchException;
2016-09-17 09:50:40 +02:00
use Illuminate\Validation\ValidationException as ValException;
2016-12-30 13:47:23 +01:00
use Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
2016-02-12 14:15:49 +01:00
/**
* Class Handler
*
* @package FireflyIII\Exceptions
*/
2015-02-11 07:35:10 +01:00
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport
= [
2016-09-16 06:40:45 +02:00
AuthenticationException::class,
AuthorizationException::class,
HttpException::class,
ModelNotFoundException::class,
2016-09-16 06:40:45 +02:00
TokenMismatchException::class,
2016-09-17 09:50:40 +02:00
ValException::class,
];
2015-02-06 04:39:52 +01:00
2015-02-11 07:35:10 +01:00
/**
* Render an exception into an HTTP response.
2015-02-11 07:35:10 +01:00
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
*
* @return \Illuminate\Http\Response
2015-02-11 07:35:10 +01:00
*/
public function render($request, Exception $exception)
2015-02-11 07:35:10 +01:00
{
2016-02-11 14:17:11 +01: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);
}
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param Exception $exception
2016-12-27 19:59:56 +01:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's exactly five.
2016-02-11 14:17:11 +01:00
*
* @return void
*/
public function report(Exception $exception)
{
$doMailError = env('SEND_ERROR_MESSAGE', true);
if (($exception instanceof FireflyException || $exception instanceof ErrorException) && $doMailError) {
2016-04-28 10:59:36 +02:00
$userData = [
'id' => 0,
'email' => 'unknown@example.com',
];
2016-09-16 12:07:45 +02:00
if (auth()->check()) {
2016-09-16 12:15:58 +02:00
$userData['id'] = auth()->user()->id;
$userData['email'] = auth()->user()->email;
2016-04-28 10:59:36 +02:00
}
$data = [
'class' => get_class($exception),
'errorMessage' => $exception->getMessage(),
2016-02-24 20:42:05 +01:00
'time' => date('r'),
'stackTrace' => $exception->getTraceAsString(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'code' => $exception->getCode(),
'version' => config('firefly.version'),
];
2016-02-10 15:18:13 +01:00
// create job that will mail.
2016-12-28 10:00:58 +01:00
$ipAddress = Request::ip() ?? '0.0.0.0';
2016-12-27 19:59:56 +01:00
$job = new MailError($userData, env('SITE_OWNER', ''), $ipAddress, $data);
dispatch($job);
2016-02-06 05:01:34 +01:00
}
parent::report($exception);
2015-02-11 07:35:10 +01:00
}
2016-09-16 06:40:45 +02:00
/**
* Convert an authentication exception into an unauthenticated response.
*
2017-01-14 19:43:33 +01:00
* @param $request
2016-10-07 05:44:21 +02:00
*
2017-01-14 19:43:33 +01:00
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
2016-09-16 06:40:45 +02:00
*/
2016-09-25 08:24:03 +02:00
protected function unauthenticated($request)
2016-09-16 06:40:45 +02:00
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
return redirect()->guest('login');
}
2015-02-06 04:39:52 +01:00
}