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/diff-parser-error-message.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion src/apps/cli/commands/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export default class Diff extends Command {

if (!firstResult.success || !secondResult.success) {
const validationError = new ValidationError({
type: 'invalid-file',
type: 'parser-error',
filepath: firstDocument.getFilePath() || secondDocument.getFilePath(),
err: firstResult.error || secondResult.error,
});
Expand Down
66 changes: 43 additions & 23 deletions src/errors/validation-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,58 @@ export class ValidationError extends Error {
private buildError(err: any) {
const errorsInfo: Array<string> = [];

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;
}
12 changes: 12 additions & 0 deletions test/integration/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,4 +605,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();
});
});
});
38 changes: 36 additions & 2 deletions test/unit/errors/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
Loading