diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 496708104..551d9b937 100755 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -2,12 +2,14 @@ namespace App\Exceptions; +use App\Exceptions\MethodNotAllowedHttpException as ExceptionsMethodNotAllowedHttpException; use App\Exceptions\QueryException as ExceptionsQueryException; use Illuminate\Auth\AuthenticationException; use Illuminate\Database\QueryException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Support\Facades\Log; use Illuminate\Validation\ValidationException as MainValidationException; +use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; use Throwable; class Handler extends ExceptionHandler @@ -89,6 +91,19 @@ public function render($request, Throwable $exception) ->render( $request ); } + if ( $exception instanceof MethodNotAllowedHttpException ) { + if ( $request->expectsJson() ) { + Log::error( $exception->getMessage() ); + + return response()->json([ + 'message' => env( 'APP_DEBUG' ) ? $exception->getMessage() : __( 'Invalid method used for the current request.' ) + ], 404); + } + + return ( new ExceptionsMethodNotAllowedHttpException( $exception->getMessage() ) ) + ->render( $request ); + } + return parent::render($request, $exception); } } diff --git a/app/Exceptions/MethodNotAllowedHttpException.php b/app/Exceptions/MethodNotAllowedHttpException.php new file mode 100644 index 000000000..eec3ac85c --- /dev/null +++ b/app/Exceptions/MethodNotAllowedHttpException.php @@ -0,0 +1,15 @@ +getMessage(); + $title = __( 'Method Not Allowed' ); + return response()->view( 'pages.errors.http-exception', compact( 'message', 'title' ), 500 ); + } +} diff --git a/resources/views/pages/errors/http-exception.blade.php b/resources/views/pages/errors/http-exception.blade.php new file mode 100755 index 000000000..26b781a76 --- /dev/null +++ b/resources/views/pages/errors/http-exception.blade.php @@ -0,0 +1,20 @@ +@extends( 'layout.default' ) + +@section( 'layout.default.body' ) +
+
+ + + +

{!! $title ?? __( 'Method Not Allowed' ) !!}

+
+

{{ $message }}

+
+ +
+
+@endsection \ No newline at end of file