Convert errors into HttpException calls
This commit is contained in:
20
app/Exceptions/AircraftPermissionDenied.php
Normal file
20
app/Exceptions/AircraftPermissionDenied.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
class AircraftPermissionDenied extends HttpException
|
||||
{
|
||||
public function __construct(string $message = null, \Exception $previous = null, int $code = 0, array $headers = [])
|
||||
{
|
||||
parent::__construct(
|
||||
400,
|
||||
'User is not allowed to fly this aircraft',
|
||||
$previous, $headers, $code
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -11,12 +11,14 @@ use Log;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
* Class Handler
|
||||
* @package App\Exceptions
|
||||
*/
|
||||
class Handler extends ExceptionHandler
|
||||
{
|
||||
/**
|
||||
* A list of the exception types that should not be reported.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $dontReport = [
|
||||
\Illuminate\Auth\AuthenticationException::class,
|
||||
@@ -64,7 +66,7 @@ class Handler extends ExceptionHandler
|
||||
*/
|
||||
public function render($request, Exception $exception)
|
||||
{
|
||||
if($request->is('api/*')) {
|
||||
if ($request->expectsJson() || $request->is('api/*')) {
|
||||
|
||||
$headers = [];
|
||||
|
||||
@@ -75,10 +77,8 @@ class Handler extends ExceptionHandler
|
||||
$error = $this->createError(404, $exception->getMessage());
|
||||
}
|
||||
|
||||
# These are application errors. Custom exceptions should
|
||||
# be extending HttpException
|
||||
# Custom exceptions should be extending HttpException
|
||||
elseif ($exception instanceof HttpException) {
|
||||
|
||||
$error = $this->createError(
|
||||
$exception->getStatusCode(),
|
||||
$exception->getMessage()
|
||||
@@ -131,8 +131,9 @@ class Handler extends ExceptionHandler
|
||||
*/
|
||||
protected function unauthenticated($request, AuthenticationException $exception)
|
||||
{
|
||||
if ($request->expectsJson()) {
|
||||
return response()->json(['error' => 'Unauthenticated.'], 401);
|
||||
if ($request->expectsJson() || $request->is('api/*')) {
|
||||
$error = $this->createError(401, 'Unauthenticated');
|
||||
return response()->json($error, 401);
|
||||
}
|
||||
|
||||
return redirect()->guest('login');
|
||||
@@ -140,8 +141,10 @@ class Handler extends ExceptionHandler
|
||||
|
||||
/**
|
||||
* Render the given HttpException.
|
||||
* @param HttpException $e
|
||||
* @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
protected function renderHttpException(\Symfony\Component\HttpKernel\Exception\HttpException $e)
|
||||
protected function renderHttpException(HttpException $e)
|
||||
{
|
||||
$status = $e->getStatusCode();
|
||||
view()->replaceNamespace('errors', [
|
||||
@@ -150,8 +153,6 @@ class Handler extends ExceptionHandler
|
||||
__DIR__ . '/views',
|
||||
]);
|
||||
|
||||
#Log::info('error status '. $status);
|
||||
|
||||
if (view()->exists("errors::{$status}")) {
|
||||
#if (view()->exists('layouts' . config('phpvms.skin') .'.errors.' .$status)) {
|
||||
return response()->view("errors::{$status}", [
|
||||
|
||||
20
app/Exceptions/PirepCancelled.php
Normal file
20
app/Exceptions/PirepCancelled.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
class PirepCancelled extends HttpException
|
||||
{
|
||||
public function __construct(string $message = null, \Exception $previous = null, int $code = 0, array $headers = [])
|
||||
{
|
||||
parent::__construct(
|
||||
400,
|
||||
'PIREP has been cancelled, updates are not allowed',
|
||||
$previous, $headers, $code
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Exceptions\AircraftPermissionDenied;
|
||||
use App\Exceptions\PirepCancelled;
|
||||
use App\Http\Requests\Acars\CommentRequest;
|
||||
use App\Http\Requests\Acars\EventRequest;
|
||||
use App\Http\Requests\Acars\FileRequest;
|
||||
@@ -28,7 +30,6 @@ use App\Services\UserService;
|
||||
use Auth;
|
||||
use Illuminate\Http\Request;
|
||||
use Log;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
|
||||
class PirepController extends RestController
|
||||
{
|
||||
@@ -63,12 +64,12 @@ class PirepController extends RestController
|
||||
/**
|
||||
* Check if a PIREP is cancelled
|
||||
* @param $pirep
|
||||
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
||||
* @throws \App\Exceptions\PirepCancelled
|
||||
*/
|
||||
protected function checkCancelled(Pirep $pirep)
|
||||
{
|
||||
if (!$pirep->allowedUpdates()) {
|
||||
throw new BadRequestHttpException('PIREP has been cancelled, comments can\'t be posted');
|
||||
throw new PirepCancelled();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,7 +111,8 @@ class PirepController extends RestController
|
||||
*
|
||||
* @param PrefileRequest $request
|
||||
* @return PirepResource
|
||||
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
||||
* @throws \App\Exceptions\PirepCancelled
|
||||
* @throws \App\Exceptions\AircraftPermissionDenied
|
||||
*/
|
||||
public function prefile(PrefileRequest $request)
|
||||
{
|
||||
@@ -130,7 +132,7 @@ class PirepController extends RestController
|
||||
if(setting('pireps.restrict_aircraft_to_rank', false)) {
|
||||
$can_use_ac = $this->userSvc->aircraftAllowed($user, $pirep->aircraft_id);
|
||||
if (!$can_use_ac) {
|
||||
throw new BadRequestHttpException('User is not allowed to fly this aircraft');
|
||||
throw new AircraftPermissionDenied();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,6 +140,7 @@ class PirepController extends RestController
|
||||
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
|
||||
if($dupe_pirep !== false) {
|
||||
$pirep = $dupe_pirep;
|
||||
$this->checkCancelled($pirep);
|
||||
}
|
||||
|
||||
$pirep->save();
|
||||
@@ -158,7 +161,8 @@ class PirepController extends RestController
|
||||
* @param $id
|
||||
* @param UpdateRequest $request
|
||||
* @return PirepResource
|
||||
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
||||
* @throws \App\Exceptions\PirepCancelled
|
||||
* @throws \App\Exceptions\AircraftPermissionDenied
|
||||
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
||||
*/
|
||||
public function update($id, UpdateRequest $request)
|
||||
@@ -178,7 +182,7 @@ class PirepController extends RestController
|
||||
) {
|
||||
$can_use_ac = $this->userSvc->aircraftAllowed($user, $pirep->aircraft_id);
|
||||
if (!$can_use_ac) {
|
||||
throw new BadRequestHttpException('User is not allowed to fly this aircraft');
|
||||
throw new AircraftPermissionDenied();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,7 +197,8 @@ class PirepController extends RestController
|
||||
* @param $id
|
||||
* @param FileRequest $request
|
||||
* @return PirepResource
|
||||
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
||||
* @throws \App\Exceptions\PirepCancelled
|
||||
* @throws \App\Exceptions\AircraftPermissionDenied
|
||||
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
||||
* @throws \Exception
|
||||
*/
|
||||
@@ -215,7 +220,7 @@ class PirepController extends RestController
|
||||
) {
|
||||
$can_use_ac = $this->userSvc->aircraftAllowed($user, $pirep->aircraft_id);
|
||||
if (!$can_use_ac) {
|
||||
throw new BadRequestHttpException('User is not allowed to fly this aircraft');
|
||||
throw new AircraftPermissionDenied();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -297,6 +302,7 @@ class PirepController extends RestController
|
||||
* @param $id
|
||||
* @param PositionRequest $request
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @throws \App\Exceptions\PirepCancelled
|
||||
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
||||
*/
|
||||
public function acars_store($id, PositionRequest $request)
|
||||
@@ -336,6 +342,7 @@ class PirepController extends RestController
|
||||
* @param $id
|
||||
* @param LogRequest $request
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @throws \App\Exceptions\PirepCancelled
|
||||
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
||||
*/
|
||||
public function acars_logs($id, LogRequest $request)
|
||||
@@ -367,6 +374,7 @@ class PirepController extends RestController
|
||||
* @param $id
|
||||
* @param EventRequest $request
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @throws \App\Exceptions\PirepCancelled
|
||||
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
||||
*/
|
||||
public function acars_events($id, EventRequest $request)
|
||||
@@ -410,6 +418,7 @@ class PirepController extends RestController
|
||||
* @param $id
|
||||
* @param CommentRequest $request
|
||||
* @return PirepCommentResource
|
||||
* @throws \App\Exceptions\PirepCancelled
|
||||
*/
|
||||
public function comments_post($id, CommentRequest $request)
|
||||
{
|
||||
@@ -447,6 +456,7 @@ class PirepController extends RestController
|
||||
* @param $id
|
||||
* @param RouteRequest $request
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @throws \App\Exceptions\PirepCancelled
|
||||
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
||||
*/
|
||||
public function route_post($id, RouteRequest $request)
|
||||
|
||||
@@ -145,7 +145,7 @@ class FlightService extends BaseService
|
||||
$user_bids = UserBid::where(['user_id' => $user->id])->first();
|
||||
if($user_bids) {
|
||||
Log::info('User "' . $user->id . '" already has bids, skipping');
|
||||
return null;
|
||||
throw new BidExists();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ class UserService extends BaseService
|
||||
|
||||
/**
|
||||
* UserService constructor.
|
||||
* @param AircraftRepository $aircraftRepo
|
||||
* @param SubfleetRepository $subfleetRepo
|
||||
*/
|
||||
public function __construct(
|
||||
|
||||
Reference in New Issue
Block a user