Convert errors into HttpException calls

This commit is contained in:
Nabeel Shahzad
2018-02-22 15:15:00 -06:00
parent 3748ab77d2
commit 53b0bbd936
6 changed files with 73 additions and 21 deletions

View 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
);
}
}

View File

@@ -11,12 +11,14 @@ use Log;
use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Class Handler
* @package App\Exceptions
*/
class Handler extends ExceptionHandler class Handler extends ExceptionHandler
{ {
/** /**
* A list of the exception types that should not be reported. * A list of the exception types that should not be reported.
*
* @var array
*/ */
protected $dontReport = [ protected $dontReport = [
\Illuminate\Auth\AuthenticationException::class, \Illuminate\Auth\AuthenticationException::class,
@@ -64,7 +66,7 @@ class Handler extends ExceptionHandler
*/ */
public function render($request, Exception $exception) public function render($request, Exception $exception)
{ {
if($request->is('api/*')) { if ($request->expectsJson() || $request->is('api/*')) {
$headers = []; $headers = [];
@@ -75,10 +77,8 @@ class Handler extends ExceptionHandler
$error = $this->createError(404, $exception->getMessage()); $error = $this->createError(404, $exception->getMessage());
} }
# These are application errors. Custom exceptions should # Custom exceptions should be extending HttpException
# be extending HttpException
elseif ($exception instanceof HttpException) { elseif ($exception instanceof HttpException) {
$error = $this->createError( $error = $this->createError(
$exception->getStatusCode(), $exception->getStatusCode(),
$exception->getMessage() $exception->getMessage()
@@ -131,8 +131,9 @@ class Handler extends ExceptionHandler
*/ */
protected function unauthenticated($request, AuthenticationException $exception) protected function unauthenticated($request, AuthenticationException $exception)
{ {
if ($request->expectsJson()) { if ($request->expectsJson() || $request->is('api/*')) {
return response()->json(['error' => 'Unauthenticated.'], 401); $error = $this->createError(401, 'Unauthenticated');
return response()->json($error, 401);
} }
return redirect()->guest('login'); return redirect()->guest('login');
@@ -140,8 +141,10 @@ class Handler extends ExceptionHandler
/** /**
* Render the given HttpException. * 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(); $status = $e->getStatusCode();
view()->replaceNamespace('errors', [ view()->replaceNamespace('errors', [
@@ -150,8 +153,6 @@ class Handler extends ExceptionHandler
__DIR__ . '/views', __DIR__ . '/views',
]); ]);
#Log::info('error status '. $status);
if (view()->exists("errors::{$status}")) { if (view()->exists("errors::{$status}")) {
#if (view()->exists('layouts' . config('phpvms.skin') .'.errors.' .$status)) { #if (view()->exists('layouts' . config('phpvms.skin') .'.errors.' .$status)) {
return response()->view("errors::{$status}", [ return response()->view("errors::{$status}", [

View 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
);
}
}

View File

@@ -2,6 +2,8 @@
namespace App\Http\Controllers\Api; namespace App\Http\Controllers\Api;
use App\Exceptions\AircraftPermissionDenied;
use App\Exceptions\PirepCancelled;
use App\Http\Requests\Acars\CommentRequest; use App\Http\Requests\Acars\CommentRequest;
use App\Http\Requests\Acars\EventRequest; use App\Http\Requests\Acars\EventRequest;
use App\Http\Requests\Acars\FileRequest; use App\Http\Requests\Acars\FileRequest;
@@ -28,7 +30,6 @@ use App\Services\UserService;
use Auth; use Auth;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Log; use Log;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class PirepController extends RestController class PirepController extends RestController
{ {
@@ -63,12 +64,12 @@ class PirepController extends RestController
/** /**
* Check if a PIREP is cancelled * Check if a PIREP is cancelled
* @param $pirep * @param $pirep
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException * @throws \App\Exceptions\PirepCancelled
*/ */
protected function checkCancelled(Pirep $pirep) protected function checkCancelled(Pirep $pirep)
{ {
if (!$pirep->allowedUpdates()) { 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 * @param PrefileRequest $request
* @return PirepResource * @return PirepResource
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException * @throws \App\Exceptions\PirepCancelled
* @throws \App\Exceptions\AircraftPermissionDenied
*/ */
public function prefile(PrefileRequest $request) public function prefile(PrefileRequest $request)
{ {
@@ -130,7 +132,7 @@ class PirepController extends RestController
if(setting('pireps.restrict_aircraft_to_rank', false)) { if(setting('pireps.restrict_aircraft_to_rank', false)) {
$can_use_ac = $this->userSvc->aircraftAllowed($user, $pirep->aircraft_id); $can_use_ac = $this->userSvc->aircraftAllowed($user, $pirep->aircraft_id);
if (!$can_use_ac) { 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); $dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
if($dupe_pirep !== false) { if($dupe_pirep !== false) {
$pirep = $dupe_pirep; $pirep = $dupe_pirep;
$this->checkCancelled($pirep);
} }
$pirep->save(); $pirep->save();
@@ -158,7 +161,8 @@ class PirepController extends RestController
* @param $id * @param $id
* @param UpdateRequest $request * @param UpdateRequest $request
* @return PirepResource * @return PirepResource
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException * @throws \App\Exceptions\PirepCancelled
* @throws \App\Exceptions\AircraftPermissionDenied
* @throws \Prettus\Validator\Exceptions\ValidatorException * @throws \Prettus\Validator\Exceptions\ValidatorException
*/ */
public function update($id, UpdateRequest $request) public function update($id, UpdateRequest $request)
@@ -178,7 +182,7 @@ class PirepController extends RestController
) { ) {
$can_use_ac = $this->userSvc->aircraftAllowed($user, $pirep->aircraft_id); $can_use_ac = $this->userSvc->aircraftAllowed($user, $pirep->aircraft_id);
if (!$can_use_ac) { 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 $id
* @param FileRequest $request * @param FileRequest $request
* @return PirepResource * @return PirepResource
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException * @throws \App\Exceptions\PirepCancelled
* @throws \App\Exceptions\AircraftPermissionDenied
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
* @throws \Exception * @throws \Exception
*/ */
@@ -215,7 +220,7 @@ class PirepController extends RestController
) { ) {
$can_use_ac = $this->userSvc->aircraftAllowed($user, $pirep->aircraft_id); $can_use_ac = $this->userSvc->aircraftAllowed($user, $pirep->aircraft_id);
if (!$can_use_ac) { 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 $id
* @param PositionRequest $request * @param PositionRequest $request
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
* @throws \App\Exceptions\PirepCancelled
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
*/ */
public function acars_store($id, PositionRequest $request) public function acars_store($id, PositionRequest $request)
@@ -336,6 +342,7 @@ class PirepController extends RestController
* @param $id * @param $id
* @param LogRequest $request * @param LogRequest $request
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
* @throws \App\Exceptions\PirepCancelled
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
*/ */
public function acars_logs($id, LogRequest $request) public function acars_logs($id, LogRequest $request)
@@ -367,6 +374,7 @@ class PirepController extends RestController
* @param $id * @param $id
* @param EventRequest $request * @param EventRequest $request
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
* @throws \App\Exceptions\PirepCancelled
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
*/ */
public function acars_events($id, EventRequest $request) public function acars_events($id, EventRequest $request)
@@ -410,6 +418,7 @@ class PirepController extends RestController
* @param $id * @param $id
* @param CommentRequest $request * @param CommentRequest $request
* @return PirepCommentResource * @return PirepCommentResource
* @throws \App\Exceptions\PirepCancelled
*/ */
public function comments_post($id, CommentRequest $request) public function comments_post($id, CommentRequest $request)
{ {
@@ -447,6 +456,7 @@ class PirepController extends RestController
* @param $id * @param $id
* @param RouteRequest $request * @param RouteRequest $request
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
* @throws \App\Exceptions\PirepCancelled
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
*/ */
public function route_post($id, RouteRequest $request) public function route_post($id, RouteRequest $request)

View File

@@ -145,7 +145,7 @@ class FlightService extends BaseService
$user_bids = UserBid::where(['user_id' => $user->id])->first(); $user_bids = UserBid::where(['user_id' => $user->id])->first();
if($user_bids) { if($user_bids) {
Log::info('User "' . $user->id . '" already has bids, skipping'); Log::info('User "' . $user->id . '" already has bids, skipping');
return null; throw new BidExists();
} }
} }

View File

@@ -21,6 +21,7 @@ class UserService extends BaseService
/** /**
* UserService constructor. * UserService constructor.
* @param AircraftRepository $aircraftRepo
* @param SubfleetRepository $subfleetRepo * @param SubfleetRepository $subfleetRepo
*/ */
public function __construct( public function __construct(