Skip to content

Commit e606607

Browse files
committed
Address comments
1 parent b31764a commit e606607

File tree

6 files changed

+49
-23
lines changed

6 files changed

+49
-23
lines changed

packages/core/src/shared/sam/build.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import globals from '../extensionGlobals'
1919
import { TreeNode } from '../treeview/resourceTreeDataProvider'
2020
import { telemetry } from '../telemetry/telemetry'
2121
import { getSpawnEnv } from '../env/resolveEnv'
22-
import { getProjectRoot, getSamCliPathAndVersion, isDotnetRuntime } from './utils'
22+
import { getErrorCode, getProjectRoot, getSamCliPathAndVersion, isDotnetRuntime } from './utils'
2323
import { getConfigFileUri, validateSamBuildConfig } from './config'
2424
import { runInTerminal } from './processTerminal'
2525

@@ -236,7 +236,7 @@ export async function runBuild(arg?: TreeNode): Promise<SamBuildResult> {
236236
} catch (error) {
237237
throw ToolkitError.chain(error, 'Failed to build SAM template', {
238238
details: { ...resolveBuildArgConflict(buildFlags) },
239-
code: error instanceof ToolkitError ? error.code : undefined,
239+
code: getErrorCode(error),
240240
})
241241
}
242242
}

packages/core/src/shared/sam/deploy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { Wizard } from '../wizards/wizard'
2424
import { addTelemetryEnvVar } from './cli/samCliInvokerUtils'
2525
import { validateSamDeployConfig, SamConfig, writeSamconfigGlobal } from './config'
2626
import { TemplateItem, createStackPrompter, createBucketPrompter, createTemplatePrompter } from './sync'
27-
import { getProjectRoot, getSamCliPathAndVersion, getSource } from './utils'
27+
import { getErrorCode, getProjectRoot, getSamCliPathAndVersion, getSource } from './utils'
2828
import { runInTerminal } from './processTerminal'
2929

