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

No Content Response Handling #88

Open
WatchfulEyeOfZod opened this issue Dec 19, 2024 · 0 comments
Open

No Content Response Handling #88

WatchfulEyeOfZod opened this issue Dec 19, 2024 · 0 comments

Comments

@WatchfulEyeOfZod
Copy link

In certain cases, when the backend legitimately returns a 204 No Content from is call (for example, when uploading to S3), the front end fails to see that as a success due to the following code in ajax.js

        const response = await this.customFetch(url, init);
        if (response.ok) {
            return await response[responseType]();
        }
        throw await response.json();

This attempts to perform a response.json() on the response with no body.

While this can be fixed with the fetchResponseInterceptor as follows, it is messy and it would be cleaner to simply handle this in the ajax.js code with a conditional as follows:

        const response = await this.customFetch(url, init);
        if (response.ok) {
            // Handle Empty Reponses
            if(response.status === 204 || response.status === 304) {
                 return {}
            }
            return await response[responseType]();
        }
        throw await response.json();

The much more complex version with the fetchResponseInterceptor

		fetchResponseInterceptor: async function fetchResponseInterceptor(originalResponse) {
		  if (originalResponse.status === 204 || originalResponse.status === 304) {
		    // Create a new valid Response with no body
		    const responseWithNoBody = new Response(null, {
		      status: originalResponse.status,
		      statusText: originalResponse.statusText,
		      headers: originalResponse.headers,
		    });
		    // Patch `.json()` to return an empty object when called
		    responseWithNoBody.json = () => Promise.resolve({});
		    return responseWithNoBody; // Return the patched response
		  }
		  // For non-204/304, return the original response as is
		  return originalResponse;
		},

It would be nice to handle the 204 gracefully in the vuefinder code vs. the application...

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

1 participant