1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Exceptions/Handler.php

137 lines
4.1 KiB
PHP
Raw Normal View History

2015-03-12 01:44:39 +01:00
<?php namespace App\Exceptions;
use Braintree\Util;
use Illuminate\Support\Facades\Response;
2015-06-16 21:35:35 +02:00
use Redirect;
2015-04-13 09:54:51 +02:00
use Utils;
2015-03-12 01:44:39 +01:00
use Exception;
use Crawler;
2015-03-12 01:44:39 +01:00
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Exception\HttpResponseException;
2016-04-11 10:33:25 +02:00
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Foundation\Validation\ValidationException;
2015-03-12 01:44:39 +01:00
/**
* Class Handler
*/
class Handler extends ExceptionHandler
{
2015-03-12 01:44:39 +01:00
2016-07-21 14:35:23 +02:00
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
2016-04-11 10:33:25 +02:00
AuthorizationException::class,
HttpException::class,
ModelNotFoundException::class,
ValidationException::class,
2016-07-21 14:35:23 +02:00
];
2015-03-12 01:44:39 +01:00
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @return bool|void
*/
2016-07-21 14:35:23 +02:00
public function report(Exception $e)
{
2016-04-11 10:33:25 +02:00
// don't show these errors in the logs
if ($e instanceof NotFoundHttpException) {
if (Crawler::isCrawler()) {
return false;
}
} elseif ($e instanceof HttpResponseException) {
2016-04-11 10:33:25 +02:00
return false;
}
2016-07-21 14:35:23 +02:00
if (Utils::isNinja() && ! Utils::isTravis()) {
2015-09-25 11:57:40 +02:00
Utils::logError(Utils::getErrorString($e));
return false;
} else {
return parent::report($e);
}
}
2015-03-12 01:44:39 +01:00
2016-07-21 14:35:23 +02:00
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
2016-11-05 02:32:06 +01:00
if ($e instanceof ModelNotFoundException) {
return Redirect::to('/');
} if ($e instanceof \Illuminate\Session\TokenMismatchException) {
// prevent loop since the page auto-submits
if ($request->path() != 'get_started') {
// https://gist.github.com/jrmadsen67/bd0f9ad0ef1ed6bb594e
return redirect()
2016-07-21 14:35:23 +02:00
->back()
->withInput($request->except('password', '_token'))
->with([
'warning' => trans('texts.token_expired')
]);
}
2015-06-16 21:35:35 +02:00
}
2015-10-20 19:12:34 +02:00
if($this->isHttpException($e))
{
switch ($e->getStatusCode())
{
// not found
case 404:
if($request->header('X-Ninja-Token') != '') {
//API request which has hit a route which does not exist
$error['error'] = ['message'=>'Route does not exist'];
$error = json_encode($error, JSON_PRETTY_PRINT);
$headers = Utils::getApiHeaders();
return response()->make($error, 404, $headers);
}
break;
// internal error
case '500':
if($request->header('X-Ninja-Token') != '') {
//API request which produces 500 error
$error['error'] = ['message'=>'Internal Server Error'];
$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()
&& !Utils::isDownForMaintenance()
&& !($e instanceof HttpResponseException)
&& !($e instanceof ValidationException)) {
2015-04-28 22:13:52 +02:00
$data = [
'error' => get_class($e),
'hideHeader' => true,
];
2015-04-28 22:13:52 +02:00
return response()->view('error', $data);
} else {
return parent::render($request, $e);
}
2016-07-21 14:35:23 +02:00
}
2015-03-12 01:44:39 +01:00
}