API error status code with fetch's POST method

I use fetch API to fetch data from our server with the POST method.

It seems that fetch API throws a " Network request failed" error every time the status code is 4XX. However, I need these codes in order to show the appropriate message to our users. Otherwise, the UX flow of our plugin could end confusing.

Btw, the GET method gives me the correct error codes.

Is there a way to access the exact status code of the error?

1 Like

Welcome to the forum!

I don’t mean to be cheeky, but aren’t you abusing data fetching by using POST instead of GET? Or are you actually changing something?

1 Like

Thank you! I’m glad to be part of this community!

Sorry for not clarifying this point. I use POST to send a Base64 image to the server and I want to get back the transformed image or the error status code in case of an error.

1 Like

Hi @dmraptis (also from me: Welcome to the forum :wave::slightly_smiling_face:)

According to Network APIs · Adobe XD Plugin Reference, XD’s fetch implementation uses GitHub - JakeChampion/fetch: A window.fetch JavaScript polyfill. as a sort of Polyfill on top of XHR to provide the fetch function. In the documentation of said library (more specifically: the README), it says that error codes have to manually get filtered (cf. GitHub - JakeChampion/fetch: A window.fetch JavaScript polyfill.), which, apperently, XD’s implementation does. Since this, then, appears to be an undocumented kind of error (there is no specification of such an error in the XD docs, meaning fetch shouldn’t actually reject in case of a 4XX or 5XX response).

If there is no status property in the Response (according to MDN, it actually shouldn’t reject even if there is a status code other than 2XX), as specified in the MDN docs, this is undocumented and your safest bet therefore would probably be to, for now, use an XMLHttpRequest

1 Like

Thank you a lot for your extensive response! I’ll use an XMLHttpRequest for now.