Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-generate-error-message.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@asyncapi/cli": patch
---

Add helpful suggestions to generator error messages (fixes #1728)
19 changes: 18 additions & 1 deletion src/apps/cli/commands/generate/fromTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
template: Args.string({ description: '- Name of the generator template like for example @asyncapi/html-template or https://github.com/asyncapi/html-template', required: false }),
};

async run() {

Check failure on line 29 in src/apps/cli/commands/generate/fromTemplate.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=asyncapi_cli&issues=AZ3znrin-YZJSWog_Qjk&open=AZ3znrin-YZJSWog_Qjk&pullRequest=2177
const { args, flags } = await this.parse(Template); // NOSONAR
const interactive = !flags['no-interactive'];
let asyncapi = args['asyncapi'] ?? '';
Expand Down Expand Up @@ -88,7 +88,24 @@
interactive,
);
if (!result.success) {
throw new GeneratorError(new Error(result.error));
const errorMsg = result.error || 'Unknown error';
const suggestions: string[] = [];

if (errorMsg.includes('ENOENT') || errorMsg.includes('not found') || errorMsg.includes('Cannot find')) {
suggestions.push('The template may not be installed. Try installing it first with: npm install <template-name>');
}
if (errorMsg.includes('invalid') || errorMsg.includes('parse') || errorMsg.includes('syntax')) {
suggestions.push('The AsyncAPI document may be invalid or unsupported. Verify it with: asyncapi validate <document>');
}
if (errorMsg.includes('E404') || errorMsg.includes('404')) {
suggestions.push('The template was not found on npm. Check the template name and try again.');
}

const fullMessage = suggestions.length > 0
? `${errorMsg}\n\nPossible causes:\n${suggestions.map(s => ` - ${s}`).join('\n')}`

Check warning on line 105 in src/apps/cli/commands/generate/fromTemplate.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this code to not use nested template literals.

See more on https://sonarcloud.io/project/issues?id=asyncapi_cli&issues=AZ3znrin-YZJSWog_Qjl&open=AZ3znrin-YZJSWog_Qjl&pullRequest=2177
: errorMsg;

throw new GeneratorError(new Error(fullMessage));
}

// Output logs in non-interactive mode
Expand Down
Loading