rtk-incubator / rtk-query

Data fetching and caching addon for Redux Toolkit
https://redux-toolkit.js.org/rtk-query/overview
MIT License
626 stars 31 forks source link

ValidateStatus => unable to validate response (server response 204) #122

Closed aloysb closed 3 years ago

aloysb commented 3 years ago

The following mutation gets rejected (I've rewritten it several times to ensure it works, this one is a coming from the docs):

 stripRoute: builder.mutation({
      query({ key }) {
        return {
          url: `routeBook/strip/${key}`,
          method: "PUT",
        };
      },
      invalidates: ["Routes"],
    }),

Action:

 {
  meta: {
    arg: {
      endpoint: 'stripRoute',
      internalQueryArgs: {
        url: 'routeBook/strip/27',
        method: 'PUT'
      },
      originalArgs: {
        key: 27
      },
      track: true,
      startedTimeStamp: 1608296390672
    },
    requestId: 'BZt7va1eGOpVeMNEXYbX9',
    rejectedWithValue: false,
    requestStatus: 'rejected',
    aborted: false,
    condition: false
  }
}

But the API is hit and updated, however the mutation returns 'rejected' which in turns doesn't trigger the invalidation.

aloysb commented 3 years ago

And the error from the devTools.

error{
  "name": "SyntaxError",
  "message": "Unexpected end of JSON input",
  "stack": "SyntaxError: Unexpected end of JSON input"
}

I'm unusure why. Other mutations seem to be working fine.

aloysb commented 3 years ago

I think the Mutation is rejected because the API doesn't return a body (204) (c.f. expected response from RTK - Q). So it might be normal behaviour.

I tried to force the validation of the mutation by adding:

validateStatus: (response) => response.status === 204

Did not work though. So the issue might be with validateStatus rather than unexpected rejection.

As a work around, I am just forcing the 'refetch' on this particular Mutation, just curious to understand why it doesn't work.

phryneas commented 3 years ago

That sounds like https://github.com/rtk-incubator/rtk-query/issues/89

You can use the current dev build from https://ci.codesandbox.io/status/rtk-incubator/rtk-query/pr/1 that already includes a patch.

aloysb commented 3 years ago

That's right, same issue.

I'll close this. However, why the validateStatus function wouldn't 'force' its way through?

I'll rename the issue so people can find it.

phryneas commented 3 years ago

no matter if you validate or not, if the parsing of the response literally crashes, it wasn't successful as you're not able to access the result.