扩展命令不处理承诺拒绝

Extension command not handling promise reject

我的 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);
    });
}

做同样的事情 vanilla Typescript 按预期工作:

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

vscode 团队指出 window.showErrorMessage 需要一个字符串,而不是错误。更改为 showErrorMessage(err.message)(或更强大的东西)可以解决问题。