Promise rejection not being dealt with by extension command

An unhandled rejected promise is causing issues with my VSCode extension.

I'm puzzled as to why the error dialog doesn't appear when running the command in this extension:

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

The same code in vanilla Typescript works fine:

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

Answer №1

Upon receiving feedback from the VSCode team, it was clarified that window.showErrorMessage requires a string as input, not an Error object. The problem can be resolved by updating the code to use showErrorMessage(err.message) or implementing a more comprehensive solution.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible for a TypeScript function to be used as a React prop even if it conflicts with the function signature's in

Why does the TypeScript type checker allow a prop with a function parameter that does not strictly match the definition? For example, I have a function called callbackImpl = (str: string): number, which is passed as a React prop parameter defined as callb ...

Angular beginner's webpage experiences technical difficulties

My Angular (or TypeScript) code is compiling and running successfully, but I'm facing an issue when trying to load the page in Firefox as it crashes. The problematic code resides in app.component.html <h1>Angular</h1> <dd-button>&l ...

How can you extract the property names of the first object in an array of objects?

I have an array of objects with the following structure and I want to extract the property names of the first object from this array without including the values. The desired result should only be ["Name", "Account", "Status"] ...

Encountering a TypeError while utilizing a custom hook for an Apollo mutation operation

I'm facing a tough time debugging an issue with my Apollo mutation in my NextJS application. The error I'm encountering is as follows: "TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))" It appears t ...

Having trouble getting the React form validation to work using Material UI TextField and TypeScript

I'm having trouble implementing validation on a "sign up" page using the MUI library in React with TypeScript. I've added the "required" attribute to each TextField tag, but the validation doesn't seem to be working upon submission. I'v ...

What is the method to make a String bold when sending it through a messaging service?

Here is the structure of my service: import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root', }) export class MessageService { messages: string[] = []; add(message: string) { this.messages.push(message); ...

Function 'Once' in Typescript with Generics

Currently, I am utilizing a feature called Once() from FP. In TypeScript, I need to define the types for this function but have been struggling with the implementation. Here's what I have attempted so far: const once = <T, U>(fn: (arg: T) => ...

The field "data" is not defined within the type "Response"

My experience with GraphQL mutations has been successful, but I'm encountering difficulties with GraphQL queries. export default interface LoadUsersResponse { users: { nodes:{ id: Number; firstName: String; ...

What are the steps to implement the useContext hook in TypeScript?

In my project, I am attempting to create a dark/light theme system, but I am facing some challenges with the code. When using this line of code in javascript, everything functions correctly: const [darktheme, setDarkTheme] = useContext(ThemeContext); How ...

Please refrain from proceeding until the data recovery process is complete

I am currently facing a priority issue in my code. The problem arises when I call a web service and attempt to retrieve usernames based on user IDs from an array (listePasseoDemandesEnCours) using a foreach loop. this.ws_demandes_en_cours.getDemandesEnCour ...

The 'save' property is not found on the 'IProtein' type. Error code: 2339

Encountering an issue with the mongoose "save" function where the error message reads as "Property 'save' does not exist on type 'IProtein'.ts(2339)". I have come across a solution involving the addition of "extends mongoose.Document" ...

The non-nullable field Mutation.create in typegraphql with bcrypt must not be null, cannot be returned as null

Recently, I have been experimenting with typegraphql alongside apollo-server, typeorm, and bcrypt in typescript. I encountered a peculiar issue when running the mutation query using the resolver below. The error 'Cannot return null for non-nullable fi ...

The property "props" is not recognized within the context of type PropType

Within my component, I am receiving a prop ("author") from a parent component. Although I have defined the prop type as "AuthorProps", I am getting an error stating Property 'author' does not exist on type 'AuthorProps', even though the ...

Angular 2 is experiencing difficulty in loading the image

I tried following the steps outlined in this documentation to display an image for my checkbox, but it doesn't seem to be showing up on the user interface. I have read that using an image is necessary for creating a checkbox, so I'm confused as t ...

Saving User-Token securely in a NativeScript application

In my {N}-App, I am looking to create a login system for users. After a successful login, the user receives a token required for making requests. Currently, I store this token using the application-settings module. Now, I want the UI to dynamically switch ...

The conflicting definitions of identifiers in one file are at odds with those found in a

Currently, I am in the process of updating an aged component from Typescript version 6 to version 8. After making changes to the Jasmine dependencies listed in the package.json, a new error has been encountered: "There are conflicting definitions for th ...

Understanding the status of HTTP requests or observing the updates of observables in Angular2/Typescript is essential

I've been working on an Angular2 and Typescript application where I'm utilizing Angular2's HTTP methods to retrieve data from a database within a service. The service is triggered inside a component's onInit() function and I'm able ...

The type '{}' is lacking the specified attributes from type 'RouteComponentProps<{},,>'

As a newcomer to React and Typescript, I'm in the process of familiarizing myself with both. Please bear with me as I navigate through this learning curve! index.tsx Router.tsx (containing all route definitions) LandingFrame.tsx (defining the page la ...

Custom options titled MUI Palette - The property 'primary' is not found in the 'TypeBackground' type

I am currently working on expanding the MUI palette to include my own custom properties. Here is the code I have been using: declare module '@mui/material/styles' { interface Palette { border: Palette['primary'] background: Pa ...

Display a specific section depending on the user's input by utilizing either ng-if or ng-show

I have a scenario where I need to display one of two sections based on user input. If the user selects 'Daily' in the first 'type' input, I want section 1 to appear (Enter start date and hour). For any other type selection, I want secti ...