dankilev
dankilev

Reputation: 782

node warnings: UnhandledPromiseRejectionWarning: TypeError: displayErrorFunction is not a function

We have two variants of code that both return the same node warnings.

variant 1

import axios from 'axios';

const correctEndpoint = `https://${process.env.AUTH0_DOMAIN}/dbconnections/signup`;
const headers = {
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    Accept: '*/*'
  }
};

const registerWithAuth0 = async (payload, redirectFunction, displayErrorFunction) => {
  try {
    const response = await axios.post(correctEndpoint, payload, headers);
    if (response.status === 200) {
      redirectFunction();
    } else {
      displayErrorFunction();
    }
  } catch (err) {
    displayErrorFunction();
  } 
}

export default registerWithAuth0;

variant 2

import axios from 'axios';

const correctEndpoint = `https://${process.env.AUTH0_DOMAIN}/dbconnections/signup`;
const headers = {
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    Accept: '*/*'
  }
};

const registerWithAuth0 = (payload, redirectFunction, displayErrorFunction) => {
  axios.post(correctEndpoint, payload, headers)
    .then((response) => {
      if (response.status === 200) {
        redirectFunction();
      } else {
        displayErrorFunction();
      }
    })
    .catch (() => {
      displayErrorFunction();
    });
}

export default registerWithAuth0;

all jest tests pass, but we can see some of the following node warnings:

(node:26886) UnhandledPromiseRejectionWarning: TypeError: displayErrorFunction is not a function
(Use `node --trace-warnings ...` to show where the warning was created)
(node:26886) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:26886) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:26886) UnhandledPromiseRejectionWarning: TypeError: displayErrorFunction is not a function
(node:26886) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)

we are using:

Any ideas about what might be the issue here?

There are some similar issues being reported online, but not quite the same:

Upvotes: 0

Views: 557

Answers (1)

Aritra Chakraborty
Aritra Chakraborty

Reputation: 12552

Make sure that any of the test cases is missing that parameter or not.

Alternative you can add a default value for displayErrorFunction.

(payload, redirectFunction, displayErrorFunction = ()=>{} ) => {

}

Upvotes: 1

Related Questions