PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.2.1
Kirki – Freeform Page Builder, Website Builder & Customizer v6.2.1
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 3 weeks ago Console 1 week ago Constants 1 month ago Contracts 1 week ago Database 1 week ago Discovery 1 month ago Exceptions 1 week ago Filesystem 1 week ago Http 1 week ago Managers 1 week ago Middlewares 1 month ago Polyfill 1 month ago Routing 1 week ago Supports 1 week ago Validation 3 days ago View 1 week ago Wordpress 1 week ago ApiExceptionHandler.php 1 month ago Application.php 1 week ago Container.php 1 month ago CoreServiceProvider.php 1 week ago DTO.php 1 month ago Facade.php 1 month ago Listener.php 3 weeks ago Resource.php 1 week ago Route.php 1 week ago Sanitizer.php 1 week ago ServiceProvider.php 1 month ago SiteExceptionHandler.php 1 week ago helpers.php 1 week 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