Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] http XHR request differences in error handling between our code an MDN example #8879

Open
pmario opened this issue Jan 6, 2025 · 4 comments

Comments

@pmario
Copy link
Member

pmario commented Jan 6, 2025

Describe the bug

At http.js request.onreadystatechange we use:

if(this.status >= 200 && this.status < 300) {

MDN Example shows:

    if (status === 0 || (status >= 200 && status < 400)) {
      // The request has been completed successfully
      console.log(xhr.responseText);
    } else {
      // Oh no! There has been an error with the request!
    }

Expected behavior

We should probably also use if (status === 0 || (status >= 200 && status < 400)) {

I am pretty sure we should use status === 0
I am not so sure about < 400 ... This may cause backwards compatibility problems

To Reproduce

Test this code:

<$let store-fetched-output="""\procedure store-fetched-output()
<$action-setfield $tiddler=Output status=<<status>> error=<<error>> data=<<data>> headers=<<headers>>/>
\end
""">
<$testcase>
<$data title=Description text="Simple tm-http-request GET"/>
<$data title=Narrative text="""Use the oncompletion attribute to store the results of a method="GET" request"""/>
<$data title=Output text=`$(store-fetched-output)$
\procedure http-get()
	<$action-sendmessage
		$message="tm-http-request"
		url="https://google.com"
		method="GET"
		oncompletion=<<store-fetched-output>>
	/>
\end

<$button actions=<<http-get>>>send HTTP GET</$button>`/>
</$testcase>

It has XHR request status === 0

The browser network tab shows a 201, but FF creates NS_ERROR_UNEXPECTED

Screenshots

image

TiddlyWiki Configuration

TW 5.3.6

Additional context

Related to Talk: https://talk.tiddlywiki.org/t/how-to-use-tm-http-request-to-fetch-data-from-sites-into-tiddlywiki/11550/6?u=pmario

@Jermolene
Copy link
Member

Hi @pmario HTTP result codes in the range 300-399 are used for various kinds of redirects. XMLHttpRequest automatically follows any redirects silently, and doesn't expose the redirects to JS code. So, while it is technically true that result codes in this range should be treated as success, they are never visible to JS code using XMLHttpRequest

@pmario
Copy link
Member Author

pmario commented Jan 7, 2025

OK -- I agree with the 300 codes. -- But what's with status === 0, which will be returned if eg: CORS access fails. As shown in the "To Reproduce" section and the screenshot. We do get status = 0 here.

It shows an error text, but with not all too much info. I know, that the test-case widget is no debugger, but may be we should return eg: "Use browser debug tools F12 to get more info" or something similar, if we encounter status=0

image

@Jermolene
Copy link
Member

Hi @pmario I am not sure I understand. From your description, we see status = 0 when access is prevented by CORS. But the MDN code counts status = 0 as success.

@pmario
Copy link
Member Author

pmario commented Jan 9, 2025

You are right. That's strange. With further digging at: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/status In the second quoted paragraph it says this:

The read-only XMLHttpRequest.status property returns the numerical HTTP status code of the XMLHttpRequest's response.

Before the request completes, the value of status is 0. Browsers also report a status of 0 in case of XMLHttpRequest errors.

So status 0 seems to be a "general -- something (not specified) is wrong" status. There no more info at: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors

Not sure, what to do yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants