From 14ff728870af76b1ae64b97c8b1884f573069005 Mon Sep 17 00:00:00 2001 From: Udhay-Adithya Date: Fri, 16 May 2025 14:30:58 +0530 Subject: [PATCH] fix: JS bridge message handling and script execution checks Refines JS bridge handlers to process messages directly as typed arguments, removing unnecessary JSON decoding and clarifying error logs. Updates logic to only execute post-response scripts when present and corrects a warning message. --- lib/providers/collection_providers.dart | 6 ++-- lib/services/flutter_js_service.dart | 44 ++++++++++++------------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/lib/providers/collection_providers.dart b/lib/providers/collection_providers.dart index afa5580b..f071002d 100644 --- a/lib/providers/collection_providers.dart +++ b/lib/providers/collection_providers.dart @@ -386,7 +386,7 @@ class CollectionStateNotifier "Skipped environment update as originalEnvironmentModel was null."); if (scriptResult.updatedEnvironment.isNotEmpty) { debugPrint( - "Warning: Pre-request script updated environment variables, but no active environment was selected to save them to."); + "Warning: Post-response script updated environment variables, but no active environment was selected to save them to."); } } } @@ -467,7 +467,9 @@ class CollectionStateNotifier httpRequestModel: substitutedHttpRequestModel, httpResponseModel: httpResponseModel, ); - handlePostResponseScript(newRequestModel, originalEnvironmentModel); + if (requestModel.postRequestScript.isNotEmpty) { + handlePostResponseScript(newRequestModel, originalEnvironmentModel); + } ref.read(historyMetaStateNotifier.notifier).addHistoryRequest(model); } diff --git a/lib/services/flutter_js_service.dart b/lib/services/flutter_js_service.dart index dc8ff24e..48411416 100644 --- a/lib/services/flutter_js_service.dart +++ b/lib/services/flutter_js_service.dart @@ -34,55 +34,54 @@ void evaluate(String code) { void setupJsBridge() { jsRuntime.onMessage('consoleLog', (args) { try { - final decodedArgs = jsonDecode(args as String); - if (decodedArgs is List) { - print('[JS LOG]: ${decodedArgs.map((e) => e.toString()).join(' ')}'); + if (args is List) { + print('[JS LOG]: ${args.map((e) => e.toString()).join(' ')}'); } else { - print('[JS LOG]: $decodedArgs'); + print('[JS LOG]: $args'); } } catch (e) { - print('[JS LOG ERROR decoding]: $args, Error: $e'); + print('[JS LOG ERROR]: $args, Error: $e'); } }); jsRuntime.onMessage('consoleWarn', (args) { try { - final decodedArgs = jsonDecode(args as String); - if (decodedArgs is List) { - print('[JS WARN]: ${decodedArgs.map((e) => e.toString()).join(' ')}'); + if (args is List) { + print('[JS WARN]: ${args.map((e) => e.toString()).join(' ')}'); } else { - print('[JS WARN]: $decodedArgs'); + print('[JS WARN]: $args'); } } catch (e) { - print('[JS WARN ERROR decoding]: $args, Error: $e'); + print('[JS WARN ERROR]: $args, Error: $e'); } }); jsRuntime.onMessage('consoleError', (args) { try { - final decodedArgs = jsonDecode(args as String); - if (decodedArgs is List) { - print('[JS ERROR]: ${decodedArgs.map((e) => e.toString()).join(' ')}'); + if (args is List) { + print('[JS ERROR]: ${args.map((e) => e.toString()).join(' ')}'); } else { - print('[JS ERROR]: $decodedArgs'); + print('[JS ERROR]: $args'); } } catch (e) { - print('[JS ERROR ERROR decoding]: $args, Error: $e'); + print('[JS ERROR ERROR]: $args, Error: $e'); } }); jsRuntime.onMessage('fatalError', (args) { try { - final errorDetails = jsonDecode(args as String); - print('[JS FATAL ERROR]: ${errorDetails['message']}'); - if (errorDetails['error']) print(' Error: ${errorDetails['error']}'); - if (errorDetails['stack']) print(' Stack: ${errorDetails['stack']}'); + // 'fatalError' message is constructed as a JSON object in setupScript + if (args is Map) { + print('[JS FATAL ERROR]: ${args['message']}'); + if (args['error'] != null) print(' Error: ${args['error']}'); + if (args['stack'] != null) print(' Stack: ${args['stack']}'); + } else { + print('[JS FATAL ERROR decoding error]: $args, Expected a Map.'); + } } catch (e) { print('[JS FATAL ERROR decoding error]: $args, Error: $e'); } }); - - //TODO: Add handlers for 'testResult' } Future< @@ -154,6 +153,8 @@ Future< try { resultingRequest = HttpRequestModel.fromJson( Map.from(resultMap['request'])); + log(resultingRequest.toString()); + log("Resmap req/; ${resultMap['request'].toString()}"); } catch (e) { print("Error deserializing modified request from script: $e"); //TODO: Handle error - maybe keep original request? @@ -242,7 +243,6 @@ Future< // TODO: Handle error - maybe show in UI, keep original request/env } else if (result.stringResult.isNotEmpty) { final resultMap = jsonDecode(result.stringResult); - log(resultMap['response'].toString()); if (resultMap is Map) { // Deserialize Request if (resultMap.containsKey('response') && resultMap['response'] is Map) {