diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 28d29a8f2..b8f415381 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -5,6 +5,9 @@ namespace App\Exceptions; use Exception; use Illuminate\Auth\AuthenticationException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Symfony\Component\Debug\Exception\FatalThrowableError; +use Illuminate\Database\Eloquent\ModelNotFoundException; class Handler extends ExceptionHandler { @@ -44,6 +47,10 @@ class Handler extends ExceptionHandler */ public function render($request, Exception $exception) { + if (env('APP_DEBUG') === false) { + return $this->handleExceptions($request, $exception); + } + return parent::render($request, $exception); } @@ -65,4 +72,43 @@ class Handler extends ExceptionHandler return redirect()->guest(route('login')); } + + private function handleExceptions($request, $exception) + { + if ($exception instanceof NotFoundHttpException) { + // ajax 404 json feedback + if ($request->ajax()) { + return response()->json(['error' => 'Not Found'], 404); + } + + flash(trans('errors.body.page_not_found'))->error(); + + // normal 404 view page feedback + return redirect() + ->back() + ->withErrors(['msg', trans('errors.body.page_not_found')]); + } + + if ($exception instanceof ModelNotFoundException) { + // ajax 404 json feedback + if ($request->ajax()) { + return response()->json(['error' => 'Not Found'], 404); + } + + // normal 404 view page feedback + return response()->view('errors.404', [], 404); + } + + if ($exception instanceof FatalThrowableError) { + // ajax 500 json feedback + if ($request->ajax()) { + return response()->json(['error' => 'Error Page'], 500); + } + + // normal 500 view page feedback + return response()->view('errors.500', [], 500); + } + + return response()->view('errors.500', [], 500); + } } diff --git a/resources/lang/en-GB/errors.php b/resources/lang/en-GB/errors.php new file mode 100644 index 000000000..e773681ec --- /dev/null +++ b/resources/lang/en-GB/errors.php @@ -0,0 +1,24 @@ + 'Forbidden Access', + 'error_page' => 'Error Page', + 'page_not_found' => 'Page Not Found', + + 'body' => [ + 'forbidden_access' => 'Oops! Forbidden Access.', + 'error_page' => 'Oops! Something went wrong.', + 'page_not_found' => 'Oops! Page not found.', + ], + + 'messages' => [ + 'forbidden_access' => 'You can not access this page. + Meanwhile, you may return to dashboard.', + 'error_page' => 'We will work on fixing that right away. + Meanwhile, you may return to dashboard.', + 'page_not_found' => 'We could not find the page you were looking for. + Meanwhile, you may return to dashboard.', + ], + +]; diff --git a/resources/views/errors/403.blade.php b/resources/views/errors/403.blade.php index 2d45bbe94..61959aa61 100644 --- a/resources/views/errors/403.blade.php +++ b/resources/views/errors/403.blade.php @@ -1,13 +1,15 @@ @extends('layouts.admin') -@section('title', 'Forbidden Access') +@section('title', trans('errors.forbidden_access')) @section('content') - -
-
-

You don't have permission.

+
+

403

+ +
+

{{ trans('errors.body.forbidden_access') }}

+ +

{!! trans('errors.messages.forbidden_access', ['link' => url('/') ]) !!}

-
@endsection diff --git a/resources/views/errors/404.blade.php b/resources/views/errors/404.blade.php new file mode 100644 index 000000000..4d2771444 --- /dev/null +++ b/resources/views/errors/404.blade.php @@ -0,0 +1,15 @@ +@extends('layouts.admin') + +@section('title', trans('errors.page_not_found')) + +@section('content') +
+

404

+ +
+

{{ trans('errors.body.page_not_found') }}

+ +

{!! trans('errors.messages.page_not_found', ['link' => url('/')]) !!}

+
+
+@endsection diff --git a/resources/views/errors/500.blade.php b/resources/views/errors/500.blade.php new file mode 100644 index 000000000..04b7d8990 --- /dev/null +++ b/resources/views/errors/500.blade.php @@ -0,0 +1,15 @@ +@extends('layouts.admin') + +@section('title', trans('errors.error_page')) + +@section('content') +
+

500

+ +
+

{{ trans('errors.body.error_page') }}

+ +

{!! trans('errors.messages.error_page', ['link' => url('/') ]) !!}

+
+
+@endsection