2017-07-09 18 views
0

被拒絕的承諾不被我的VSCode擴展處理。擴展命令不處理承諾拒絕

爲什麼下面的擴展名在命令運行時不顯示錯誤對話框?

'use strict'; 
import {commands,window,ExtensionContext} from 'vscode'; 

export function activate(context: ExtensionContext) { 
    let cmd = commands.registerCommand('extension.sayHello', sayHello) 
    context.subscriptions.push(cmd); 
} 
function sayHello() { 
    doSomethingAsync('hello') 
    .then( res => window.showInformationMessage('Success!')) 
    .catch(err => window.showErrorMessage(err)) 
} 
function doSomethingAsync(value:string): Promise<string> { 
    return new Promise <string> ((resolve, reject) => { 
     setTimeout(function() { 
      reject(new Error('Rejected!'))    
     }, 250); 
    }); 
} 

做同樣的事情香草打字稿按預期工作:

'use strict'; 
function sayHello() { 
    doSomethingAsync('hello') 
    .then( res => console.log('Success!')) 
    .catch(err => console.error('Failed!', err)) 
} 
function doSomethingAsync(value:string): Promise<string> { 
    return new Promise <string> ((resolve, reject) => { 
     setTimeout(function() { 
      reject(new Error('Rejected!'))  
     }, 250); 
    }); 
} 
sayHello() 

輸出:

$ node --version && tsc --version && code --version 
v8.1.3 
Version 2.3.4 
1.13.1 
379d2efb5539b09112c793d3d9a413017d736f89 

$ ls ~/.vscode/extensions/ 

$ tsc -p ./ && node out/src/reject.js 
Failed! Error: Rejected! 
    at Timeout._onTimeout (/Users/mike/Source/vscode/extensions/issues/vscode-unhandled-promise/out/src/reject.js:9:20) 
    at ontimeout (timers.js:488:11) 
    at tryOnTimeout (timers.js:323:5) 
    at Timer.listOnTimeout (timers.js:283:5) 

回答

0

的vscode團隊指出,window.showErrorMessage需要一個字符串,而不是一個錯誤。更改爲showErrorMessage(err.message)(或更強大一點)可以解決此問題。