1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-06 03:02:34 +01:00
invoiceninja/app/Exceptions/Handler.php

162 lines
6.3 KiB
PHP
Raw Normal View History

2018-10-04 19:10:43 +02:00
<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
2018-10-04 19:10:43 +02:00
namespace App\Exceptions;
2020-06-10 07:21:11 +02:00
use App\Exceptions\GenericPaymentDriverFailure;
2018-10-04 19:10:43 +02:00
use Exception;
2019-10-03 02:17:29 +02:00
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
2019-04-02 08:36:49 +02:00
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
2020-05-28 11:40:35 +02:00
use Illuminate\Database\Eloquent\RelationNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Exceptions\ThrottleRequestsException;
2019-10-01 03:56:48 +02:00
use Illuminate\Support\Arr;
2020-05-28 11:40:35 +02:00
use Illuminate\Support\Facades\Schema;
2019-10-10 03:01:38 +02:00
use Illuminate\Validation\ValidationException;
use function Sentry\configureScope;
2020-05-28 11:40:35 +02:00
use Sentry\State\Scope;
2019-04-23 08:19:45 +02:00
use Symfony\Component\Debug\Exception\FatalThrowableError;
2019-10-08 13:14:23 +02:00
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
2019-10-10 03:01:38 +02:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;
2018-10-04 19:10:43 +02:00
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
\PDOException::class,
2020-08-19 08:35:35 +02:00
\Swift_TransportException::class,
\Illuminate\Queue\MaxAttemptsExceededException::class,
\Symfony\Component\Console\Exception\CommandNotFoundException::class,
2018-10-04 19:10:43 +02:00
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* @param \Throwable $exception
2018-10-04 19:10:43 +02:00
* @return void
*/
public function report(Throwable $exception)
2018-10-04 19:10:43 +02:00
{
if (! Schema::hasTable('accounts')) {
info('account table not found');
2020-05-28 11:40:35 +02:00
return;
2020-06-22 05:07:58 +02:00
}
2020-05-28 11:40:35 +02:00
2019-04-10 04:01:28 +02:00
if (app()->bound('sentry') && $this->shouldReport($exception)) {
2020-06-22 13:32:10 +02:00
app('sentry')->configureScope(function (Scope $scope): void {
if (auth()->guard('contact') && auth()->guard('contact')->user() && auth()->guard('contact')->user()->company->account->report_errors) {
$scope->setUser([
'id' => auth()->guard('contact')->user()->company->account->key,
'email' => 'anonymous@example.com',
'name' => 'Anonymous User',
2020-06-22 13:32:10 +02:00
]);
} elseif (auth()->guard('user') && auth()->guard('user')->user() && auth()->user()->company() && auth()->user()->company()->account->report_errors) {
$scope->setUser([
'id' => auth()->user()->account->key,
'email' => 'anonymous@example.com',
'name' => 'Anonymous User',
2020-06-22 13:32:10 +02:00
]);
}
});
// app('sentry')->setRelease(config('ninja.app_version'));
2019-04-10 04:01:28 +02:00
app('sentry')->captureException($exception);
}
2019-04-02 08:36:49 +02:00
2019-04-10 04:01:28 +02:00
parent::report($exception);
2018-10-04 19:10:43 +02:00
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
2018-10-04 19:10:43 +02:00
* @return \Illuminate\Http\Response
*/
public function render($request, Throwable $exception)
2018-10-04 19:10:43 +02:00
{
if ($exception instanceof ModelNotFoundException && $request->expectsJson()) {
return response()->json(['message'=>$exception->getMessage()], 400);
} elseif ($exception instanceof ThrottleRequestsException && $request->expectsJson()) {
return response()->json(['message'=>'Too many requests'], 429);
} elseif ($exception instanceof FatalThrowableError && $request->expectsJson()) {
return response()->json(['message'=>'Fatal error'], 500);
} elseif ($exception instanceof AuthorizationException) {
return response()->json(['message'=>'You are not authorized to view or perform this action'], 401);
} elseif ($exception instanceof \Illuminate\Session\TokenMismatchException) {
2019-08-13 23:16:31 +02:00
return redirect()
->back()
2019-08-13 23:41:02 +02:00
->withInput($request->except('password', 'password_confirmation', '_token'))
2019-08-13 23:16:31 +02:00
->with([
'message' => ctrans('texts.token_expired'),
'message-type' => 'danger', ]);
} elseif ($exception instanceof NotFoundHttpException && $request->expectsJson()) {
return response()->json(['message'=>'Route does not exist'], 404);
} elseif ($exception instanceof MethodNotAllowedHttpException && $request->expectsJson()) {
return response()->json(['message'=>'Method not support for this route'], 404);
} elseif ($exception instanceof ValidationException && $request->expectsJson()) {
info(print_r($exception->validator->getMessageBag(), 1));
2019-10-10 03:01:38 +02:00
return response()->json(['message' => 'The given data was invalid.', 'errors' => $exception->validator->getMessageBag()], 422);
} elseif ($exception instanceof RelationNotFoundException && $request->expectsJson()) {
return response()->json(['message' => $exception->getMessage()], 400);
2020-06-10 07:21:11 +02:00
} elseif ($exception instanceof GenericPaymentDriverFailure && $request->expectsJson()) {
return response()->json(['message' => $exception->getMessage()], 400);
} elseif ($exception instanceof GenericPaymentDriverFailure) {
$data['message'] = $exception->getMessage();
2020-08-13 04:30:45 +02:00
//dd($data);
// return view('errors.layout', $data);
}
2019-08-13 23:41:02 +02:00
2018-10-04 19:10:43 +02:00
return parent::render($request, $exception);
}
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
2019-10-01 03:56:48 +02:00
$guard = Arr::get($exception->guards(), 0);
switch ($guard) {
case 'contact':
$login = 'client.login';
break;
case 'user':
$login = 'login';
break;
default:
$login = 'default';
break;
}
return redirect()->guest(route($login));
}
2018-10-04 19:10:43 +02:00
}