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