From 53b0bbd936e1a4d6c279537dce0caec62fad22b0 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Thu, 22 Feb 2018 15:15:00 -0600 Subject: [PATCH] Convert errors into HttpException calls --- app/Exceptions/AircraftPermissionDenied.php | 20 ++++++++++++++ app/Exceptions/Handler.php | 23 ++++++++-------- app/Exceptions/PirepCancelled.php | 20 ++++++++++++++ app/Http/Controllers/Api/PirepController.php | 28 +++++++++++++------- app/Services/FlightService.php | 2 +- app/Services/UserService.php | 1 + 6 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 app/Exceptions/AircraftPermissionDenied.php create mode 100644 app/Exceptions/PirepCancelled.php diff --git a/app/Exceptions/AircraftPermissionDenied.php b/app/Exceptions/AircraftPermissionDenied.php new file mode 100644 index 00000000..b0e83b43 --- /dev/null +++ b/app/Exceptions/AircraftPermissionDenied.php @@ -0,0 +1,20 @@ +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}", [ diff --git a/app/Exceptions/PirepCancelled.php b/app/Exceptions/PirepCancelled.php new file mode 100644 index 00000000..7c1cd513 --- /dev/null +++ b/app/Exceptions/PirepCancelled.php @@ -0,0 +1,20 @@ +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) diff --git a/app/Services/FlightService.php b/app/Services/FlightService.php index 8369d53f..a7f9223e 100644 --- a/app/Services/FlightService.php +++ b/app/Services/FlightService.php @@ -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(); } } diff --git a/app/Services/UserService.php b/app/Services/UserService.php index c2ecc4f5..074bc4c0 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -21,6 +21,7 @@ class UserService extends BaseService /** * UserService constructor. + * @param AircraftRepository $aircraftRepo * @param SubfleetRepository $subfleetRepo */ public function __construct(