Reputation: 51
A rejected promise is not being handled by my VSCode extension.
Why doesn't the following extension show the error dialog when the command is run?
'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);
});
}
Doing the same thing vanilla Typescript works as expected:
'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()
Output:
$ 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)
Upvotes: 1
Views: 528
Reputation: 51
The vscode team pointed out that window.showErrorMessage
needs a string, not an Error. Changing to showErrorMessage(err.message)
(or something a bit more robust) fixes the issue.
Upvotes: 1