Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put error response interceptor on redux-axios-middleware

I have a problem with https://github.com/svrcekmichal/redux-axios-middleware.

I want to set the interceptor response (error). But can't successfully set it up.

Here is my code:

function interceptorResponse({ dispatch, getState, getAction }, response) {
    console.log(response);
}

export const client = axios.create({
    baseURL: API_URL,
    headers: {
        Accept: 'application/json',
    },
});

export const clientOptions = {
    interceptors: {
        request: [interceptorRequest],
        response: [interceptorResponse],
    },
};

the console.log(response) only respond if the response is 200. How can I set it to accept an error response?

I've tried set it like this

 function interceptorResponse({ dispatch, getState, getAction }) {
    return response => response.data, (error) => {
        const meta = error.response.data.meta;

        const { code, status } = meta;

        console.log(meta);
    };
}

but still never show anything.

Any soluion?

like image 950
ssuhat Avatar asked Apr 21 '17 05:04

ssuhat


1 Answers

Here is an example usage with ES6 :

import axios from 'axios'  
import axiosMiddleware from 'redux-axios-middleware'    

const options = {
        // not required, but use-full configuration option
        returnRejectedPromiseOnError: true,
        interceptors: {
            request: [
                ({ getState, dispatch }, config) => {
                   // Request interception
                  return config
                }
            ],
            response: [
            {
              success: ({ dispatch }, response) => {
                // Response interception
                return response
              },
              error: ({ dispatch }, error) => {
                // Response Error Interception 
                return Promise.reject(error)
              }
            }
          ]
        }
}    
export default axiosMiddleware(axios, options)

Note that the created middleware should be passed to createStore()

like image 200
Miroslav Genov Avatar answered Oct 31 '22 13:10

Miroslav Genov