Collections
1 month ago
Concerns
3 weeks ago
Console
1 week ago
Constants
1 month ago
Contracts
1 week ago
Database
1 week ago
Discovery
1 month ago
Exceptions
1 week ago
Filesystem
1 week ago
Http
1 week ago
Managers
1 week ago
Middlewares
1 month ago
Polyfill
1 month ago
Routing
1 week ago
Supports
1 week ago
Validation
4 days ago
View
1 week ago
Wordpress
1 week ago
ApiExceptionHandler.php
1 month ago
Application.php
1 week ago
Container.php
1 month ago
CoreServiceProvider.php
1 week ago
DTO.php
1 month ago
Facade.php
1 month ago
Listener.php
3 weeks ago
Resource.php
1 week ago
Route.php
1 week ago
Sanitizer.php
1 week ago
ServiceProvider.php
1 month ago
SiteExceptionHandler.php
1 week ago
helpers.php
1 week ago
SiteExceptionHandler.php
66 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Translates thrown exceptions into WordPress-native site route error responses. |
| 5 | * Maps authorization, not-found, and validation failures to HTTP status pages. |
| 6 | * |
| 7 | * @package Framework |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | namespace Kirki\Framework; |
| 11 | |
| 12 | \defined('ABSPATH') || exit; |
| 13 | use Kirki\Framework\Exceptions\AuthorizationException; |
| 14 | use Kirki\Framework\Exceptions\ModelNotFoundException; |
| 15 | use Kirki\Framework\Exceptions\ValidationException; |
| 16 | use Kirki\Framework\Http\Response; |
| 17 | use Exception; |
| 18 | class SiteExceptionHandler |
| 19 | { |
| 20 | /** |
| 21 | * Handle an exception for a site route request. |
| 22 | * |
| 23 | * @param Exception $exception The exception to handle. |
| 24 | * |
| 25 | * @return void |
| 26 | * |
| 27 | * @since 1.0.0 |
| 28 | */ |
| 29 | public static function handle(Exception $exception) |
| 30 | { |
| 31 | if ($exception instanceof AuthorizationException) { |
| 32 | static::fail(Response::FORBIDDEN, $exception->getMessage() ?: 'Forbidden'); |
| 33 | } |
| 34 | if ($exception instanceof ModelNotFoundException) { |
| 35 | static::fail(Response::NOT_FOUND, $exception->getMessage() ?: 'Not Found'); |
| 36 | } |
| 37 | if ($exception instanceof ValidationException) { |
| 38 | static::fail(Response::UNPROCESSABLE_ENTITY, $exception->getMessage() ?: 'Validation failed'); |
| 39 | } |
| 40 | $status = (int) $exception->getCode(); |
| 41 | if ($status < 100 || $status > 599) { |
| 42 | $status = Response::INTERNAL_SERVER_ERROR; |
| 43 | } |
| 44 | if (\function_exists('error_log')) { |
| 45 | \error_log($exception->getMessage()); |
| 46 | } |
| 47 | static::fail($status, $exception->getMessage() ?: 'Internal Server Error'); |
| 48 | } |
| 49 | /** |
| 50 | * Stop the request with a WordPress error page at the given HTTP status. |
| 51 | * |
| 52 | * @param int $status HTTP status code. |
| 53 | * @param string $message Error message. |
| 54 | * |
| 55 | * @return void |
| 56 | * |
| 57 | * @since 1.0.0 |
| 58 | */ |
| 59 | protected static function fail(int $status, string $message) |
| 60 | { |
| 61 | status_header($status); |
| 62 | nocache_headers(); |
| 63 | wp_die(esc_html($message), esc_html($message), ['response' => $status]); |
| 64 | } |
| 65 | } |
| 66 |