2018-10-04 19:10:43 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-11 05:32:07 +02:00
|
|
|
*/
|
2018-10-04 19:10:43 +02:00
|
|
|
|
|
|
|
namespace App\Exceptions;
|
|
|
|
|
2023-04-26 09:41:30 +02:00
|
|
|
use Throwable;
|
|
|
|
use PDOException;
|
2021-05-19 00:08:37 +02:00
|
|
|
use App\Utils\Ninja;
|
2023-04-26 09:41:30 +02:00
|
|
|
use Sentry\State\Scope;
|
|
|
|
use Illuminate\Support\Arr;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\Http\Request;
|
2023-04-26 09:41:30 +02:00
|
|
|
use Sentry\Laravel\Integration;
|
2020-05-28 11:40:35 +02:00
|
|
|
use Illuminate\Support\Facades\Schema;
|
2023-04-26 09:41:30 +02:00
|
|
|
use GuzzleHttp\Exception\ConnectException;
|
|
|
|
use Illuminate\Auth\AuthenticationException;
|
2023-02-16 02:36:09 +01:00
|
|
|
use League\Flysystem\UnableToCreateDirectory;
|
2023-04-26 09:41:30 +02:00
|
|
|
use Illuminate\Session\TokenMismatchException;
|
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
|
|
|
use Illuminate\Queue\MaxAttemptsExceededException;
|
|
|
|
use Illuminate\Http\Exceptions\ThrottleRequestsException;
|
|
|
|
use Symfony\Component\Process\Exception\RuntimeException;
|
|
|
|
use Illuminate\Database\Eloquent\RelationNotFoundException;
|
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Symfony\Component\Console\Exception\CommandNotFoundException;
|
2019-10-10 03:01:38 +02:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2023-04-26 09:41:30 +02:00
|
|
|
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
|
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
|
2023-05-20 11:33:37 +02:00
|
|
|
use InvalidArgumentException;
|
2018-10-04 19:10:43 +02:00
|
|
|
|
|
|
|
class Handler extends ExceptionHandler
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* A list of the exception types that are not reported.
|
|
|
|
*
|
2023-05-20 11:33:37 +02:00
|
|
|
* @var array<int, class-string<Throwable>>
|
2018-10-04 19:10:43 +02:00
|
|
|
*/
|
|
|
|
protected $dontReport = [
|
2023-05-20 04:04:07 +02:00
|
|
|
// PDOException::class,
|
2023-05-20 11:33:37 +02:00
|
|
|
MaxAttemptsExceededException::class,
|
|
|
|
CommandNotFoundException::class,
|
|
|
|
ValidationException::class,
|
2023-05-20 04:04:07 +02:00
|
|
|
// ModelNotFoundException::class,
|
2023-05-20 11:33:37 +02:00
|
|
|
NotFoundHttpException::class,
|
2018-10-04 19:10:43 +02:00
|
|
|
];
|
|
|
|
|
2023-01-17 08:25:43 +01:00
|
|
|
protected $selfHostDontReport = [
|
2023-02-15 13:23:37 +01:00
|
|
|
FilePermissionsFailure::class,
|
|
|
|
PDOException::class,
|
|
|
|
MaxAttemptsExceededException::class,
|
|
|
|
CommandNotFoundException::class,
|
|
|
|
ValidationException::class,
|
|
|
|
ModelNotFoundException::class,
|
|
|
|
NotFoundHttpException::class,
|
|
|
|
UnableToCreateDirectory::class,
|
2023-04-26 09:41:30 +02:00
|
|
|
ConnectException::class,
|
2023-02-15 13:23:37 +01:00
|
|
|
RuntimeException::class,
|
2023-04-26 09:41:30 +02:00
|
|
|
InvalidArgumentException::class,
|
2023-02-19 10:35:54 +01:00
|
|
|
Aws\Exception\CredentialsException::class,
|
2023-01-17 08:25:43 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
protected $hostedDontReport = [
|
|
|
|
PDOException::class,
|
|
|
|
MaxAttemptsExceededException::class,
|
|
|
|
CommandNotFoundException::class,
|
|
|
|
ValidationException::class,
|
|
|
|
ModelNotFoundException::class,
|
|
|
|
NotFoundHttpException::class,
|
|
|
|
];
|
|
|
|
|
2018-10-04 19:10:43 +02:00
|
|
|
/**
|
|
|
|
* A list of the inputs that are never flashed for validation exceptions.
|
|
|
|
*
|
2023-05-05 06:15:50 +02:00
|
|
|
* @var array<1, string>
|
2018-10-04 19:10:43 +02:00
|
|
|
*/
|
|
|
|
protected $dontFlash = [
|
2022-06-21 11:59:19 +02:00
|
|
|
'current_password',
|
2018-10-04 19:10:43 +02:00
|
|
|
'password',
|
|
|
|
'password_confirmation',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Report or log an exception.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param Throwable $exception
|
2018-10-04 19:10:43 +02:00
|
|
|
* @return void
|
2020-10-28 11:10:49 +01:00
|
|
|
* @throws Throwable
|
2018-10-04 19:10:43 +02:00
|
|
|
*/
|
2020-09-06 11:38:10 +02:00
|
|
|
public function report(Throwable $exception)
|
2020-12-13 21:37:29 +01:00
|
|
|
{
|
2020-09-06 11:38:10 +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
|
|
|
|
2023-01-17 05:39:20 +01:00
|
|
|
if (Ninja::isHosted()) {
|
2023-05-05 06:15:50 +02:00
|
|
|
|
|
|
|
if($exception instanceof ThrottleRequestsException && class_exists(\Modules\Admin\Events\ThrottledExceptionRaised::class)) {
|
|
|
|
event(new \Modules\Admin\Events\ThrottledExceptionRaised(auth()->user()->account->key));
|
|
|
|
}
|
|
|
|
|
2022-12-02 23:55:00 +01:00
|
|
|
Integration::configureScope(function (Scope $scope): void {
|
2021-07-08 07:33:33 +02:00
|
|
|
$name = 'hosted@invoiceninja.com';
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (auth()->guard('contact') && auth()->guard('contact')->user()) {
|
|
|
|
$name = 'Contact = '.auth()->guard('contact')->user()->email;
|
2021-05-19 00:08:37 +02:00
|
|
|
$key = auth()->guard('contact')->user()->company->account->key;
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif (auth()->guard('user') && auth()->guard('user')->user()) {
|
|
|
|
$name = 'Admin = '.auth()->guard('user')->user()->email;
|
2021-05-19 00:08:37 +02:00
|
|
|
$key = auth()->user()->account->key;
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-05-19 00:08:37 +02:00
|
|
|
$key = 'Anonymous';
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-05-19 00:08:37 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$scope->setUser([
|
|
|
|
'id' => $key,
|
|
|
|
'email' => 'hosted@invoiceninja.com',
|
|
|
|
'name' => $name,
|
|
|
|
]);
|
|
|
|
});
|
2021-05-19 01:03:30 +02:00
|
|
|
|
2023-01-17 08:25:43 +01:00
|
|
|
if ($this->validException($exception) && $this->sentryShouldReport($exception)) {
|
2023-01-17 05:39:20 +01:00
|
|
|
Integration::captureUnhandledException($exception);
|
|
|
|
}
|
2023-01-17 08:25:43 +01:00
|
|
|
} elseif (app()->bound('sentry')) {
|
2022-12-02 23:55:00 +01:00
|
|
|
Integration::configureScope(function (Scope $scope): void {
|
2020-06-22 13:32:10 +02:00
|
|
|
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,
|
2020-09-06 11:38:10 +02:00
|
|
|
'email' => 'anonymous@example.com',
|
|
|
|
'name' => 'Anonymous User',
|
2020-06-22 13:32:10 +02:00
|
|
|
]);
|
2021-05-18 15:12:03 +02:00
|
|
|
} elseif (auth()->guard('user') && auth()->guard('user')->user() && auth()->user()->company() && auth()->user()->company()->account->report_errors) {
|
2020-06-22 13:32:10 +02:00
|
|
|
$scope->setUser([
|
|
|
|
'id' => auth()->user()->account->key,
|
2020-09-06 11:38:10 +02:00
|
|
|
'email' => 'anonymous@example.com',
|
|
|
|
'name' => 'Anonymous User',
|
2020-06-22 13:32:10 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
});
|
2020-03-29 14:22:14 +02:00
|
|
|
|
2023-01-17 08:25:43 +01:00
|
|
|
if ($this->validException($exception) && $this->sentryShouldReport($exception)) {
|
2022-12-02 23:55:00 +01:00
|
|
|
Integration::captureUnhandledException($exception);
|
2020-12-16 12:52:40 +01:00
|
|
|
}
|
2019-04-10 04:01:28 +02:00
|
|
|
}
|
2019-04-02 08:36:49 +02:00
|
|
|
|
2021-07-08 03:48:11 +02:00
|
|
|
parent::report($exception);
|
2018-10-04 19:10:43 +02:00
|
|
|
}
|
|
|
|
|
2020-12-16 12:52:40 +01:00
|
|
|
private function validException($exception)
|
2020-12-12 12:23:29 +01:00
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (strpos($exception->getMessage(), 'file_put_contents') !== false) {
|
2020-12-16 12:52:40 +01:00
|
|
|
return false;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2020-12-12 12:23:29 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (strpos($exception->getMessage(), 'Permission denied') !== false) {
|
2020-12-16 12:52:40 +01:00
|
|
|
return false;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($exception->getMessage(), 'flock') !== false) {
|
2020-12-16 12:52:40 +01:00
|
|
|
return false;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2020-12-13 00:20:03 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (strpos($exception->getMessage(), 'expects parameter 1 to be resource') !== false) {
|
2021-02-11 04:06:03 +01:00
|
|
|
return false;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-02-21 22:27:00 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (strpos($exception->getMessage(), 'fwrite()') !== false) {
|
2021-02-21 22:29:39 +01:00
|
|
|
return false;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($exception->getMessage(), 'LockableFile') !== false) {
|
2021-03-28 04:19:44 +02:00
|
|
|
return false;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-02-11 04:06:03 +01:00
|
|
|
|
2020-12-16 12:52:40 +01:00
|
|
|
return true;
|
2020-12-12 12:23:29 +01:00
|
|
|
}
|
|
|
|
|
2023-01-17 08:25:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the exception is in the "do not report" list.
|
|
|
|
*
|
|
|
|
* @param \Throwable $e
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function sentryShouldReport(Throwable $e)
|
|
|
|
{
|
2023-02-16 02:36:09 +01:00
|
|
|
if (Ninja::isHosted()) {
|
2023-01-17 08:25:43 +01:00
|
|
|
$dontReport = array_merge($this->hostedDontReport, $this->internalDontReport);
|
2023-02-16 02:36:09 +01:00
|
|
|
} else {
|
2023-01-17 08:25:43 +01:00
|
|
|
$dontReport = array_merge($this->selfHostDontReport, $this->internalDontReport);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-01-17 08:25:43 +01:00
|
|
|
|
|
|
|
return is_null(Arr::first($dontReport, fn ($type) => $e instanceof $type));
|
|
|
|
}
|
|
|
|
|
2018-10-04 19:10:43 +02:00
|
|
|
/**
|
|
|
|
* Render an exception into an HTTP response.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param Request $request
|
|
|
|
* @param Throwable $exception
|
|
|
|
* @throws Throwable
|
2018-10-04 19:10:43 +02:00
|
|
|
*/
|
2020-09-06 11:38:10 +02:00
|
|
|
public function render($request, Throwable $exception)
|
2018-10-04 19:10:43 +02:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($exception instanceof ModelNotFoundException && $request->expectsJson()) {
|
2020-02-17 20:07:31 +01:00
|
|
|
return response()->json(['message'=>$exception->getMessage()], 400);
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif ($exception instanceof InternalPDFFailure && $request->expectsJson()) {
|
2021-04-12 06:36:51 +02:00
|
|
|
return response()->json(['message' => $exception->getMessage()], 500);
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif ($exception instanceof PhantomPDFFailure && $request->expectsJson()) {
|
2021-04-12 06:36:51 +02:00
|
|
|
return response()->json(['message' => $exception->getMessage()], 500);
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif ($exception instanceof FilePermissionsFailure) {
|
2021-04-12 06:36:51 +02:00
|
|
|
return response()->json(['message' => $exception->getMessage()], 500);
|
2019-12-30 22:59:12 +01:00
|
|
|
} elseif ($exception instanceof ThrottleRequestsException && $request->expectsJson()) {
|
|
|
|
return response()->json(['message'=>'Too many requests'], 429);
|
2023-01-17 05:39:20 +01:00
|
|
|
// } elseif ($exception instanceof FatalThrowableError && $request->expectsJson()) {
|
|
|
|
// return response()->json(['message'=>'Fatal error'], 500); //@deprecated
|
2023-01-31 22:43:32 +01:00
|
|
|
} elseif ($exception instanceof AuthorizationException && $request->expectsJson()) {
|
2022-11-23 00:01:37 +01:00
|
|
|
return response()->json(['message'=> $exception->getMessage()], 401);
|
2020-10-28 11:10:49 +01:00
|
|
|
} elseif ($exception instanceof 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'),
|
2020-09-06 11:38:10 +02:00
|
|
|
'message-type' => 'danger', ]);
|
2019-12-30 22:59:12 +01:00
|
|
|
} elseif ($exception instanceof NotFoundHttpException && $request->expectsJson()) {
|
|
|
|
return response()->json(['message'=>'Route does not exist'], 404);
|
|
|
|
} elseif ($exception instanceof MethodNotAllowedHttpException && $request->expectsJson()) {
|
2021-07-27 14:20:32 +02:00
|
|
|
return response()->json(['message'=>'Method not supported for this route'], 404);
|
2019-12-30 22:59:12 +01:00
|
|
|
} elseif ($exception instanceof ValidationException && $request->expectsJson()) {
|
2019-10-10 03:01:38 +02:00
|
|
|
return response()->json(['message' => 'The given data was invalid.', 'errors' => $exception->validator->getMessageBag()], 422);
|
2019-12-30 22:59:12 +01:00
|
|
|
} elseif ($exception instanceof RelationNotFoundException && $request->expectsJson()) {
|
2022-10-31 07:05:05 +01:00
|
|
|
return response()->json(['message' => "Relation `{$exception->relation}` is not a valid include."], 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) {
|
2021-07-08 10:13:07 +02:00
|
|
|
return response()->json(['message' => $exception->getMessage()], 400);
|
2021-08-15 08:21:18 +02:00
|
|
|
} elseif ($exception instanceof StripeConnectFailure) {
|
|
|
|
return response()->json(['message' => $exception->getMessage()], 400);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-10-24 00:07:55 +02:00
|
|
|
|
2018-10-04 19:10:43 +02:00
|
|
|
return parent::render($request, $exception);
|
|
|
|
}
|
2018-10-15 07:00:48 +02:00
|
|
|
|
|
|
|
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);
|
2018-10-15 07:00:48 +02:00
|
|
|
|
|
|
|
switch ($guard) {
|
2023-02-16 02:36:09 +01:00
|
|
|
case 'contact':
|
2019-07-16 04:38:11 +02:00
|
|
|
$login = 'client.login';
|
2018-10-15 07:00:48 +02:00
|
|
|
break;
|
|
|
|
case 'user':
|
|
|
|
$login = 'login';
|
|
|
|
break;
|
2022-06-15 07:20:00 +02:00
|
|
|
case 'vendor':
|
|
|
|
$login = 'vendor.catchall';
|
|
|
|
break;
|
2018-10-15 07:00:48 +02:00
|
|
|
default:
|
|
|
|
$login = 'default';
|
|
|
|
break;
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2018-10-15 07:00:48 +02:00
|
|
|
return redirect()->guest(route($login));
|
|
|
|
}
|
2018-10-04 19:10:43 +02:00
|
|
|
}
|