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.
This commit is contained in:
Udhay-Adithya
2025-05-16 14:30:58 +05:30
parent c2c67a7fca
commit 14ff728870
2 changed files with 26 additions and 24 deletions

View File

@@ -386,7 +386,7 @@ class CollectionStateNotifier
"Skipped environment update as originalEnvironmentModel was null."); "Skipped environment update as originalEnvironmentModel was null.");
if (scriptResult.updatedEnvironment.isNotEmpty) { if (scriptResult.updatedEnvironment.isNotEmpty) {
debugPrint( 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, httpRequestModel: substitutedHttpRequestModel,
httpResponseModel: httpResponseModel, httpResponseModel: httpResponseModel,
); );
handlePostResponseScript(newRequestModel, originalEnvironmentModel); if (requestModel.postRequestScript.isNotEmpty) {
handlePostResponseScript(newRequestModel, originalEnvironmentModel);
}
ref.read(historyMetaStateNotifier.notifier).addHistoryRequest(model); ref.read(historyMetaStateNotifier.notifier).addHistoryRequest(model);
} }

View File

@@ -34,55 +34,54 @@ void evaluate(String code) {
void setupJsBridge() { void setupJsBridge() {
jsRuntime.onMessage('consoleLog', (args) { jsRuntime.onMessage('consoleLog', (args) {
try { try {
final decodedArgs = jsonDecode(args as String); if (args is List) {
if (decodedArgs is List) { print('[JS LOG]: ${args.map((e) => e.toString()).join(' ')}');
print('[JS LOG]: ${decodedArgs.map((e) => e.toString()).join(' ')}');
} else { } else {
print('[JS LOG]: $decodedArgs'); print('[JS LOG]: $args');
} }
} catch (e) { } catch (e) {
print('[JS LOG ERROR decoding]: $args, Error: $e'); print('[JS LOG ERROR]: $args, Error: $e');
} }
}); });
jsRuntime.onMessage('consoleWarn', (args) { jsRuntime.onMessage('consoleWarn', (args) {
try { try {
final decodedArgs = jsonDecode(args as String); if (args is List) {
if (decodedArgs is List) { print('[JS WARN]: ${args.map((e) => e.toString()).join(' ')}');
print('[JS WARN]: ${decodedArgs.map((e) => e.toString()).join(' ')}');
} else { } else {
print('[JS WARN]: $decodedArgs'); print('[JS WARN]: $args');
} }
} catch (e) { } catch (e) {
print('[JS WARN ERROR decoding]: $args, Error: $e'); print('[JS WARN ERROR]: $args, Error: $e');
} }
}); });
jsRuntime.onMessage('consoleError', (args) { jsRuntime.onMessage('consoleError', (args) {
try { try {
final decodedArgs = jsonDecode(args as String); if (args is List) {
if (decodedArgs is List) { print('[JS ERROR]: ${args.map((e) => e.toString()).join(' ')}');
print('[JS ERROR]: ${decodedArgs.map((e) => e.toString()).join(' ')}');
} else { } else {
print('[JS ERROR]: $decodedArgs'); print('[JS ERROR]: $args');
} }
} catch (e) { } catch (e) {
print('[JS ERROR ERROR decoding]: $args, Error: $e'); print('[JS ERROR ERROR]: $args, Error: $e');
} }
}); });
jsRuntime.onMessage('fatalError', (args) { jsRuntime.onMessage('fatalError', (args) {
try { try {
final errorDetails = jsonDecode(args as String); // 'fatalError' message is constructed as a JSON object in setupScript
print('[JS FATAL ERROR]: ${errorDetails['message']}'); if (args is Map<String, dynamic>) {
if (errorDetails['error']) print(' Error: ${errorDetails['error']}'); print('[JS FATAL ERROR]: ${args['message']}');
if (errorDetails['stack']) print(' Stack: ${errorDetails['stack']}'); 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) { } catch (e) {
print('[JS FATAL ERROR decoding error]: $args, Error: $e'); print('[JS FATAL ERROR decoding error]: $args, Error: $e');
} }
}); });
//TODO: Add handlers for 'testResult'
} }
Future< Future<
@@ -154,6 +153,8 @@ Future<
try { try {
resultingRequest = HttpRequestModel.fromJson( resultingRequest = HttpRequestModel.fromJson(
Map<String, Object?>.from(resultMap['request'])); Map<String, Object?>.from(resultMap['request']));
log(resultingRequest.toString());
log("Resmap req/; ${resultMap['request'].toString()}");
} catch (e) { } catch (e) {
print("Error deserializing modified request from script: $e"); print("Error deserializing modified request from script: $e");
//TODO: Handle error - maybe keep original request? //TODO: Handle error - maybe keep original request?
@@ -242,7 +243,6 @@ Future<
// TODO: Handle error - maybe show in UI, keep original request/env // TODO: Handle error - maybe show in UI, keep original request/env
} else if (result.stringResult.isNotEmpty) { } else if (result.stringResult.isNotEmpty) {
final resultMap = jsonDecode(result.stringResult); final resultMap = jsonDecode(result.stringResult);
log(resultMap['response'].toString());
if (resultMap is Map<String, dynamic>) { if (resultMap is Map<String, dynamic>) {
// Deserialize Request // Deserialize Request
if (resultMap.containsKey('response') && resultMap['response'] is Map) { if (resultMap.containsKey('response') && resultMap['response'] is Map) {