Skip to content

fix: throw meaningful errors - #449

Closed
ritik307 wants to merge 20 commits into
asyncapi:masterfrom
ritik307:fix/throw-meaningfull-error
Closed

ritik307 wants to merge 20 commits into
asyncapi:masterfrom
ritik307:fix/throw-meaningfull-error

Conversation

@ritik307

Copy link
Copy Markdown

Description

  • Added error messages to parser.js

Related issue(s)

Fixes #157

@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.

@derberg derberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think I found your problem, have a look at my comments

I have to say that it is super hard to review this PR because of all the autoformatted changes. Can we do something about it?

Comment thread README.md Resolved
Comment thread lib/parser.js Resolved
Comment thread lib/parser.js Resolved
Comment thread lib/parser.js Resolved
@ritik307
ritik307 requested a review from derberg January 20, 2022 15:33
@ritik307

Copy link
Copy Markdown
Author

@derberg I am not able to figure out what to pass inside groupValidationErrors function in lib/parser.js

@derberg derberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I provided a few hints. Sorry, I just realized this code related to validation that I wrote is complicated and provided JSDoc is lame 😞

Lemme know if you need more help. And please remember to remove all the autoformatted parts before next review as is is really hard to review

Comment thread lib/parser.js Outdated
'server does not have a corresponding schema object',
new Map(),
fileFormat,
parser

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

here you should pass information if original AsyncAPI file passed into parser was YAML or JSON. It is in fileFormat variable.

Comment thread lib/parser.js Outdated
'servers',
'server does not have a corresponding schema object',
new Map(),
fileFormat,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it should be originalAsyncAPIDocument, the oryginal file as string. It is needed because returned error will have to indicate where exactly in the file (line, column) the error is in, so original document is needed. So later in the UI you can highlight the error

