tsdjs / tsd

Check TypeScript type definitions
MIT License
2.36k stars 68 forks source link

Fix error messages when `tsd` fails #189

Closed tommy-mitchell closed 1 year ago

tommy-mitchell commented 1 year ago

Similar to #182, tsd will also display an error stack on failure (even if it didn't crash), e.g. when no index.d.ts is found:

Error running tsd:
Error: The type definition \`index.d.ts\` does not exist at \`.../fixtures/no-tsd/index.d.ts\`. Is the path correct? Create one and try again.
    at .../lib/index.js:31:15
    at Generator.next (<anonymous>)
    at fulfilled (.../lib/index.js:5:58)

This should only show the error message:

The type definition \`index.d.ts\` does not exist at \`.../fixtures/no-tsd/index.d.ts\`. Is the path correct? Create one and try again.

This PR adds a custom TsdError to differentiate between the two cases:

try {
    // ... run tsd
} catch (error: unknown) {
    const potentialError = error as Error | undefined;

    if (potentialError instanceof TsdError) {
        exit(potentialError.message);
    }

    const errorMessage = potentialError?.stack ?? potentialError?.message ?? 'tsd unexpectedly crashed.';

    exit(`Error running tsd:\n${errorMessage}`);
}
tommy-mitchell commented 1 year ago

@sindresorhus could you put out a patch release for this?

sindresorhus commented 1 year ago

https://github.com/SamVerschueren/tsd/releases/tag/v0.28.1