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

fix: resolve error in AsyncAPI optimize #1634

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
12 changes: 9 additions & 3 deletions src/commands/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

parser = new Parser();

async run() {

Check warning on line 59 in src/commands/optimize.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Refactor this function to reduce its Cognitive Complexity from 18 to the 15 allowed
const { args, flags } = await this.parse(Optimize); //NOSONAR
let filePath = args['spec-file'];
const proxyHost = flags['proxyHost'];
Expand All @@ -70,13 +70,19 @@
} catch (err:any) {
if (err.message.includes('Failed to download')) {
throw new Error('Proxy Connection Error: Unable to establish a connection to the proxy check hostName or PortNumber.');
} else {
} else if (filePath) {
this.error(
new ValidationError({
type: 'invalid-file',
filepath: filePath,
})
);
} else {
this.error(
new ValidationError({
type: 'no-spec-found'
})
);
}
}

Expand All @@ -88,8 +94,8 @@
} catch (err) {
this.error(
new ValidationError({
type: 'invalid-file',
filepath: filePath,
type: 'invalid-syntax-file',
filepath: this.specFile.getFilePath(),
})
);
}
Expand Down
5 changes: 4 additions & 1 deletion src/core/errors/validation-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type ErrorType = 'parser-error' | 'invalid-file' | 'no-spec-found';
type ErrorType = 'parser-error' | 'invalid-file' | 'no-spec-found' | 'invalid-syntax-file';

interface IValidationErrorInput {
type: ErrorType;
Expand All @@ -15,6 +15,9 @@ export class ValidationError extends Error {
if (error.type === 'invalid-file') {
this.message = `There is no file or context with name "${error.filepath}".`;
}
if (error.type === 'invalid-syntax-file') {
this.message = `Syntax Error in "${error.filepath}"`;
}
if (error.type === 'no-spec-found') {
this.message = 'Unable to perform validation. Specify what AsyncAPI file should be validated.\n\nThese are your options to specify in the CLI what AsyncAPI file should be used:\n- You can provide a path to the AsyncAPI file: asyncapi validate path/to/file/asyncapi.yml\n- You can also pass a saved context that points to your AsyncAPI file: asyncapi validate mycontext\n- In case you did not specify a context that you want to use, the CLI checks if there is a default context and uses it. To set default context run: asyncapi context use mycontext\n- In case you did not provide any reference to AsyncAPI file and there is no default context, the CLI detects if in your current working directory you have files like asyncapi.json, asyncapi.yaml, asyncapi.yml. Just rename your file accordingly.';
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/optimize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('optimize', () => {
.command(['optimize'])
.it('throws error message if no context file exists', (ctx, done) => {
expect(ctx.stdout).to.equal('');
expect(ctx.stderr).to.equal('ValidationError: There is no file or context with name "undefined".\n');
expect(ctx.stderr).to.equal('ValidationError: Unable to perform validation. Specify what AsyncAPI file should be validated.\n\nThese are your options to specify in the CLI what AsyncAPI file should be used:\n- You can provide a path to the AsyncAPI file: asyncapi validate path/to/file/asyncapi.yml\n- You can also pass a saved context that points to your AsyncAPI file: asyncapi validate mycontext\n- In case you did not specify a context that you want to use, the CLI checks if there is a default context and uses it. To set default context run: asyncapi context use mycontext\n- In case you did not provide any reference to AsyncAPI file and there is no default context, the CLI detects if in your current working directory you have files like asyncapi.json, asyncapi.yaml, asyncapi.yml. Just rename your file accordingly.\n');
done();
});
});
Expand Down
Loading