Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report parse failure reasons #1051

Closed
michael-ball-ctct opened this issue Oct 18, 2023 · 2 comments · Fixed by #1058
Closed

Report parse failure reasons #1051

michael-ball-ctct opened this issue Oct 18, 2023 · 2 comments · Fixed by #1058
Labels
enhancement New feature or request released

Comments

@michael-ball-ctct
Copy link
Contributor

michael-ball-ctct commented Oct 18, 2023

Reason/Context

This is a general Quality of Life improvement for anyone using the generator. By implementing this, the speed at which someone trying to process AsyncAPI docs can diagnose and understand the issues with their document formatting will significantly increase.

Description

The changes for this are fairly simple. In lib/generator.js - Generator.parseInput() block where the document is reported as "not correct" (currently at https://github.com/asyncapi/generator/blob/master/lib/generator.js#L353 ), before the error the diagnostics from the error response can be looped over to console.error out each of the issues found before the error is thrown. Something like:

      if (!document) {
        const err = new Error('Input is not a correct AsyncAPI document so it cannot be processed.');
        err.diagnostics = diagnostics;

        for (const diag of diagnostics) {
          console.error(`Diagnostic err: ${diag['message']} in path ${JSON.stringify(diag['path'])} starting at L${diag['range']['start']['line']} C${diag['range']['start']['character']}, and ending at L${diag['range']['end']['line']} C${diag['range']['end']['character']}`);
        }

        throw err;
      } else {
        this.asyncapi = document;
      }

This would result in an error log being emitted before the thrown error ("Error: Input is not a correct AsyncAPI document so it cannot be processed.") such as:

Diagnostic err: Property "subscribe" is not expected to be here in path ["channels","user/signedup","subscribe"] starting at  L0 L185, and ending at L0 L240

Obviously, the exact phrasing of the error is less important than the information. console.error("Diagnostic err:", diag); would still be a huge improvement here (and I guess also be less brittle, as it doesn't rely so heavily on the structure of the diagnostic objects that get returns being consistent...)

Thanks in advance for any consideration of this. I'm happy to make a PR for this if you want - just let me know which logging strategy is preferred.

@michael-ball-ctct michael-ball-ctct added the enhancement New feature or request label Oct 18, 2023
@derberg
Copy link
Member

derberg commented Oct 25, 2023

@michael-ball-ctct lgtm, please feel free to open a PR 🙏🏼

michael-ball-ctct added a commit to michael-ball-ctct/asyncapi-source-generator that referenced this issue Oct 30, 2023
For [](asyncapi#1051)

I opted for the more basic logging style - I don't know enough about
the diagnostics objects to be sure that they'll always contain the same
fields, so erred on the side of caution rather than trying to access
fields within fields that aren't guaranteed to exist (as well as
including all available information, regardless of what is provided).
michael-ball-ctct added a commit to michael-ball-ctct/asyncapi-source-generator that referenced this issue Oct 30, 2023
For [](asyncapi#1051)

I opted for the more basic logging style - I don't know enough about
the diagnostics objects to be sure that they'll always contain the same
fields, so erred on the side of caution rather than trying to access
fields within fields that aren't guaranteed to exist (as well as
including all available information, regardless of what is provided).
@asyncapi-bot
Copy link
Contributor

🎉 This issue has been resolved in version 1.16.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants