| | |
| | | <?php |
| | | namespace JVBase\rest\routes; |
| | | |
| | | use JVBase\JVB; |
| | | use JVBase\rest\RestRouteManager; |
| | | use JVBase\rest\Response; |
| | | use JVBase\rest\Rest; |
| | | use JVBase\rest\Route; |
| | | use WP_REST_Request; |
| | | use WP_REST_Response; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | | exit; // Exit if accessed directly |
| | | } |
| | | class ErrorRoutes extends RestRouteManager |
| | | class ErrorRoutes extends Rest |
| | | { |
| | | /** |
| | | * Registers error routes |
| | |
| | | */ |
| | | public function registerRoutes():void |
| | | { |
| | | register_rest_route($this->namespace, '/errors/log', [ |
| | | [ |
| | | 'methods' => 'POST', |
| | | 'callback' => [$this, 'handleErrorLog'], |
| | | 'permission_callback' => '__return_true', // Allow anyone to log errors |
| | | ] |
| | | ]); |
| | | Route::for('errors/log') |
| | | ->post([$this, 'handleErrorLog']) |
| | | ->args([ |
| | | 'error_type' => 'string|required|enum:network,timeout,offline,auth,rate_limit,server,client,unknown', |
| | | 'message' => 'string|required', |
| | | 'context' => 'string', |
| | | ]) |
| | | ->auth('public') |
| | | ->rateLimit(10); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public function handleErrorLog(WP_REST_Request$request):WP_REST_Response |
| | | { |
| | | $error_type = $request->get_param('error_type'); |
| | | $message = $request->get_param('message'); |
| | | $context = json_decode($request->get_param('context'), true); |
| | | $error_type = sanitize_text_field($request->get_param('error_type')); |
| | | $message = sanitize_text_field($request->get_param('message')); |
| | | $context = $request->get_param('context'); |
| | | |
| | | // Parse context JSON if provided |
| | | $contextData = []; |
| | | if (!empty($context)) { |
| | | $decoded = json_decode($context, true); |
| | | $contextData = is_array($decoded) ? $decoded : []; |
| | | } |
| | | |
| | | // Determine severity based on error type |
| | | $severity = $this->getSeverityFromType($error_type); |
| | | |
| | | JVB()->error()->log( |
| | | $context['component'] ?? 'client-js', |
| | | $contextData['component'] ?? 'client-js', |
| | | $message, |
| | | $context, |
| | | $contextData, |
| | | $severity |
| | | ); |
| | | |
| | | return new WP_REST_Response([ |
| | | 'success' => true, |
| | | 'message' => 'Error logged' |
| | | ]); |
| | | return Response::success(['message'=>'Error logged']); |
| | | } |
| | | |
| | | /** |