Comment thread lib/parser.js Outdated
detail:
'You shall not pass, no schema parser available that can parse this schema. The good news is that we have one for Avro, just install @asyncapi/avro-schema-parser and use registerSchemaParser to register it with the current parser',
validationErrors: groupValidationErrors(
'servers',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it is not server, it is channels

Comment thread lib/parser.js Outdated
validationErrors: groupValidationErrors(
'servers',
'server does not have a corresponding schema object',
new Map(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is a map of errors, so definitely cannot be empty.

you choose to use groupValidationErrors, which is much better from initial implementation.

Why?

because you will not throw error immediately when it is found. The intention with groupValidationErrors is to use it in error when you want to throw it after thing through all messages.

without such approach, you would get error for one message, then you fix, then error for another message, and fix, and so on, not so nice.

JSDoc say this about the map

map of error elements cause the validation error might happen in many places in the document. 
The key should have a path information where the error was found, the value holds information about error element but it is not mandatory

so the key is the json path pointing to the affected place in the document. Later it ends up in error object under location.jsonPointer. You will need to use tilde util for sure.

So from your test example,

  • first you need to make validateAndConvertMessage aware of the channel name and operation where validated message is located.
  • Then in this map, the key would be something like `tilde(${channelName})/${operationName}/message/schemaFormat
  • and value is, I don't remember but probably broken value 🤷🏼

does it make sense?

Comment thread lib/parser.js
const parser = PARSERS[String(schemaFormat)];

if (!parser) {
throw new ParserError({

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

when using groupValidationErrors, do not throw error immediately. Let parser go through all messages, so they you throw error a the end, with all broken schemaFormats from all messages. Look into customValidators how they use it.

I think it will be trick, but yeah, try

@ritik307

Copy link
Copy Markdown
Author

@derberg I have made changes as you have suggested but I am still getting errors while running the test and am not sure about the implementation of throwing all the errors at once.😅

Comment thread lib/parser.js Resolved
@ritik307

Copy link
Copy Markdown
Author

Hi @derberg
I got your point and have made some changes kindly look into them.😊😊

@derberg derberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please convert it to regular PR so we can have tests running in a PR. Also please remove autoformatting also from test/parse_test.js file 😅

@ritik307
ritik307 marked this pull request as ready for review January 24, 2022 16:07
@ritik307

Copy link
Copy Markdown
Author

@derberg Done! 😁

Comment thread lib/parser.js Resolved
@ritik307

Copy link
Copy Markdown
Author
if (schemaValidationErrors.length) {
        const errors = schemaValidationErrors;
        schemaValidationErrors.length = 0;
        throw new ParserError({
          type: validationError,
          title: 'Channel validation failed',
          parsedJSON,
          validationErrors: errors,
        });

@magicmatatjahu Will using the above be a good idea because I can't clear the array after the throw command. Please suggest if any other better solution exists.

@ritik307

Copy link
Copy Markdown
Author

@derberg @magicmatatjahu I have implemented the requested changes but failed the npm run test and I am not able to figure out how I can resolve the TypeErrors I am getting. Kindly help me out.

@magicmatatjahu magicmatatjahu left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm sorry, but I won't accept the current solution with the global array schemaValidationErrors and the reason is simple: parsing is done in an asynchronous way and if we run the function 100 or even more times in a row it may happen that the array schemaValidationErrors will have some data (or will be deleted) at the moment when other independent parsing is done. This can happen in our server-api project as well as in studio. If we still want to have schemaValidationErrors we need create that array in local scope (e.g. at the beginning of the parse function) and pass it to the corresponding functions. Global values should be independent.

@ritik307

Copy link
Copy Markdown
Author

@derberg @magicmatatjahu I have made the requested changes kindly review the PR :)

@derberg derberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I found why your tests fail, also added some comments to improve naming of variables and text of error messages.

good work so far 🚀

Comment thread lib/parser.js Resolved
Comment thread lib/parser.js Resolved
Comment thread lib/parser.js Resolved
Comment thread lib/parser.js Resolved
Comment thread lib/parser.js Resolved
Comment thread test/parse_test.js Resolved
Comment thread test/wrong/unknown-schema-format.yaml Resolved
Comment thread lib/parser.js Outdated
const defaultSchemaFormat = getDefaultSchemaFormat(parsedAsyncAPIDocument.asyncapi);
const schemaFormat = msg.schemaFormat || defaultSchemaFormat;
const parser = PARSERS[String(schemaFormat)];
const errorMap = new Map();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this whole error map creation is related to if (!parser) { only, so better move it there for clarity

Comment thread lib/parser.js Resolved
Comment thread lib/parser.js Resolved
@ritik307

Copy link
Copy Markdown
Author

hi @derberg While implementing the requested changes I am confused about some requested changes like

Screenshot of error i am currently facing while running npm run test
ss1

@ritik307

Copy link
Copy Markdown
Author

@derberg @magicmatatjahu sorry I was caught up with some college work 😅. I have made some changes kindly review them.🙂

@derberg derberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

hey, left some comments

also please make sure that you always run npm run lint before you submit something for review

Comment thread lib/parser.js Resolved
Comment thread lib/parser.js Resolved
Comment thread lib/parser.js Outdated
Comment on lines +212 to +225
if(schemaValidationErrors.length) {
const errors = schemaValidationErrors.map(e => ({
...e,
dataPath: e.dataPath.replace(/^\./, ''),
}));
schemaValidationErrors.length=0;
throw new ParserError({
type: validationError,
title: 'The document uses a schema format that parser does not support',
detail:'Parser is not able to parse messages as they are provided in not supported schema. Parser can be extended to parse other schemas, you can also use existing custom schema parsers for Avro, RAML Data Types and OpenAPI Schema. Install one of them, @asyncapi/avro-schema-parser, @asyncapi/openapi-schema-parser or @asyncapi/ramldt-schema-parser and use registerSchemaParser function to register it with the this parser',
parsedJSON,
validationErrors: errors,
});
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if(schemaValidationErrors.length) {
const errors = schemaValidationErrors.map(e => ({
...e,
dataPath: e.dataPath.replace(/^\./, ''),
}));
schemaValidationErrors.length=0;
throw new ParserError({
type: validationError,
title: 'The document uses a schema format that parser does not support',
detail:'Parser is not able to parse messages as they are provided in not supported schema. Parser can be extended to parse other schemas, you can also use existing custom schema parsers for Avro, RAML Data Types and OpenAPI Schema. Install one of them, @asyncapi/avro-schema-parser, @asyncapi/openapi-schema-parser or @asyncapi/ramldt-schema-parser and use registerSchemaParser function to register it with the this parser',
parsedJSON,
validationErrors: errors,
});
}
if (schemaValidationErrors.size) {
throw new ParserError({
type: validationError,
title: 'The document uses a schema format that parser does not support',
detail: 'Parser is not able to parse messages as they are provided in not supported schema. Parser can be extended to parse other schemas, you can also use existing custom schema parsers for Avro, RAML Data Types and OpenAPI Schema. Install one of them, @asyncapi/avro-schema-parser, @asyncapi/openapi-schema-parser or @asyncapi/ramldt-schema-parser and use registerSchemaParser function to register it with the this parser',
parsedJSON,
validationErrors: groupValidationErrors(
'channels',
'uses unsupported schema format',
schemaValidationErrors,
asyncapiYAMLorJSON,
initialFormat
)
});
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is only half the working code. This will now trigger good errors so you need to adjust tests accordingly

The only thing that doesn't work in this code is line 219 -> 'channels'. It is hardcoded to channels even though sometimes as you know error might be not in message under channels but also in message under components, so sometimes instead of 'channels' you must pass 'components'.

We need to figure out how to do this....so first please apply my suggestions, fix tests and what should left for us to think about is this channels vs components thingy. I think the only way to solve it will be to refactor groupValidationErrors somehow

@magicmatatjahu

Copy link
Copy Markdown
Member

@ritik307 Do you have time for further contribution? Any problems/issues?

@ritik307

ritik307 commented Apr 7, 2022

Copy link
Copy Markdown
Author

@magicmatatjahu Sorry for the late response. Yes, I do have time to contribute but not as much as I had before 😔. As of now, I will only be able to contribute on weekends.

@derberg

derberg commented Apr 11, 2022

Copy link
Copy Markdown
Member

@ritik307 no worries, just keep in mind that with this one you are almost done, so do not lose the time that you've already put in this PR. It is a great addition.

@ritik307
ritik307 requested a review from derberg April 30, 2022 17:20
@ritik307

Copy link
Copy Markdown
Author

Sorry for the delay. I made the changes but didn't forget to push the code. I am still facing some doubts though. Kindly help me out

@derberg

derberg commented May 9, 2022

Copy link
Copy Markdown
Member

@ritik307 if you did not push the code, I'm not sure how can I help 😄

@ritik307

ritik307 commented May 10, 2022 •

Copy link
Copy Markdown
Author

@derberg Sorry for the miscommunication. I meant I have pushed the code can you review it.😊

@sonarqubecloud

Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Comment thread test/parse_test.js
};
await checkErrorWrapper(async () => {
await parser.parse(notRegisteredFormatFile, { path: __filename });
}, errorObject);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
}, errorObject);
}, expectedErrorObject);

@derberg

derberg commented May 23, 2022 •

Copy link
Copy Markdown
Member

@ritik307 you definitely need to run tests before you push code 😄
Please have a look and update the tests with proper info. Just look at other tests and get some inspiration on how we check error objects and what level of details is needed.

@derberg

derberg commented Jul 28, 2022

Copy link
Copy Markdown
Member

@ritik307 you were pretty advanced here, do not want to continue?

@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this pull request, add a comment with detailed explanation.

There can be many reasons why some specific pull request has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this pull request forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@github-actions github-actions Bot added the stale label Nov 26, 2022
@magicmatatjahu

Copy link
Copy Markdown
Member

I am closing due to lack of response.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Throw meaningfull error when parser of specific schemaFormat is not available

3 participants