2017-09-07 7 views
0

오류를 다시 던지기를 원하지만 원래 오류 추적을 유지하려는 상황에 처해 있습니다. 다음과 같이 수행 할 수 있습니다 평범한 구식 동기 자바 스크립트에서 다음과 같이node.js : bluebird에서 다시 던진 오류에서 스택 추적 유지

try { 
    processThings(); 
} catch (e) { 
    const moreDetailedError = new Error("Couldn't process things due to: " + e.message); 
    moreDetailedError.original = e; 
    moreDetailedError.stack = moreDetailedError.stack + '\nCaused by:\n' + e.stack; 

    throw moreDetailedError; 
} 

function processThings() { 
    throw new Error('Internal error'); 
} 

결과 스택 추적 모양 : 내가 원하는 정확히 무엇을 꽤 많이입니다

Error: Couldn't process things due to: Internal error 
    at Object.<anonymous> (test.js:8:29) 
    at Module._compile (module.js:570:32) 
    [...] 
    at bootstrap_node.js:509:3 
Caused by: 
Error: Internal error 
    at processThings (test.js:16:9) 
    at Object.<anonymous> (test.js:6:3) 
    at Module._compile (module.js:570:32) 
    [...] 
    at bootstrap_node.js:509:3 

합니다. 이제 나는 블루 버드 (Bluebird)와의 약속 체인 연속을 통해 똑같이 할 수 있기를 원합니다. 여기에 (긴 스택 트레이스를 얻을 수 BLUEBIRD_DEBUG=1으로 실행해야합니다)에 해당하는 약속 기반 코드입니다 :

const Promise = require('bluebird'); 

Promise.try(processThings).catch(e => { 
    const moreDetailedError = new Error("Couldn't process things due to: " + e.message); 
    moreDetailedError.original = e; 
    moreDetailedError.stack = moreDetailedError.stack + '\nCaused by:\n' + e.stack; 

    throw moreDetailedError; 
}); 

function processThings() { 
    throw new Error('Internal error'); 
} 

그러나, 블루 버드 내 Caused by 정보를 삼키는 것으로 보인다 단순히 나에게 원래없이 바로 moreDetailedError의 스택 트레이스를 제공합니다 :

Unhandled rejection Error: Couldn't process things due to: Internal error 
    at Promise.try.catch.e (test.js:6:29) 
    at runCallback (timers.js:637:20) 
    [...] 
    at Object.<anonymous> (test.js:5:12) 
From previous event: 
    at Object.<anonymous> (test.js:5:33) 
    at Module._compile (module.js:570:32) 
    [...] 
    at bootstrap_node.js:509:3 

말에 제대로 나올 블루 버드를 통해 수정 된 스택을 얻는 방법이 있나요?

답변

0

스택 추적을 조작하는 대신 원래 오류 스택을 더 자세한 오류 메시지 인 message에 첨부하면이 문제를 피할 수있었습니다. 내가 찾던 솔루션이 아니지만 제 목적을 위해 작동합니다.