Collections
1 month ago
Concerns
2 weeks ago
Console
4 days ago
Constants
1 month ago
Contracts
4 days ago
Database
4 days ago
Discovery
1 month ago
Exceptions
4 days ago
Filesystem
4 days ago
Http
4 days ago
Managers
4 days ago
Middlewares
1 month ago
Polyfill
1 month ago
Routing
4 days ago
Supports
4 days ago
Validation
4 days ago
View
4 days ago
Wordpress
4 days ago
ApiExceptionHandler.php
1 month ago
Application.php
4 days ago
Container.php
1 month ago
CoreServiceProvider.php
4 days ago
DTO.php
1 month ago
Facade.php
1 month ago
Listener.php
2 weeks ago
Resource.php
4 days ago
Route.php
4 days ago
Sanitizer.php
4 days ago
ServiceProvider.php
1 month ago
SiteExceptionHandler.php
4 days ago
helpers.php
4 days 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 |