Skip to content

Commit

Permalink
XMLHttpRequest - several fixes to throw the correct error
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Feb 2, 2025
1 parent 1de2d0f commit ff88a8f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

<body>
<release version="4.10.0" date="February xx, 2025" description="Bugfixes">
<action type="fix" dev="rbri">
XMLHttpRequest - several fixes to throw the correct error.
</action>
<action type="fix" dev="rbri">
Geolocation.ctor() throws a TypeError.
</action>
Expand Down Expand Up @@ -51,7 +54,7 @@
CanvasRenderingContext2D.measureText() throws a TypeError.
</action>
<action type="fix" dev="rbri">
HTMLTableElement/HTMLTableRowElement several fixes to throw the correct error.
HTMLTableElement/HTMLTableRowElement - several fixes to throw the correct error.
</action>
<action type="fix" dev="rbri">
Element.querySelectorAll()/querySelector() throws a SyntaxError.
Expand Down
30 changes: 20 additions & 10 deletions src/main/java/org/htmlunit/javascript/host/xml/XMLHttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import org.htmlunit.javascript.configuration.JsxSetter;
import org.htmlunit.javascript.host.URLSearchParams;
import org.htmlunit.javascript.host.Window;
import org.htmlunit.javascript.host.dom.DOMException;
import org.htmlunit.javascript.host.dom.DOMParser;
import org.htmlunit.javascript.host.dom.Document;
import org.htmlunit.javascript.host.event.Event;
Expand Down Expand Up @@ -278,8 +279,11 @@ public void setResponseType(final String responseType) {
|| RESPONSE_TYPE_TEXT.equals(responseType)) {

if (state_ == OPENED && !async_) {
throw JavaScriptEngine.reportRuntimeError(
"InvalidAccessError: synchronous XMLHttpRequests do not support responseType");
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new DOMException(
"synchronous XMLHttpRequests do not support responseType",
DOMException.INVALID_ACCESS_ERR));
}

responseType_ = responseType;
Expand Down Expand Up @@ -450,10 +454,13 @@ public String getResponseText() {
}

if (!RESPONSE_TYPE_DEFAULT.equals(responseType_) && !RESPONSE_TYPE_TEXT.equals(responseType_)) {
throw JavaScriptEngine.reportRuntimeError(
"InvalidStateError: Failed to read the 'responseText' property from 'XMLHttpRequest': "
+ "The value is only accessible if the object's 'responseType' is '' or 'text' "
+ "(was '" + getResponseType() + "').");
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new DOMException(
"InvalidStateError: Failed to read the 'responseText' property from 'XMLHttpRequest': "
+ "The value is only accessible if the object's 'responseType' is '' or 'text' "
+ "(was '" + getResponseType() + "').",
DOMException.INVALID_STATE_ERR));
}

if (state_ == UNSENT || state_ == OPENED) {
Expand Down Expand Up @@ -953,8 +960,7 @@ void doSend() {
if (LOG.isDebugEnabled()) {
LOG.debug("No permitted request for URL " + webRequest_.getUrl());
}
throw JavaScriptEngine.throwAsScriptRuntimeEx(
new RuntimeException("No permitted \"Access-Control-Allow-Origin\" header."));
throw JavaScriptEngine.networkError("No permitted \"Access-Control-Allow-Origin\" header.");
}
}

Expand Down Expand Up @@ -1075,7 +1081,7 @@ public Charset getContentCharset() {
fireJavascriptEvent(Event.TYPE_LOAD_END);
}

throw JavaScriptEngine.throwAsScriptRuntimeEx(e);
throw JavaScriptEngine.networkError(e.getMessage());
}
}
}
Expand Down Expand Up @@ -1202,7 +1208,11 @@ static boolean isAuthorizedHeader(final String name) {
@JsxFunction
public void overrideMimeType(final String mimeType) {
if (state_ != UNSENT && state_ != OPENED) {
throw JavaScriptEngine.reportRuntimeError("Property 'overrideMimeType' not writable after sent.");
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new DOMException(
"Property 'overrideMimeType' not writable after sent.",
DOMException.INVALID_STATE_ERR));
}
overriddenMimeType_ = mimeType;
}
Expand Down

0 comments on commit ff88a8f

Please sign in to comment.