From 47937a7700c4217ea8a272fb6404bd4a3b5b370f Mon Sep 17 00:00:00 2001 From: Gaurav Saini Date: Sat, 18 Jan 2025 15:05:20 +0530 Subject: [PATCH 1/4] fix error in asyncapi optimize Signed-off-by: Gourav --- src/commands/optimize.ts | 24 ++++++++++++++++-------- src/core/errors/validation-error.ts | 5 ++++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/commands/optimize.ts b/src/commands/optimize.ts index 435971c6427..4fe57becf31 100644 --- a/src/commands/optimize.ts +++ b/src/commands/optimize.ts @@ -71,12 +71,20 @@ export default class Optimize extends Command { 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 { - this.error( - new ValidationError({ - type: 'invalid-file', - filepath: filePath, - }) - ); + if(filePath){ + this.error( + new ValidationError({ + type: 'invalid-file', + filepath: filePath, + }) + ); + }else{ + this.error( + new ValidationError({ + type: 'no-spec-found' + }) + ); + } } } @@ -88,8 +96,8 @@ export default class Optimize extends Command { } catch (err) { this.error( new ValidationError({ - type: 'invalid-file', - filepath: filePath, + type: 'invalid-syntax-file', + filepath: this.specFile.getFilePath(), }) ); } diff --git a/src/core/errors/validation-error.ts b/src/core/errors/validation-error.ts index c8386082f59..44c4164bae8 100644 --- a/src/core/errors/validation-error.ts +++ b/src/core/errors/validation-error.ts @@ -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; @@ -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.'; } From 80d4da014cf701934c17af59dd0968ba36a8a6ab Mon Sep 17 00:00:00 2001 From: Gaurav Saini Date: Sat, 18 Jan 2025 15:05:20 +0530 Subject: [PATCH 2/4] fix error in asyncapi optimize Signed-off-by: Gourav --- src/commands/optimize.ts | 24 ++++++++++++++++-------- src/core/errors/validation-error.ts | 5 ++++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/commands/optimize.ts b/src/commands/optimize.ts index 435971c6427..4fe57becf31 100644 --- a/src/commands/optimize.ts +++ b/src/commands/optimize.ts @@ -71,12 +71,20 @@ export default class Optimize extends Command { 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 { - this.error( - new ValidationError({ - type: 'invalid-file', - filepath: filePath, - }) - ); + if(filePath){ + this.error( + new ValidationError({ + type: 'invalid-file', + filepath: filePath, + }) + ); + }else{ + this.error( + new ValidationError({ + type: 'no-spec-found' + }) + ); + } } } @@ -88,8 +96,8 @@ export default class Optimize extends Command { } catch (err) { this.error( new ValidationError({ - type: 'invalid-file', - filepath: filePath, + type: 'invalid-syntax-file', + filepath: this.specFile.getFilePath(), }) ); } diff --git a/src/core/errors/validation-error.ts b/src/core/errors/validation-error.ts index c8386082f59..44c4164bae8 100644 --- a/src/core/errors/validation-error.ts +++ b/src/core/errors/validation-error.ts @@ -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; @@ -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.'; } From 88f8b0e088eab3dcf7bb23ec20276eaac178cb22 Mon Sep 17 00:00:00 2001 From: Gaurav Saini Date: Sat, 25 Jan 2025 20:16:17 +0530 Subject: [PATCH 3/4] improved testcase for optimize Signed-off-by: Gourav --- test/integration/optimize.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/optimize.test.ts b/test/integration/optimize.test.ts index a1b561d9d2e..5b71d3aee05 100644 --- a/test/integration/optimize.test.ts +++ b/test/integration/optimize.test.ts @@ -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(); }); }); From 9c9f4975706d0d00677a15da9f25159f81aadc67 Mon Sep 17 00:00:00 2001 From: Gaurav Saini Date: Sat, 25 Jan 2025 20:25:00 +0530 Subject: [PATCH 4/4] fix linting issue Signed-off-by: Gourav --- src/commands/optimize.ts | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/commands/optimize.ts b/src/commands/optimize.ts index 4fe57becf31..63427f8b7ce 100644 --- a/src/commands/optimize.ts +++ b/src/commands/optimize.ts @@ -70,21 +70,19 @@ export default class Optimize extends Command { } 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 if (filePath) { + this.error( + new ValidationError({ + type: 'invalid-file', + filepath: filePath, + }) + ); } else { - if(filePath){ - this.error( - new ValidationError({ - type: 'invalid-file', - filepath: filePath, - }) - ); - }else{ - this.error( - new ValidationError({ - type: 'no-spec-found' - }) - ); - } + this.error( + new ValidationError({ + type: 'no-spec-found' + }) + ); } }