Skip to content

activate() has no error handling -- API failure crashes extension activation #5

Description

@rioredwards

Location

extension.js:20-22

Problem

const affirmations = await fetchAffirmations();
const randomNumber = Math.floor(Math.random() * affirmations.length);
vscode.window.showInformationMessage(affirmations[randomNumber].text);

If fetchAffirmations throws (network error) or the API returns an error object instead of an array, affirmations.length is undefined and .text throws a TypeError. This crashes extension activation, which means none of the commands register.

Suggested fix

try {
    const affirmations = await fetchAffirmations();
    if (Array.isArray(affirmations) && affirmations.length) {
        const randomNumber = Math.floor(Math.random() * affirmations.length);
        vscode.window.showInformationMessage(affirmations[randomNumber].text);
    }
} catch {
    // affirmation display is cosmetic -- don't block activation
}

The same defensive pattern should be applied inside each command handler (they also index into filtered arrays without a length check).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions