PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.2.5
Kirki – Freeform Page Builder, Website Builder & Customizer v6.2.5
6.3.0 6.2.5 6.2.4 6.2.3 6.2.2 6.2.1 6.2.0 6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / libraries / framework / ApiExceptionHandler.php
kirki / libraries / framework Last commit date
Collections 1 month 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 1 month ago Polyfill 1 month ago Routing 2 weeks ago Supports 2 weeks ago Validation 3 weeks ago View 1 month ago Wordpress 2 weeks ago ApiExceptionHandler.php 1 month ago Application.php 2 weeks ago Container.php 1 month ago CoreServiceProvider.php 2 weeks ago DTO.php 1 month ago Facade.php 1 month ago Listener.php 1 month ago Resource.php 1 month ago Route.php 2 weeks ago Sanitizer.php 1 month ago ServiceProvider.php 1 month ago SiteExceptionHandler.php 1 month ago helpers.php 2 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