Skip to content

Commit

Permalink
#405 - transferring exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofrewak committed Apr 3, 2024
1 parent 37012d3 commit 01e48a1
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 89 deletions.
61 changes: 0 additions & 61 deletions app/Exceptions/ExceptionHandler.php

This file was deleted.

4 changes: 4 additions & 0 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Response;
use Spatie\Permission\Models\Permission;
use Toby\Actions\CreateUserAction;
use Toby\Actions\SyncUserPermissionsWithRoleAction;
use Toby\Actions\UpdateUserAction;
Expand All @@ -25,6 +26,9 @@ class UserController extends Controller
*/
public function index(Request $request): Response
{
/** @var User $user */
$user = $request->user();

$this->authorize("manageUsers");

$searchText = $request->query("search");
Expand Down
6 changes: 4 additions & 2 deletions app/Http/Middleware/HandleInertiaRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Toby\Domain\VacationRequestStatesRetriever;
use Toby\Helpers\YearPeriodRetriever;
use Toby\Http\Resources\UserResource;
use Toby\Models\User;
use Toby\Models\VacationRequest;

class HandleInertiaRequests extends Middleware
Expand All @@ -36,14 +37,15 @@ public function share(Request $request): array

protected function getAuthData(Request $request): Closure
{
/** @var ?User $user */
$user = $request->user()?->load("profile");

return fn(): array => [
"user" => $user ? new UserResource($user) : null,
"user" => $user ?? new UserResource($user),
"can" => Permission::query()->with("roles")->get()
->mapWithKeys(
fn(Permission $permission): array => [
$permission->name => $user ? $user->hasPermissionTo($permission) : false,
$permission->name => $user && $user->hasPermissionTo($permission),
],
),
];
Expand Down
42 changes: 37 additions & 5 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
declare(strict_types=1);

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Request as LaravelRequest;
use Inertia\Inertia;
use Sentry\State\HubInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Toby\Http\Middleware\HandleInertiaRequests;
use Toby\Http\Middleware\RedirectIfAuthenticated;

Expand All @@ -22,12 +27,39 @@
$middleware->trustProxies(
at: "*",
headers: Request::HEADER_X_FORWARDED_FOR |
Request::HEADER_X_FORWARDED_HOST |
Request::HEADER_X_FORWARDED_PORT |
Request::HEADER_X_FORWARDED_PROTO |
Request::HEADER_X_FORWARDED_AWS_ELB,
Request::HEADER_X_FORWARDED_HOST |
Request::HEADER_X_FORWARDED_PORT |
Request::HEADER_X_FORWARDED_PROTO |
Request::HEADER_X_FORWARDED_AWS_ELB,
);
$middleware->statefulApi();
})
->withExceptions()
->withExceptions(function (Exceptions $exceptions): void {
$exceptions->reportable(function (Throwable $exception): void {
if (app()->bound(HubInterface::class)) {
app(HubInterface::class)->captureException($exception);
}
});

$exceptions->respond(function (Response $response, Throwable $exception, LaravelRequest $request) {
if (!app()->environment(['local', 'testing']) && in_array($response->getStatusCode(), [
Response::HTTP_INTERNAL_SERVER_ERROR,
Response::HTTP_SERVICE_UNAVAILABLE,
Response::HTTP_TOO_MANY_REQUESTS,
Response::HTTP_NOT_FOUND,
Response::HTTP_FORBIDDEN,
Response::HTTP_UNAUTHORIZED,
], strict: true)) {
return Inertia::render('Error', ['status' => $response->getStatusCode()])
->toResponse($request)
->setStatusCode($response->getStatusCode());
} elseif ($response->getStatusCode() === 419) {
return back()->with([
'message' => 'The page expired, please try again.',
]);
}

return $response;
});
})
->create();
9 changes: 6 additions & 3 deletions config/permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
declare(strict_types=1);

use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role as SpatieRole;
use Toby\Enums\Role;

return [
"models" => [
"permission" => Permission::class,
"role" => Spatie\Permission\Models\Role::class,
"role" => SpatieRole::class,
],
"table_names" => [
"roles" => "roles",
Expand All @@ -24,9 +25,11 @@
"team_foreign_key" => "team_id",
],
"register_permission_check_method" => true,
'register_octane_reset_listener' => false,
"teams" => false,
"display_permission_in_exception" => false,
"display_role_in_exception" => false,
'use_passport_client_credentials' => false,
"display_permission_in_exception" => true,
"display_role_in_exception" => true,
"enable_wildcard_permission" => false,
"cache" => [
"expiration_time" => DateInterval::createFromDateString("24 hours"),
Expand Down
11 changes: 6 additions & 5 deletions public/vendor/telescope/app-dark.css

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions public/vendor/telescope/app.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/vendor/telescope/app.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions public/vendor/telescope/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"/app.js": "/app.js?id=d3ff915bff3de87b87cc",
"/app-dark.css": "/app-dark.css?id=3ae28ef5f7b987d68dc6",
"/app.css": "/app.css?id=7c970f699ed9cf60d80b"
"/app.js": "/app.js?id=7049e92a398e816f8cd53a915eaea592",
"/app-dark.css": "/app-dark.css?id=1ea407db56c5163ae29311f1f38eb7b9",
"/app.css": "/app.css?id=de4c978567bfd90b38d186937dee5ccf"
}
9 changes: 4 additions & 5 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
Route::post("/users/{user}/restore", [UserController::class, "restore"])
->whereNumber("user")
->withTrashed();
Route::get("/users/{user}/permissions", [PermissionController::class, "show"])
->whereNumber("user");
Route::patch("/users/{user}/permissions", [PermissionController::class, "update"])
->whereNumber("user");

Route::resource("equipment-items", EquipmentController::class)
->except("show")
Expand All @@ -54,11 +58,6 @@
->only(["index", "store", "destroy"])
->whereNumber("equipmentLabels");

Route::get("/users/{user}/permissions", [PermissionController::class, "show"])
->whereNumber("user");
Route::patch("/users/{user}/permissions", [PermissionController::class, "update"])
->whereNumber("user");

Route::resource("benefits", BenefitController::class)
->only(["index", "store", "destroy"])
->whereNumber("benefit");
Expand Down

0 comments on commit 01e48a1

Please sign in to comment.