Skip to content

No Content Response Handling #88

@WatchfulEyeOfZod

Description

@WatchfulEyeOfZod

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...

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestwipwork in progress

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions