1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 | package Torello.Browser;
import static Torello.Java.C.BGREEN_BKGND;
import static Torello.Java.C.RESET;
import Torello.Java.StorageWriter;
import Torello.Java.UnreachableError;
import Torello.Java.Additional.AppendableSafe;
import Torello.Java.Additional.Promise;
import javax.json.JsonObject;
import javax.json.JsonException;
// ************************************************************************************************
// ************************************************************************************************
// Chat-GPT wrote the summary below: July 20th, 2025 - VERY ACCURATE!
// ************************************************************************************************
// ************************************************************************************************
//
// VERSION #1
//
// Package-private class responsible for handling CDP command responses returned from the browser.
// When a client sends a command through WebSocketSender (e.g., Runtime.evaluate), this class is
// used to finalize the associated Promise by either resolving it with a result or rejecting it
// with an error code.
//
// This handler ensures that each sent command is paired with its corresponding JSON reply. It also
// handles diagnostic logging, error checking, and detection of malformed or incomplete responses.
//
//
// VERSION #2
//
// This class is a package-private utility used by the Browser WebSocket interface to handle
// incoming responses to previously sent commands. Its role is to inspect the returned JSON object,
// detect errors (if any), and then either resolve or reject a `Promise<JsonObject, ?>` based on
// the contents.
//
// The `handle(...)` method is invoked whenever Chrome DevTools Protocol (CDP) sends back a JSON
// response. It logs diagnostic output if `StorageWriter` logging is enabled, checks for a top-level
// "error" field, and safely completes the Promise object provided.
//
// NOTE: The logging uses color-coded terminal strings via Torello.Java.C, and error handling relies
// on throwing a `JsonException` or `UnreachableError` in cases where responses are malformed or
// critical assumptions are violated.
class HandleScriptResponse
{
// The Browser has sent a message about a particular Send-Request. Report this response
// to the promise.
static void handle(
final int idReceived,
final Promise<JsonObject, ?> promise,
final JsonObject jo,
final WebSocketSender THIS
)
{
final AppendableSafe app = THIS.app();
final AppendableSafe err = THIS.err();
app
.append(BGREEN_BKGND + " Browser Command Response: " + RESET)
.append(" Received ID: " + idReceived + ")\n");
// Check for errors first
final JsonObject error = jo.getJsonObject("error");
if (error != null)
{
final int code = error.getInt("code", -1);
final String errorMessage = error.getString("message", "[No Message Present]");
err
.append("Received Browser Error-Code\n")
.append(" Error Message: " + errorMessage + '\n')
.append(" Error Code: " + code + '\n');
// NOTE: This is *NOT* an 'RDPError' Instance, which are produced from mistakes in my
// code, this is a Browser-Transmitted Error. This means it is much more likely
// User Error (not guaranteed, I have not test everything in Torello.Browser)
//
// Unless I did smething wrong with my code, this is an error that has been caused by
// the clients interaction with the browser.
promise.acceptError(code, errorMessage);
}
else
{
final JsonObject commandResultJSON = jo.getJsonObject("result");
if (commandResultJSON == null)
{
final String errMsg =
"Response JSON Did not contain a 'result' Property\n";
// This is not something I am currently comprehending. I suspect (or I want to
// believe) that this entire 'if-branch' is dead-code / unreachable. If this is
// not Unreachable, I truly do not know whether this would be my mistake or the
// the End-User's mistake.
err.append(errMsg);
promise.acceptError(-1, errMsg);
}
// Everything went off
else promise.acceptResponse(commandResultJSON);
}
}
}
|