From 76e1dbd6870adebfbfe8fa6ff1ef9d67cb49df15 Mon Sep 17 00:00:00 2001 From: "Ember (prep for Dammy Okunlola)" Date: Mon, 2 Nov 2026 23:59:41 +0000 Subject: [PATCH 1/3] fix(diff): use parser-error type for parse failures in diff --- .changeset/diff-parser-error-message.md | 5 +++++ src/apps/cli/commands/diff.ts | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 .changeset/diff-parser-error-message.md diff --git a/.changeset/diff-parser-error-message.md b/.changeset/diff-parser-error-message.md new file mode 100644 index 000000000..075e6105f --- /dev/null +++ b/.changeset/diff-parser-error-message.md @@ -0,0 +1,5 @@ +--- +'@asyncapi/cli': patch +--- + +Fix `asyncapi diff` printing a bare `ValidationError:` with no diagnostic body when one of the supplied documents fails to parse. The parse-failure branch in `parseDocuments` used the `invalid-file` ValidationError type (the file-not-found template) instead of `parser-error`, and `buildError` produced an empty string for error shapes that do not follow the ParserError contract, such as the plain string returned by the validation service or a wrapped ValidationError. Both paths now surface a non-empty message. diff --git a/src/apps/cli/commands/diff.ts b/src/apps/cli/commands/diff.ts index 683ed6bae..70457264b 100644 --- a/src/apps/cli/commands/diff.ts +++ b/src/apps/cli/commands/diff.ts @@ -247,7 +247,7 @@ export default class Diff extends Command { if (!firstResult.success || !secondResult.success) { this.error( new ValidationError({ - type: 'invalid-file', + type: 'parser-error', filepath: firstDocument.getFilePath() || secondDocument.getFilePath(), err: firstResult.error || secondResult.error, }), From d2884d4674da1425629eb642f7e1388063c85ee7 Mon Sep 17 00:00:00 2001 From: "Ember (prep for Dammy Okunlola)" Date: Mon, 2 Nov 2026 23:59:41 +0000 Subject: [PATCH 2/3] fix(errors): surface a message for non-ParserError shapes in ValidationError --- src/errors/validation-error.ts | 66 ++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/src/errors/validation-error.ts b/src/errors/validation-error.ts index 71dc7b7e1..aff97fadd 100644 --- a/src/errors/validation-error.ts +++ b/src/errors/validation-error.ts @@ -32,38 +32,58 @@ export class ValidationError extends Error { private buildError(err: any) { const errorsInfo: Array = []; - if (err.title) { + if (err?.title) { errorsInfo.push(err.title); } - if (err.detail) { + if (err?.detail) { errorsInfo.push(err.details); } - if (err.validationErrors) { + if (err?.validationErrors) { for (const e of err.validationErrors) { - const errorHasTitle = !!e.title; - const errorHasLocation = !!e.location; - /* - * All the conditions below are needed since validationErrors (from ParserError) come from Parser JS library, - * so we cannot assure that all the fields or properties are always provided in the error. There might be cases - * that even title is not provided. - */ - if (errorHasTitle && errorHasLocation) { - errorsInfo.push( - `${e.title} ${e.location.startLine}:${e.location.startColumn}`, - ); - continue; - } - if (errorHasTitle) { - errorsInfo.push(`${e.title}`); - continue; - } - if (errorHasLocation) { - errorsInfo.push(`${e.location.startLine}:${e.location.startColumn}`); + const formatted = formatValidationError(e); + if (formatted) { + errorsInfo.push(formatted); } } } - this.message = errorsInfo.join('\n'); + + if (errorsInfo.length > 0) { + this.message = errorsInfo.join('\n'); + return; + } + + /* + * Fallback for error shapes that do not follow the ParserError contract. + * The validation service reports parse failures as a plain string, and + * command catch blocks may wrap an existing ValidationError; without a + * fallback those shapes produced an empty message and users only saw a + * bare "ValidationError:" with no diagnostic body. + */ + this.message = + (typeof err === 'string' && err) || err?.message || 'Unknown parser error'; + } +} + +/* + * Format a single validationErrors entry from a ParserError. Every field is + * treated as optional since validationErrors come from the Parser JS library, + * so we cannot assure that all the fields or properties are always provided + * in the error. There might be cases that even title is not provided. + */ +function formatValidationError(e: any): string | undefined { + const errorHasTitle = !!e.title; + const errorHasLocation = !!e.location; + + if (errorHasTitle && errorHasLocation) { + return `${e.title} ${e.location.startLine}:${e.location.startColumn}`; + } + if (errorHasTitle) { + return `${e.title}`; + } + if (errorHasLocation) { + return `${e.location.startLine}:${e.location.startColumn}`; } + return undefined; } From 97ebf014343f550e27eb8a64be8095fbf1df71bc Mon Sep 17 00:00:00 2001 From: "Ember (prep for Dammy Okunlola)" Date: Mon, 2 Nov 2026 23:59:41 +0000 Subject: [PATCH 3/3] test: cover empty ValidationError regression for diff parse failures --- test/integration/diff.test.ts | 12 +++++++++++ test/unit/errors/errors.test.ts | 38 +++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/test/integration/diff.test.ts b/test/integration/diff.test.ts index 5befd309c..4c594d0dd 100644 --- a/test/integration/diff.test.ts +++ b/test/integration/diff.test.ts @@ -577,4 +577,16 @@ describe('diff', () => { }); }); }); + + describe('when one of the documents cannot be parsed', () => { + test + .stderr() + .stdout() + .command(['diff', './test/fixtures/specification.yml', './test/fixtures/specification-invalid.yml']) + .it('surfaces the parse failure instead of an empty ValidationError', (ctx, done) => { + expect(ctx.stderr).to.contain('ValidationError'); + expect(ctx.stderr).to.contain('Failed to parse document'); + done(); + }); + }); }); diff --git a/test/unit/errors/errors.test.ts b/test/unit/errors/errors.test.ts index 2774a8f9c..717920ed3 100644 --- a/test/unit/errors/errors.test.ts +++ b/test/unit/errors/errors.test.ts @@ -205,12 +205,46 @@ describe('Error classes', () => { expect(err.message).to.include('20:1'); }); - it('should handle parser-error with empty validation errors', () => { + it('should fall back to a non-empty message for parser-error with empty validation errors', () => { const err = new ValidationError({ type: 'parser-error', err: { validationErrors: [] }, }); - expect(err.message).to.equal(''); + expect(err.message).to.equal('Unknown parser error'); + }); + + it('should use a string err as the message for parser-error', () => { + // The validation service reports parse failures as a plain string + // ('Failed to parse document'); it used to produce an empty message. + const err = new ValidationError({ + type: 'parser-error', + err: 'Failed to parse document', + }); + expect(err.message).to.equal('Failed to parse document'); + }); + + it('should use the message of a generic Error for parser-error', () => { + const err = new ValidationError({ + type: 'parser-error', + err: new Error('Something went wrong'), + }); + expect(err.message).to.equal('Something went wrong'); + }); + + it('should surface the inner message when a ValidationError is wrapped in another one', () => { + // The diff command's catch block wraps the ValidationError thrown by + // parseDocuments in a second one; the inner message must survive. + const inner = new ValidationError({ + type: 'parser-error', + err: 'Failed to parse document', + }); + const err = new ValidationError({ type: 'parser-error', err: inner }); + expect(err.message).to.equal('Failed to parse document'); + }); + + it('should fall back to a non-empty message when err is undefined', () => { + const err = new ValidationError({ type: 'parser-error', err: undefined }); + expect(err.message).to.equal('Unknown parser error'); }); }); });