Skip to content

fix(parser): add throwOnError option to surface validation failures (#878)#1182

Open
Guciolek wants to merge 2 commits into
asyncapi:masterfrom
Guciolek:fix/878-throw-on-validation-error
Open

fix(parser): add throwOnError option to surface validation failures (#878)#1182
Guciolek wants to merge 2 commits into
asyncapi:masterfrom
Guciolek:fix/878-throw-on-validation-error

Conversation

@Guciolek

@Guciolek Guciolek commented Jun 7, 2026

Copy link
Copy Markdown

Summary

Fixes #878 by adding an opt-in throwOnError option to Parser#parse() that surfaces validation failures as a typed AsyncAPIParseError instead of silently returning a result with document: undefined.

Problem

When the parser encounters an invalid AsyncAPI document, it currently returns:

{ document: undefined, diagnostics: [...], extras: { document } }

This silent-failure mode is easy to miss — especially for the legacy .document() accessor which throws a confusing is not a function error if you don't first null-check.

The original report from #878:

The Parser doesn't throw any error when I provide an invalid asyncapi file but instead returns undefined

Fix

Added throwOnError?: boolean to ParseOptions. When true and the input produces at least one error-severity diagnostic, the parser throws a new AsyncAPIParseError that carries:

  • name'AsyncAPIParseError'
  • message — human-readable description
  • diagnostics — the full Diagnostic[] from validation
  • document — the resolved Spectral Document (when available) for advanced inspection

The default is false, so this is fully backward compatible.

Usage

import { Parser, AsyncAPIParseError } from '@asyncapi/parser';

const parser = new Parser();

try {
  const { document } = await parser.parse(invalidDoc, { throwOnError: true });
  // document is guaranteed to be defined here
} catch (err) {
  if (err instanceof AsyncAPIParseError) {
    console.error(err.message);
    for (const d of err.diagnostics) {
      console.error(`  [${d.code}] ${d.message}`);
    }
  }
}

Tests

  • 6 new tests in test/parse.spec.ts covering:
    • default (silent) behavior preserved
    • explicit throwOnError: false preserved
    • throwOnError: true throws AsyncAPIParseError on invalid input
    • thrown error exposes diagnostics
    • valid document still parses without throwing
    • diagnostics with warnings/info only do not throw
  • 2472 tests pass (2466 existing + 6 new), zero regressions.

Notes

…syncapi#878)

When parsing an invalid AsyncAPI document, the parser used to silently
return a result with 'document: undefined', which made it easy for
callers to miss the failure (e.g. 'result.document()' would throw a
confusing 'is not a function' error).

This change adds a 'throwOnError' option to ParseOptions. When enabled,
the parser throws a new 'AsyncAPIParseError' carrying the diagnostics
and (when available) the resolved Spectral document whenever the input
fails validation with at least one error-severity diagnostic.

The default behavior is preserved (no throw) to keep this fully
backward compatible.

Refs: asyncapi#878
@changeset-bot

changeset-bot Bot commented Jun 7, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 990fa1f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@asyncapi/parser Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome to AsyncAPI. Thanks a lot for creating your first pull request. Please check out our contributors guide useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

@sonarqubecloud

sonarqubecloud Bot commented Jun 9, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parser fails to throw an error when i provide an invalid asyncapi file

1 participant