Skip to content

Commit dce56b0

Browse files
authored
CM-41505- Fix CliError parsing (#110)
1 parent c4fddc9 commit dce56b0

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
## [v1.11.1]
6+
7+
- Fix CliError parsing
8+
59
## [v1.11.0]
610

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

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

110+
[v1.11.1]: https://github.com/cycodehq/vscode-extension/releases/tag/v1.11.1
111+
106112
[v1.11.0]: https://github.com/cycodehq/vscode-extension/releases/tag/v1.11.0
107113

108114
[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
141147

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

144-
[Unreleased]: https://github.com/cycodehq/vscode-extension/compare/v1.11.0...HEAD
150+
[Unreleased]: https://github.com/cycodehq/vscode-extension/compare/v1.11.1...HEAD

src/cli/cli-wrapper.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ILoggerService } from '../services/logger-service';
88
import { LoggerServiceSymbol } from '../symbols';
99
import JSON_ from '../utils/json_';
1010
import { ClassConstructor, plainToInstance } from 'class-transformer';
11-
import { CliError } from './models/cli-error';
11+
import { CliError, isCliError } from './models/cli-error';
1212
import { ExitCode } from './exit-code';
1313
import { CommandParameters } from './constants';
1414
import { getUserAgentArg } from './user-agent';
@@ -30,27 +30,24 @@ export class CliWrapper {
3030
return new CliResultSuccess(null);
3131
}
3232

33+
const camelCasedObj = JSON_.parse(out);
34+
if (isCliError(camelCasedObj)) {
35+
this.logger.debug('Found CliError in output. Returning CliResultError');
36+
const cliError = plainToInstance(CliError, camelCasedObj);
37+
return new CliResultError(cliError);
38+
}
39+
3340
try {
34-
const cliResult = plainToInstance(classConst, JSON_.parse(out)) as T;
41+
this.logger.debug('Trying to parse CliResultSuccess');
42+
const cliResult = plainToInstance(classConst, camelCasedObj) as T;
3543
if (!cliResult) {
3644
throw new Error('Failed to parse CliResultSuccess');
3745
}
3846

3947
return new CliResultSuccess<T>(cliResult);
4048
} catch {
41-
this.logger.debug('Failed to parse CliResultSuccess. Parsing CliResultError');
42-
43-
try {
44-
const cliError = plainToInstance(CliError, JSON_.parse(out));
45-
if (!cliError) {
46-
throw new Error('Failed to parse CliError');
47-
}
48-
49-
return new CliResultError(cliError);
50-
} catch {
51-
this.logger.debug('Failed to parse any output Returning CliResultPanic');
52-
return new CliResultPanic(exitCode, out);
53-
}
49+
this.logger.debug('Failed to parse any output Returning CliResultPanic');
50+
return new CliResultPanic(exitCode, out);
5451
}
5552
}
5653

src/cli/models/cli-error.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ export class CliError {
55
public message: string;
66
public softFail?: boolean = false;
77
}
8+
9+
export const isCliError = (obj: any): boolean => { // eslint-disable-line @typescript-eslint/no-explicit-any
10+
return obj.message !== undefined && (obj.code !== undefined || obj.error !== undefined);
11+
};

0 commit comments

Comments
 (0)