From dce56b00fa8d013667e874856f6e26e6e6121a4b Mon Sep 17 00:00:00 2001 From: Ilya Siamionau Date: Tue, 29 Oct 2024 17:04:52 +0100 Subject: [PATCH] CM-41505- Fix CliError parsing (#110) --- CHANGELOG.md | 8 +++++++- src/cli/cli-wrapper.ts | 27 ++++++++++++--------------- src/cli/models/cli-error.ts | 4 ++++ 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2ed3c0..4527ba5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/cli/cli-wrapper.ts b/src/cli/cli-wrapper.ts index 50467c0..53b45f5 100644 --- a/src/cli/cli-wrapper.ts +++ b/src/cli/cli-wrapper.ts @@ -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(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); } } diff --git a/src/cli/models/cli-error.ts b/src/cli/models/cli-error.ts index 98410fc..bc3245e 100644 --- a/src/cli/models/cli-error.ts +++ b/src/cli/models/cli-error.ts @@ -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); +};