firefly-iii/app/Exceptions/Handler.php

121 lines
3.5 KiB
PHP
Raw Normal View History

<?php
2016-05-20 04:59:54 -05:00
/**
* Handler.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
namespace FireflyIII\Exceptions;
2015-02-05 21:39:52 -06:00
2016-02-11 01:11:26 -06:00
use ErrorException;
2015-02-05 21:39:52 -06:00
use Exception;
use FireflyIII\Jobs\MailError;
use Illuminate\Auth\Access\AuthorizationException;
2016-09-15 23:40:45 -05:00
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
2016-09-15 23:40:45 -05:00
use Illuminate\Session\TokenMismatchException;
2016-09-17 02:50:40 -05:00
use Illuminate\Validation\ValidationException as ValException;
use Symfony\Component\HttpKernel\Exception\HttpException;
2016-02-12 07:15:49 -06:00
/**
* Class Handler
*
* @package FireflyIII\Exceptions
*/
2015-02-11 00:35:10 -06:00
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport
= [
2016-09-15 23:40:45 -05:00
AuthenticationException::class,
AuthorizationException::class,
HttpException::class,
ModelNotFoundException::class,
2016-09-15 23:40:45 -05:00
TokenMismatchException::class,
2016-09-17 02:50:40 -05:00
ValException::class,
];
2015-02-05 21:39:52 -06:00
2015-02-11 00:35:10 -06:00
/**
* Render an exception into an HTTP response.
2015-02-11 00:35:10 -06:00
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
*
* @return \Illuminate\Http\Response
2015-02-11 00:35:10 -06:00
*/
public function render($request, Exception $exception)
2015-02-11 00:35:10 -06:00
{
2016-02-11 07:17:11 -06: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
*
* @return void
*/
public function report(Exception $exception)
{
2016-02-11 01:11:26 -06:00
if ($exception instanceof FireflyException || $exception instanceof ErrorException) {
2016-04-28 03:59:36 -05:00
$userData = [
'id' => 0,
'email' => 'unknown@example.com',
];
2016-09-16 05:07:45 -05:00
if (auth()->check()) {
2016-09-16 05:15:58 -05:00
$userData['id'] = auth()->user()->id;
$userData['email'] = auth()->user()->email;
2016-04-28 03:59:36 -05:00
}
$data = [
'class' => get_class($exception),
'errorMessage' => $exception->getMessage(),
2016-02-24 13:42:05 -06:00
'time' => date('r'),
'stackTrace' => $exception->getTraceAsString(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'code' => $exception->getCode(),
];
2016-02-10 08:18:13 -06:00
// create job that will mail.
2016-09-15 23:40:45 -05:00
$ip = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
2016-09-16 02:02:35 -05:00
$job = new MailError($userData, env('SITE_OWNER', ''), $ip, $data);
dispatch($job);
2016-02-05 22:01:34 -06:00
}
parent::report($exception);
2015-02-11 00:35:10 -06:00
}
2016-09-15 23:40:45 -05:00
/**
* Convert an authentication exception into an unauthenticated response.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
2016-09-25 01:24:03 -05:00
protected function unauthenticated($request)
2016-09-15 23:40:45 -05:00
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
return redirect()->guest('login');
}
2015-02-05 21:39:52 -06:00
}