PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.1.1
Kirki – Freeform Page Builder, Website Builder & Customizer v6.1.1
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 3 weeks ago Concerns 1 week ago Console 3 weeks ago Constants 3 weeks ago Contracts 3 weeks ago Database 1 week ago Discovery 3 weeks ago Exceptions 3 weeks ago Filesystem 3 weeks ago Http 3 weeks ago Managers 1 week 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 1 week ago Resource.php 1 week 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