Skip to content

Commit

Permalink
[4.x] Avoid custom exception handler for API requests (#9275)
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Jan 9, 2024
1 parent d870914 commit 934f895
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 65 deletions.
2 changes: 0 additions & 2 deletions routes/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
use Illuminate\Support\Facades\Route;
use Statamic\API\Middleware\Cache;
use Statamic\Facades\Glide;
use Statamic\Http\Middleware\API\SwapExceptionHandler as SwapAPIExceptionHandler;
use Statamic\Http\Middleware\CP\SwapExceptionHandler as SwapCpExceptionHandler;
use Statamic\Http\Middleware\RequireStatamicPro;

if (config('statamic.api.enabled')) {
Route::middleware([
SwapApiExceptionHandler::class,
RequireStatamicPro::class,
Cache::class,
])->group(function () {
Expand Down
16 changes: 0 additions & 16 deletions src/Exceptions/ApiExceptionHandler.php

This file was deleted.

13 changes: 13 additions & 0 deletions src/Exceptions/ApiValidationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Exceptions;

use Illuminate\Validation\ValidationException;

class ApiValidationException extends ValidationException
{
public function render()
{
return response()->json(['message' => $this->getMessage()], 422);
}
}
28 changes: 0 additions & 28 deletions src/Exceptions/Concerns/RendersApiExceptions.php

This file was deleted.

9 changes: 9 additions & 0 deletions src/Exceptions/Concerns/RendersHttpExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public function render()
return response()->view('statamic::errors.'.$this->getStatusCode(), [], $this->getStatusCode());
}

if (Statamic::isApiRoute()) {
return response()->json(['message' => $this->getApiMessage()], $this->getStatusCode());
}

if (view()->exists('errors.'.$this->getStatusCode())) {
return response($this->contents(), $this->getStatusCode());
}
Expand Down Expand Up @@ -44,4 +48,9 @@ protected function layout()
return view()->exists($layout);
});
}

public function getApiMessage()
{
return $this->getMessage();
}
}
5 changes: 5 additions & 0 deletions src/Exceptions/NotFoundHttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@
class NotFoundHttpException extends SymfonyException
{
use RendersHttpExceptions;

public function getApiMessage()
{
return 'Not found.';
}
}
5 changes: 4 additions & 1 deletion src/Exceptions/StatamicProAuthorizationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@

class StatamicProAuthorizationException extends AuthorizationException
{
//
public function render()
{
throw new StatamicProRequiredException($this->getMessage());
}
}
2 changes: 0 additions & 2 deletions src/GraphQL/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Statamic\Contracts\GraphQL\ResponseCache;
use Statamic\GraphQL\ResponseCache\DefaultCache;
use Statamic\GraphQL\ResponseCache\NullCache;
use Statamic\Http\Middleware\API\SwapExceptionHandler;
use Statamic\Http\Middleware\HandleToken;
use Statamic\Http\Middleware\RequireStatamicPro;

Expand Down Expand Up @@ -60,7 +59,6 @@ private function addMiddleware()
collect($this->app['router']->getRoutes()->getRoutes())
->filter(fn ($route) => $route->getAction()['uses'] === GraphQLController::class.'@query')
->each(fn ($route) => $route->middleware([
SwapExceptionHandler::class,
RequireStatamicPro::class,
HandleToken::class,
]));
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/API/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Statamic\Http\Controllers\API;

use Facades\Statamic\API\ResourceAuthorizer;
use Illuminate\Validation\ValidationException;
use Statamic\Exceptions\ApiValidationException;
use Statamic\Exceptions\NotFoundHttpException;
use Statamic\Facades\Site;
use Statamic\Http\Controllers\Controller;
Expand Down Expand Up @@ -138,7 +138,7 @@ protected function getFilters()
->filter(fn ($field) => ! $allowedFilters->contains($field));

if ($forbidden->isNotEmpty()) {
throw ValidationException::withMessages([
throw ApiValidationException::withMessages([
'filter' => Str::plural('Forbidden filter', $forbidden).': '.$forbidden->join(', '),
]);
}
Expand Down
14 changes: 0 additions & 14 deletions src/Http/Middleware/API/SwapExceptionHandler.php

This file was deleted.

0 comments on commit 934f895

Please sign in to comment.