节点警告:未处理的PromiseRejectionWarning:TypeError:displayErrorFunct



我们有两种代码变体,它们都返回相同的节点警告。

变体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;

变体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;

所有的jest测试都通过了,但我们可以看到以下节点警告

(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)

我们使用的是:

  • 节点v14.17.1
  • 反应v17.0.1
  • jest v26.6.3
  • babel jest v26.6.3
  • @babel/core v7.14.0

有什么想法吗

网上也报道了一些类似的问题,但并不完全相同:

  • 未处理的PromiseRejection警告:此错误源于在没有catch块的异步函数内部抛出
  • 未处理的PromiseRejectionWarning:TypeError:createUser不是函数

确保任何测试用例是否缺少该参数。

或者,您可以为displayErrorFunction添加默认值。

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

最新更新