programing tip

Node.js UnhandledPromiseRejectionWarning에서 처리되지 않은 약속을 찾는 방법은 무엇입니까?

itbloger 2020. 6. 3. 21:29
반응형

Node.js UnhandledPromiseRejectionWarning에서 처리되지 않은 약속을 찾는 방법은 무엇입니까?


버전 7의 Node.js에는 약속을 처리하기위한 비동기 / 대기 구문 설탕이 있으며 이제 내 코드에서 다음 경고가 자주 발생합니다.

(node:11057) UnhandledPromiseRejectionWarning: Unhandled promise 
rejection (rejection id: 1): ReferenceError: Error: Can't set headers 
after they are sent.
(node:11057) 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.

불행히도 캐치가 누락 된 라인에 대한 참조는 없습니다. 모든 try / catch 블록을 확인하지 않고 찾을 수있는 방법이 있습니까?


unhandledRejection프로세스의 이벤트를 습니다.

process.on('unhandledRejection', (reason, p) => {
  console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
  // application specific logging, throwing an error, or other logic here
});

올바른 처리되지 않은 ES6 약속 거부에 대한 전체 스택 트레이스를 표시하는 방법으로,과 Node.js를 실행하는 것입니다 --trace-warnings플래그. 이것은 자신의 코드 내에서 거부를 가로 챌 필요없이 모든 경고에 대한 전체 스택 추적을 보여줍니다. 예를 들면 다음과 같습니다.

노드 --trace-warnings app.js

파일 이름 앞에trace-warnings 플래그가 있는지 확인하십시오 ! 그렇지 않으면 플래그는 스크립트에 대한 인수로 해석되며 Node.js 자체에서 무시됩니다..js

처리 되지 않은 거부 를 실제로 처리 하려면 (예 : 로깅) unhandled-rejection모듈을 대신 사용 하면 단일 이벤트 처리기로 모듈을 지원하는 모든 주요 약속 구현에 대해 처리되지 않은 모든 거부를 잡을 수 있습니다.

그 모듈을 지원하는 블루 버드, ES6의 약속, Q, WhenJS, es6-promise, then/promise, 무엇이든 그 처리되지 않은 거부 사양 (문서 전체 세부 사항)의 준수합니다.


스택 추적으로 로깅

유용한 오류 메시지가 더있는 경우 이것을 노드 파일에 추가하십시오. 충돌이 발생한 전체 스택 추적을 표시해야합니다.

process.on('unhandledRejection', (error, p) => {
  console.log('=== UNHANDLED REJECTION ===');
  console.dir(error.stack);
});

참고 URL : https://stackoverflow.com/questions/43834559/how-to-find-which-promises-are-unhandled-in-node-js-unhandledpromiserejectionwar

반응형