I have a Next.js app, in which I’m using RTK queries for data fetching. In my app, I am able to access the response data when the request is successful, but when I am sending an error code like 403 with some message in the response data, I am not able to access it.
What I am trying to achieve is to display the error message that is sent by the endpoint in the client.
Am I missing something to configure on the server or client?
const errorLogger =
({ dispatch }) =>
(next) =>
(action) => {
console.log('Action', action);
return next(action);
};
const store = configureStore({
reducer: rootReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: false
}).concat([Api.middleware, errorLogger])
});
This is how i am sending the request
const usersApi = Api.injectEndpoints({
endpoints: (build) => ({
createUser: build.mutation({
query: (arg) => {
return { url: paths.registerUser, body: arg, method: API_METHODS.POST };
},
forceRefetch: () => true
}),
}),
overrideExisting: true
});
This is what gets logged with RTK queries
{
"type": "api/executeMutation/rejected",
"payload": {
"status": "FETCH_ERROR",
"error": "TypeError: Failed to fetch"
},
"meta": {
"baseQueryMeta": {
"request": {}
},
"RTK_autoBatch": true,
"arg": {
"type": "mutation",
"endpointName": "createUser",
"originalArgs": {
“email”:”[email protected]”
"name": "admin55",
"isActive": true,
"password": "Password@123",
"company_name": "",
"organization_id": 1,
"address": "",
"bank_account": "",
"type": "admin",
"role_id": 2
},
"track": true
},
"requestId": "KMeIQgnpwBiGKg-89OEXk",
"rejectedWithValue": true,
"requestStatus": "rejected",
"aborted": false,
"condition": false
},
"error": {
"message": "Rejected"
}
}
what actually is sent by the endpoint
{
"status":403,
"data":{
"status":403
"message":"display this error in the client"
}
When I tried the same with Axios, I wasn’t able to access the payload with it either.
{
"message": "Network Error",
"name": "AxiosError",
"stack": "AxiosError: Network Errorn at XMLHttpRequest.handleError (webpack-
internal:///./node_modules/axios/lib/adapters/xhr.js:172:14)",
"config": {
"transitional": {
"silentJSONParsing": true,
"forcedJSONParsing": true,
"clarifyTimeoutError": false
},
"adapter": [
"xhr",
"http"
],
"transformRequest": [
null
],
"transformResponse": [
null
],
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"maxBodyLength": -1,
"env": {},
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json"
},
"method": "post",
"url": "https://dev-dispatch.bookmyride.eu:81/api/users/register",