Skip to content

Commit

Permalink
CM-41505- Fix CliError parsing (#110)
Browse files Browse the repository at this point in the history
MarshalX authored Oct 29, 2024
1 parent c4fddc9 commit dce56b0
Showing 3 changed files with 23 additions and 16 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,10 @@

## [Unreleased]

## [v1.11.1]

- Fix CliError parsing

## [v1.11.0]

- Add extension loading screen
@@ -103,6 +107,8 @@

The first stable release with the support of Secrets, SCA, TreeView, Violation Card, and more.

[v1.11.1]: https://github.com/cycodehq/vscode-extension/releases/tag/v1.11.1

[v1.11.0]: https://github.com/cycodehq/vscode-extension/releases/tag/v1.11.0

[v1.10.0]: https://github.com/cycodehq/vscode-extension/releases/tag/v1.10.0
@@ -141,4 +147,4 @@ The first stable release with the support of Secrets, SCA, TreeView, Violation C

[v1.0.0]: https://github.com/cycodehq/vscode-extension/releases/tag/v1.0.0

[Unreleased]: https://github.com/cycodehq/vscode-extension/compare/v1.11.0...HEAD
[Unreleased]: https://github.com/cycodehq/vscode-extension/compare/v1.11.1...HEAD
27 changes: 12 additions & 15 deletions src/cli/cli-wrapper.ts
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import { ILoggerService } from '../services/logger-service';
import { LoggerServiceSymbol } from '../symbols';
import JSON_ from '../utils/json_';
import { ClassConstructor, plainToInstance } from 'class-transformer';
import { CliError } from './models/cli-error';
import { CliError, isCliError } from './models/cli-error';
import { ExitCode } from './exit-code';
import { CommandParameters } from './constants';
import { getUserAgentArg } from './user-agent';
@@ -30,27 +30,24 @@ export class CliWrapper {
return new CliResultSuccess(null);
}

const camelCasedObj = JSON_.parse(out);
if (isCliError(camelCasedObj)) {
this.logger.debug('Found CliError in output. Returning CliResultError');
const cliError = plainToInstance(CliError, camelCasedObj);
return new CliResultError(cliError);
}

try {
const cliResult = plainToInstance(classConst, JSON_.parse(out)) as T;
this.logger.debug('Trying to parse CliResultSuccess');
const cliResult = plainToInstance(classConst, camelCasedObj) as T;
if (!cliResult) {
throw new Error('Failed to parse CliResultSuccess');
}

return new CliResultSuccess<T>(cliResult);
} catch {
this.logger.debug('Failed to parse CliResultSuccess. Parsing CliResultError');

try {
const cliError = plainToInstance(CliError, JSON_.parse(out));
if (!cliError) {
throw new Error('Failed to parse CliError');
}

return new CliResultError(cliError);
} catch {
this.logger.debug('Failed to parse any output Returning CliResultPanic');
return new CliResultPanic(exitCode, out);
}
this.logger.debug('Failed to parse any output Returning CliResultPanic');
return new CliResultPanic(exitCode, out);
}
}

4 changes: 4 additions & 0 deletions src/cli/models/cli-error.ts
Original file line number Diff line number Diff line change
@@ -5,3 +5,7 @@ export class CliError {
public message: string;
public softFail?: boolean = false;
}

export const isCliError = (obj: any): boolean => { // eslint-disable-line @typescript-eslint/no-explicit-any
return obj.message !== undefined && (obj.code !== undefined || obj.error !== undefined);
};

0 comments on commit dce56b0

Please sign in to comment.