Skip to content

Commit ff88a8f

Browse files
committed
XMLHttpRequest - several fixes to throw the correct error
1 parent 1de2d0f commit ff88a8f

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/changes/changes.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
<body>
1010
<release version="4.10.0" date="February xx, 2025" description="Bugfixes">
11+
<action type="fix" dev="rbri">
12+
XMLHttpRequest - several fixes to throw the correct error.
13+
</action>
1114
<action type="fix" dev="rbri">
1215
Geolocation.ctor() throws a TypeError.
1316
</action>
@@ -51,7 +54,7 @@
5154
CanvasRenderingContext2D.measureText() throws a TypeError.
5255
</action>
5356
<action type="fix" dev="rbri">
54-
HTMLTableElement/HTMLTableRowElement several fixes to throw the correct error.
57+
HTMLTableElement/HTMLTableRowElement - several fixes to throw the correct error.
5558
</action>
5659
<action type="fix" dev="rbri">
5760
Element.querySelectorAll()/querySelector() throws a SyntaxError.

src/main/java/org/htmlunit/javascript/host/xml/XMLHttpRequest.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import org.htmlunit.javascript.configuration.JsxSetter;
7979
import org.htmlunit.javascript.host.URLSearchParams;
8080
import org.htmlunit.javascript.host.Window;
81+
import org.htmlunit.javascript.host.dom.DOMException;
8182
import org.htmlunit.javascript.host.dom.DOMParser;
8283
import org.htmlunit.javascript.host.dom.Document;
8384
import org.htmlunit.javascript.host.event.Event;
@@ -278,8 +279,11 @@ public void setResponseType(final String responseType) {
278279
|| RESPONSE_TYPE_TEXT.equals(responseType)) {
279280

280281
if (state_ == OPENED && !async_) {
281-
throw JavaScriptEngine.reportRuntimeError(
282-
"InvalidAccessError: synchronous XMLHttpRequests do not support responseType");
282+
throw JavaScriptEngine.asJavaScriptException(
283+
getWindow(),
284+
new DOMException(
285+
"synchronous XMLHttpRequests do not support responseType",
286+
DOMException.INVALID_ACCESS_ERR));
283287
}
284288

285289
responseType_ = responseType;
@@ -450,10 +454,13 @@ public String getResponseText() {
450454
}
451455

452456
if (!RESPONSE_TYPE_DEFAULT.equals(responseType_) && !RESPONSE_TYPE_TEXT.equals(responseType_)) {
453-
throw JavaScriptEngine.reportRuntimeError(
454-
"InvalidStateError: Failed to read the 'responseText' property from 'XMLHttpRequest': "
455-
+ "The value is only accessible if the object's 'responseType' is '' or 'text' "
456-
+ "(was '" + getResponseType() + "').");
457+
throw JavaScriptEngine.asJavaScriptException(
458+
getWindow(),
459+
new DOMException(
460+
"InvalidStateError: Failed to read the 'responseText' property from 'XMLHttpRequest': "
461+
+ "The value is only accessible if the object's 'responseType' is '' or 'text' "
462+
+ "(was '" + getResponseType() + "').",
463+
DOMException.INVALID_STATE_ERR));
457464
}
458465

459466
if (state_ == UNSENT || state_ == OPENED) {
@@ -953,8 +960,7 @@ void doSend() {
953960
if (LOG.isDebugEnabled()) {
954961
LOG.debug("No permitted request for URL " + webRequest_.getUrl());
955962
}
956-
throw JavaScriptEngine.throwAsScriptRuntimeEx(
957-
new RuntimeException("No permitted \"Access-Control-Allow-Origin\" header."));
963+
throw JavaScriptEngine.networkError("No permitted \"Access-Control-Allow-Origin\" header.");
958964
}
959965
}
960966

@@ -1075,7 +1081,7 @@ public Charset getContentCharset() {
10751081
fireJavascriptEvent(Event.TYPE_LOAD_END);
10761082
}
10771083

1078-
throw JavaScriptEngine.throwAsScriptRuntimeEx(e);
1084+
throw JavaScriptEngine.networkError(e.getMessage());
10791085
}
10801086
}
10811087
}
@@ -1202,7 +1208,11 @@ static boolean isAuthorizedHeader(final String name) {
12021208
@JsxFunction
12031209
public void overrideMimeType(final String mimeType) {
12041210
if (state_ != UNSENT && state_ != OPENED) {
1205-
throw JavaScriptEngine.reportRuntimeError("Property 'overrideMimeType' not writable after sent.");
1211+
throw JavaScriptEngine.asJavaScriptException(
1212+
getWindow(),
1213+
new DOMException(
1214+
"Property 'overrideMimeType' not writable after sent.",
1215+
DOMException.INVALID_STATE_ERR));
12061216
}
12071217
overriddenMimeType_ = mimeType;
12081218
}

0 commit comments

Comments
 (0)