3030
export interface DeployParams {
@@ -394,7 +394,7 @@ export async function runDeploy(arg: any, wizardParams?: DeployParams): Promise<
394394
} catch (error) {
395395
throw ToolkitError.chain(error, 'Failed to deploy SAM template', {
396396
details: { ...deployFlags },
397-
code: error instanceof ToolkitError ? error.code : undefined,
397+
code: getErrorCode(error),
398398
})
399399
}
400400
return {

packages/core/src/shared/sam/processTerminal.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,16 @@ import { CancellationError } from '../utilities/timeoutUtils'
1212
import { getLogger } from '../logger'
1313
import { removeAnsi } from '../utilities/textUtilities'
1414
import { isAutomation } from '../vscode/env'
15-
import { getSamCliErrorMessage, SamCliErrorTypes } from './utils'
15+
import { throwIfErrorMatches } from './utils'
1616

1717
let oldTerminal: ProcessTerminal | undefined
1818
export async function runInTerminal(proc: ChildProcess, cmd: string) {
1919
const handleResult = (result?: ChildProcessResult) => {
2020
if (result && result.exitCode !== 0) {
21-
const defaultMessage = `sam ${cmd} exited with a non-zero exit code: ${result.exitCode}`
22-
if (result.stderr.includes('is up to date')) {
23-
throw ToolkitError.chain(result.error, defaultMessage, {
24-
code: 'NoUpdateExitCode',
25-
})
26-
}
27-
for (const errorType in SamCliErrorTypes) {
28-
const errorMessage = getSamCliErrorMessage(result.stderr)
29-
if (errorMessage.includes(SamCliErrorTypes[errorType as keyof typeof SamCliErrorTypes])) {
30-
throw ToolkitError.chain(result.error, errorMessage, {
31-
code: errorType,
32-
})
33-
}
34-
}
21+
throwIfErrorMatches(result)
3522

3623
// If no specific error matched, throw the default non-zero exit code error.
24+
const defaultMessage = `sam ${cmd} exited with a non-zero exit code: ${result.exitCode}`
3725
throw ToolkitError.chain(result.error, defaultMessage, {
3826
code: 'NonZeroExitCode',
3927
})

packages/core/src/shared/sam/sync.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import { IamConnection } from '../../auth/connection'
4141
import { CloudFormationTemplateRegistry } from '../fs/templateRegistry'
4242
import { TreeNode } from '../treeview/resourceTreeDataProvider'
4343
import { getSpawnEnv } from '../env/resolveEnv'
44-
import { getProjectRoot, getProjectRootUri, getSamCliPathAndVersion, getSource } from './utils'
44+
import { getErrorCode, getProjectRoot, getProjectRootUri, getSamCliPathAndVersion, getSource } from './utils'
4545
import { runInTerminal } from './processTerminal'
4646

4747
const localize = nls.loadMessageBundle()
@@ -663,7 +663,7 @@ export async function runSync(
663663
} catch (err) {
664664
throw ToolkitError.chain(err, 'Failed to sync SAM application', {
665665
details: { ...params },
666-
code: err instanceof ToolkitError ? err.code : undefined,
666+
code: getErrorCode(err),
667667
})
668668
}
669669
})

packages/core/src/shared/sam/utils.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { ToolkitError } from '../errors'
1515
import { SamCliSettings } from './cli/samCliSettings'
1616
import { SamCliInfoInvocation } from './cli/samCliInfo'
1717
import { parse } from 'semver'
18+
import { ChildProcessResult } from '../utilities/processUtils'
1819

1920
/**
2021
* @description determines the root directory of the project given Template Item
@@ -93,12 +94,27 @@ export function getSamCliErrorMessage(stderr: string): string {
9394
return lines[lines.length - 1]
9495
}
9596

97+
export function getErrorCode(error: unknown): string | undefined {
98+
return error instanceof ToolkitError ? error.code : undefined
99+
}
100+
101+
export function throwIfErrorMatches(result: ChildProcessResult) {
102+
const errorMessage = getSamCliErrorMessage(result.stderr)
103+
for (const errorType in SamCliErrorTypes) {
104+
if (errorMessage.includes(SamCliErrorTypes[errorType as keyof typeof SamCliErrorTypes])) {
105+
throw ToolkitError.chain(result.error, errorMessage, {
106+
code: errorType,
107+
})
108+
}
109+
}
110+
}
111+
96112
export enum SamCliErrorTypes {
97113
DockerUnreachable = 'Docker is unreachable.',
98114
ResolveS3AndS3Set = 'Cannot use both --resolve-s3 and --s3-bucket parameters in non-guided deployments.',
99115
DeployStackStatusMissing = 'Was not able to find a stack with the name:',
100116
DeployStackOutPutFailed = 'Failed to get outputs from stack',
101117
DeployBucketRequired = 'Templates with a size greater than 51,200 bytes must be deployed via an S3 Bucket.',
102-
NoUpdate = 'is up to date',
118+
NoUpdateExitCode = 'is up to date',
103119
ChangeSetEmpty = 'No changes to deploy. Stack is up to date',
104120
}

packages/core/src/test/shared/sam/utils.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ import {
1212
getSource,
1313
isDotnetRuntime,
1414
getSamCliErrorMessage,
15+
throwIfErrorMatches,
1516
} from '../../../shared/sam/utils'
1617
import { TemplateItem } from '../../../shared/sam/sync'
1718
import { RegionNode } from '../../../awsexplorer/regionNode'
1819
import { Region } from '../../../shared/regions/endpoints'
19-
import { RegionProvider } from '../../../shared'
20+
import { RegionProvider, ToolkitError } from '../../../shared'
2021
import { DeployedResource, DeployedResourceNode } from '../../../awsService/appBuilder/explorer/nodes/deployedNode'
22+
import { ChildProcessResult } from '../../../shared/utilities/processUtils'
2123

2224
describe('SAM utils', async function () {
2325
it('returns the projectRoot', async function () {
@@ -166,4 +168,24 @@ describe('SAM utils', async function () {
166168
)
167169
})
168170
})
171+
172+
describe('throwIfErrorMatches', async function () {
173+
it('should throw a ToolkitError with the correct code when an error message matches', () => {
174+
const mockError = new Error('Mock Error')
175+
const mockResult: ChildProcessResult = {
176+
exitCode: 1,
177+
error: mockError,
178+
stdout: '',
179+
stderr: 'Docker is unreachable.',
180+
}
181+
assert.throws(
182+
() => throwIfErrorMatches(mockResult),
183+
(e: any) => {
184+
assert.strictEqual(e instanceof ToolkitError, true)
185+
assert.strictEqual(e.code, 'DockerUnreachable')
186+
return true
187+
}
188+
)
189+
})
190+
})
169191
})

0 commit comments

Comments
 (0)