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\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}", [

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