2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Exceptions;
|
2015-03-12 01:44:39 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
use Exception;
|
2017-11-14 09:58:08 +01:00
|
|
|
use Illuminate\Auth\AuthenticationException;
|
2016-04-11 10:33:25 +02:00
|
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
|
|
|
use Illuminate\Foundation\Validation\ValidationException;
|
|
|
|
use Illuminate\Http\Exception\HttpResponseException;
|
|
|
|
use Illuminate\Support\Facades\Response;
|
2017-03-20 21:16:13 +01:00
|
|
|
use Illuminate\Session\TokenMismatchException;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Redirect;
|
2016-04-11 10:33:25 +02:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
2016-08-17 11:51:26 +02:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Utils;
|
2018-09-20 13:39:45 +02:00
|
|
|
use Request;
|
2016-09-20 13:56:58 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Class Handler.
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
|
|
|
class Handler extends ExceptionHandler
|
|
|
|
{
|
2017-01-30 17:05:31 +01:00
|
|
|
/**
|
|
|
|
* A list of the exception types that should not be reported.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $dontReport = [
|
2017-03-20 21:16:13 +01:00
|
|
|
TokenMismatchException::class,
|
2017-12-11 15:42:14 +01:00
|
|
|
ModelNotFoundException::class,
|
2018-01-08 08:32:28 +01:00
|
|
|
ValidationException::class,
|
|
|
|
\Illuminate\Validation\ValidationException::class,
|
2017-03-20 21:16:13 +01:00
|
|
|
//AuthorizationException::class,
|
|
|
|
//HttpException::class,
|
2017-01-30 17:05:31 +01:00
|
|
|
];
|
2015-03-12 01:44:39 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* Report or log an exception.
|
|
|
|
*
|
|
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
|
|
|
|
*
|
2017-01-30 20:40:43 +01:00
|
|
|
* @param \Exception $e
|
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return bool|void
|
|
|
|
*/
|
2017-01-30 17:05:31 +01:00
|
|
|
public function report(Exception $e)
|
|
|
|
{
|
2017-03-20 21:16:13 +01:00
|
|
|
if (! $this->shouldReport($e)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-21 16:59:54 +01:00
|
|
|
// if these classes don't exist the install is broken, maybe due to permissions
|
|
|
|
if (! class_exists('Utils') || ! class_exists('Crawler')) {
|
2017-12-29 13:26:23 +01:00
|
|
|
return parent::report($e);
|
|
|
|
}
|
|
|
|
|
2018-02-21 16:59:54 +01:00
|
|
|
if (\Crawler::isCrawler()) {
|
2017-09-06 15:45:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-11 10:33:25 +02:00
|
|
|
// don't show these errors in the logs
|
2016-08-17 11:51:26 +02:00
|
|
|
if ($e instanceof NotFoundHttpException) {
|
2017-03-21 10:18:36 +01:00
|
|
|
// The logo can take a few seconds to get synced between servers
|
|
|
|
// TODO: remove once we're using cloud storage for logos
|
|
|
|
if (Utils::isNinja() && strpos(request()->url(), '/logo/') !== false) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-14 20:18:55 +02:00
|
|
|
// Log 404s to a separate file
|
2017-08-16 09:28:40 +02:00
|
|
|
$errorStr = date('Y-m-d h:i:s') . ' ' . $e->getMessage() . ' URL:' . request()->url() . "\n" . json_encode(Utils::prepareErrorData('PHP')) . "\n\n";
|
2018-03-22 12:05:39 +01:00
|
|
|
if (config('app.log') == 'single') {
|
|
|
|
@file_put_contents(storage_path('logs/not-found.log'), $errorStr, FILE_APPEND);
|
|
|
|
} else {
|
|
|
|
Utils::logError('[not found] ' . $errorStr);
|
|
|
|
}
|
2017-06-14 20:18:55 +02:00
|
|
|
return false;
|
2016-08-17 11:51:26 +02:00
|
|
|
} elseif ($e instanceof HttpResponseException) {
|
2016-04-11 10:33:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-08-17 11:51:26 +02:00
|
|
|
|
2017-06-13 19:19:47 +02:00
|
|
|
if (! Utils::isTravis()) {
|
2015-09-25 11:57:40 +02:00
|
|
|
Utils::logError(Utils::getErrorString($e));
|
2017-08-16 09:28:40 +02:00
|
|
|
$stacktrace = date('Y-m-d h:i:s') . ' ' . $e->getMessage() . ': ' . $e->getTraceAsString() . "\n\n";
|
2018-03-22 12:05:39 +01:00
|
|
|
if (config('app.log') == 'single') {
|
|
|
|
@file_put_contents(storage_path('logs/stacktrace.log'), $stacktrace, FILE_APPEND);
|
|
|
|
} else {
|
2018-03-23 09:09:31 +01:00
|
|
|
Utils::logError('[stacktrace] ' . $stacktrace);
|
2018-03-22 12:05:39 +01:00
|
|
|
}
|
2015-09-25 11:57:40 +02:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return parent::report($e);
|
|
|
|
}
|
|
|
|
}
|
2015-03-12 01:44:39 +01:00
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
/**
|
|
|
|
* Render an exception into an HTTP response.
|
|
|
|
*
|
2017-01-30 20:40:43 +01:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Exception $e
|
|
|
|
*
|
2017-01-30 17:05:31 +01:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function render($request, Exception $e)
|
|
|
|
{
|
2018-09-20 13:39:45 +02:00
|
|
|
$value = Request::header('X-Ninja-Token');
|
|
|
|
|
2016-11-05 02:32:06 +01:00
|
|
|
if ($e instanceof ModelNotFoundException) {
|
2018-09-20 13:39:45 +02:00
|
|
|
|
|
|
|
if( isset($value) && strlen($value) > 1 ){
|
|
|
|
$headers = \App\Libraries\Utils::getApiHeaders();
|
|
|
|
$response = json_encode(['message' => 'record does not exist'], JSON_PRETTY_PRINT);
|
|
|
|
|
|
|
|
return Response::make($response, 404, $headers);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return Redirect::to('/');
|
2017-01-30 17:05:31 +01:00
|
|
|
}
|
2017-03-20 21:16:13 +01:00
|
|
|
|
2017-12-29 13:26:23 +01:00
|
|
|
if (! class_exists('Utils')) {
|
|
|
|
return parent::render($request, $e);
|
|
|
|
}
|
|
|
|
|
2017-03-20 21:16:13 +01:00
|
|
|
if ($e instanceof TokenMismatchException) {
|
2017-03-21 09:13:48 +01:00
|
|
|
if (! in_array($request->path(), ['get_started', 'save_sidebar_state'])) {
|
2016-02-28 19:54:31 +01:00
|
|
|
// https://gist.github.com/jrmadsen67/bd0f9ad0ef1ed6bb594e
|
|
|
|
return redirect()
|
2016-07-21 14:35:23 +02:00
|
|
|
->back()
|
|
|
|
->withInput($request->except('password', '_token'))
|
|
|
|
->with([
|
2017-01-30 20:40:43 +01:00
|
|
|
'warning' => trans('texts.token_expired'),
|
2016-07-21 14:35:23 +02:00
|
|
|
]);
|
2016-02-28 19:54:31 +01:00
|
|
|
}
|
2015-06-16 21:35:35 +02:00
|
|
|
}
|
2015-10-20 19:12:34 +02:00
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
if ($this->isHttpException($e)) {
|
|
|
|
switch ($e->getStatusCode()) {
|
2016-11-04 20:20:33 +01:00
|
|
|
// not found
|
|
|
|
case 404:
|
2017-01-30 17:05:31 +01:00
|
|
|
if ($request->header('X-Ninja-Token') != '') {
|
2016-11-04 20:20:33 +01:00
|
|
|
//API request which has hit a route which does not exist
|
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
$error['error'] = ['message' => 'Route does not exist'];
|
2016-11-04 20:20:33 +01:00
|
|
|
$error = json_encode($error, JSON_PRETTY_PRINT);
|
|
|
|
$headers = Utils::getApiHeaders();
|
|
|
|
|
|
|
|
return response()->make($error, 404, $headers);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// internal error
|
|
|
|
case '500':
|
2017-01-30 17:05:31 +01:00
|
|
|
if ($request->header('X-Ninja-Token') != '') {
|
2016-11-04 20:20:33 +01:00
|
|
|
//API request which produces 500 error
|
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
$error['error'] = ['message' => 'Internal Server Error'];
|
2016-11-04 20:20:33 +01:00
|
|
|
$error = json_encode($error, JSON_PRETTY_PRINT);
|
|
|
|
$headers = Utils::getApiHeaders();
|
|
|
|
|
|
|
|
return response()->make($error, 500, $headers);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-29 07:23:30 +02:00
|
|
|
// In production, except for maintenance mode, we'll show a custom error screen
|
2016-04-18 19:17:02 +02:00
|
|
|
if (Utils::isNinjaProd()
|
2017-01-30 20:40:43 +01:00
|
|
|
&& ! Utils::isDownForMaintenance()
|
|
|
|
&& ! ($e instanceof HttpResponseException)
|
2018-01-07 11:49:50 +01:00
|
|
|
&& ! ($e instanceof \Illuminate\Validation\ValidationException)
|
2017-01-30 20:40:43 +01:00
|
|
|
&& ! ($e instanceof ValidationException)) {
|
2015-04-28 22:13:52 +02:00
|
|
|
$data = [
|
|
|
|
'error' => get_class($e),
|
|
|
|
'hideHeader' => true,
|
|
|
|
];
|
2016-08-17 11:51:26 +02:00
|
|
|
|
2017-02-19 13:26:59 +01:00
|
|
|
return response()->view('error', $data, 500);
|
2015-04-28 22:13:52 +02:00
|
|
|
} else {
|
|
|
|
return parent::render($request, $e);
|
|
|
|
}
|
2017-01-30 17:05:31 +01:00
|
|
|
}
|
2017-11-14 09:58:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert an authentication exception into an unauthenticated response.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
protected function unauthenticated($request, AuthenticationException $exception)
|
|
|
|
{
|
|
|
|
if ($request->expectsJson()) {
|
|
|
|
return response()->json(['error' => 'Unauthenticated.'], 401);
|
|
|
|
}
|
|
|
|
|
|
|
|
$guard = array_get($exception->guards(), 0);
|
|
|
|
|
|
|
|
switch ($guard) {
|
|
|
|
case 'client':
|
|
|
|
$url = '/client/login';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$url = '/login';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->guest($url);
|
|
|
|
}
|
2015-03-12 01:44:39 +01:00
|
|
|
}
|