Collections
3 weeks ago
Concerns
3 weeks ago
Console
3 weeks ago
Constants
3 weeks ago
Contracts
3 weeks ago
Database
3 weeks ago
Discovery
3 weeks ago
Exceptions
3 weeks ago
Filesystem
3 weeks ago
Http
3 weeks ago
Managers
3 weeks ago
Middlewares
3 weeks ago
Polyfill
3 weeks ago
Supports
3 weeks ago
Validation
3 weeks ago
Wordpress
3 weeks ago
ApiExceptionHandler.php
3 weeks ago
Application.php
3 weeks ago
Container.php
3 weeks ago
CoreServiceProvider.php
3 weeks ago
DTO.php
3 weeks ago
Facade.php
3 weeks ago
Listener.php
3 weeks ago
Resource.php
3 weeks ago
Route.php
3 weeks ago
Sanitizer.php
3 weeks ago
ServiceProvider.php
3 weeks ago
helpers.php
3 weeks ago
ApiExceptionHandler.php
62 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Translates thrown exceptions into consistent JSON REST responses for the API layer. |
| 5 | * Maps ValidationException and ModelNotFoundException to appropriate HTTP status codes with structured error payloads. |
| 6 | * Falls back to a generic JSON error using the exception code when no specialized handler applies. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @since 1.0.0 |
| 10 | */ |
| 11 | namespace Kirki\Framework; |
| 12 | |
| 13 | \defined('ABSPATH') || exit; |
| 14 | use Kirki\Framework\Exceptions\ModelNotFoundException; |
| 15 | use Kirki\Framework\Exceptions\ValidationException; |
| 16 | use Kirki\Framework\Http\Response; |
| 17 | use Exception; |
| 18 | use function Kirki\Framework\response; |
| 19 | class ApiExceptionHandler |
| 20 | { |
| 21 | /** |
| 22 | * Get the response for the exception. |
| 23 | * |
| 24 | * @param Exception $exception The exception to get the response for. |
| 25 | * |
| 26 | * @return Response The response for the exception. |
| 27 | * |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | public static function get_response(Exception $exception) |
| 31 | { |
| 32 | if ($exception instanceof ValidationException) { |
| 33 | $response = ['message' => $exception->getMessage(), 'errors' => $exception->get_errors()]; |
| 34 | return response()->json($response, Response::UNPROCESSABLE_ENTITY); |
| 35 | } |
| 36 | if ($exception instanceof ModelNotFoundException) { |
| 37 | $response = ['message' => $exception->getMessage()]; |
| 38 | return response()->json($response, Response::NOT_FOUND); |
| 39 | } |
| 40 | return static::fallback_response($exception); |
| 41 | } |
| 42 | /** |
| 43 | * Get the fallback response for the exception. |
| 44 | * |
| 45 | * @param Exception $exception The exception to get the fallback response for. |
| 46 | * |
| 47 | * @return Response The fallback response for the exception. |
| 48 | * |
| 49 | * @since 1.0.0 |
| 50 | */ |
| 51 | protected static function fallback_response(Exception $exception) |
| 52 | { |
| 53 | $status_code = (int) $exception->getCode(); |
| 54 | if ($status_code < 100 || $status_code > 599) { |
| 55 | $status_code = Response::INTERNAL_SERVER_ERROR; |
| 56 | // fallback to 500 |
| 57 | } |
| 58 | $response = ['message' => $exception->getMessage()]; |
| 59 | return response()->json($response, $status_code); |
| 60 | } |
| 61 | } |
| 62 |