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.'; }