0

Running the following code:

import axios from 'axios';
import config from '../config';

const login = code => ({
  type: 'SESSION',
  payload: new Promise((resolve, reject) => {
    return axios({
      method: 'post',
      responseType: 'json',
      url: `${config.api.url}/auth`,
      data: {
        code
      }
    })
    .then((response) => {
      console.log('success');
      return resolve(response.data);
    })
    .catch((error) => {
      console.log('error', error.response.data);
      return reject(error.response.data); // something going wrong here
    });
  })
});

export {
  login
};

Causes the following error:

Uncaught (in promise) {status: 500, error: "Something went wrong on the server.", type: "internalServerError"}

The status 500 is on purpose, to debug my error handling. However, there is a problem with rejecting the error.

The Promise looks valid to me. Am I missing something here?

Lars
  • 437
  • 1
  • 5
  • 11
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Sep 03 '17 at 15:02
  • 1
    No, there likely is no problem with the code you've shown here. There's a problem with the code that is *calling* the exported `login` function and not handling errors from it. – Bergi Sep 03 '17 at 15:03
  • @Bergi This seemed to be it. I was missing a 'catch' in the React Component, which was calling the action. – Lars Sep 03 '17 at 15:05

0 Answers0