$status, ]; if ( $details !== null ) { $data['details'] = $details; } return new WP_Error( $code, $message, $data ); } /** * Format exception as REST error * * @param \Throwable $e Throwable. * @return WP_Error */ public static function exceptionToRestError( \Throwable $e ): WP_Error { $message = $e->getMessage(); $code = $e->getCode() ?: 500; // Log the full exception. Log::error( 'Exception caught', [ 'message' => $message, 'code' => $code, 'file' => $e->getFile(), 'line' => $e->getLine(), 'trace' => $e->getTraceAsString(), ] ); // The full trace is already recorded server-side via Log::error above. // Never return it to the client: a stack trace exposes file paths and // call-site arguments (which can include secrets such as API keys) in // the HTTP response body, regardless of WP_DEBUG. return self::createRestError( 'aisentic_exception', $message, null, is_int( $code ) && $code >= 400 && $code < 600 ? $code : 500 ); } }