Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/core/src/shared/sam/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ import globals from '../extensionGlobals'
import { TreeNode } from '../treeview/resourceTreeDataProvider'
import { telemetry } from '../telemetry/telemetry'
import { getSpawnEnv } from '../env/resolveEnv'
import { getErrorCode, getProjectRoot, getSamCliPathAndVersion, isDotnetRuntime, updateRecentResponse } from './utils'
import {
getErrorCode,
getProjectRoot,
getSamCliPathAndVersion,
getTerminalFromError,
isDotnetRuntime,
updateRecentResponse,
} from './utils'
import { getConfigFileUri, validateSamBuildConfig } from './config'
import { runInTerminal } from './processTerminal'

Expand Down Expand Up @@ -240,7 +247,7 @@ export async function runBuild(arg?: TreeNode): Promise<SamBuildResult> {
}
} catch (error) {
throw ToolkitError.chain(error, 'Failed to build SAM template', {
details: { ...resolveBuildArgConflict(buildFlags) },
details: { terminal: getTerminalFromError(error), ...resolveBuildArgConflict(buildFlags) },
code: getErrorCode(error),
})
}
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/shared/sam/processTerminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ import { throwIfErrorMatches } from './utils'

let oldTerminal: ProcessTerminal | undefined
export async function runInTerminal(proc: ChildProcess, cmd: string) {
const handleResult = (result?: ChildProcessResult) => {
const handleResult = (result?: ChildProcessResult, terminal?: vscode.Terminal) => {
if (result && result.exitCode !== 0) {
throwIfErrorMatches(result)
throwIfErrorMatches(result, terminal)

// If no specific error matched, throw the default non-zero exit code error.
const defaultMessage = `sam ${cmd} exited with a non-zero exit code: ${result.exitCode}`
throw ToolkitError.chain(result.error, defaultMessage, {
code: 'NonZeroExitCode',
details: { terminal: terminal },
})
}
}
Expand Down Expand Up @@ -56,7 +57,7 @@ export async function runInTerminal(proc: ChildProcess, cmd: string) {
? ToolkitError.chain(result.error, 'SAM CLI was cancelled before exiting', { cancelled: true })
: new CancellationError('user')
} else {
return handleResult(result)
return handleResult(result, terminal)
}
}

Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/shared/sam/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,23 @@ export function getErrorCode(error: unknown): string | undefined {
return error instanceof ToolkitError ? error.code : undefined
}

export function throwIfErrorMatches(result: ChildProcessResult) {
export function throwIfErrorMatches(result: ChildProcessResult, terminal?: vscode.Terminal) {
const errorMessage = getSamCliErrorMessage(result.stderr)
for (const errorType in SamCliErrorTypes) {
if (errorMessage.includes(SamCliErrorTypes[errorType as keyof typeof SamCliErrorTypes])) {
throw ToolkitError.chain(result.error, errorMessage, {
code: errorType,
details: { terminal: terminal },
})
}
}
}

export function getTerminalFromError(error: any): vscode.Terminal {
return error.details?.['terminal'] as unknown as vscode.Terminal
//return vscode.window.activeTerminal as vscode.Terminal
}

export enum SamCliErrorTypes {
DockerUnreachable = 'Docker is unreachable.',
ResolveS3AndS3Set = 'Cannot use both --resolve-s3 and --s3-bucket parameters in non-guided deployments.',
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/shared/utilities/logAndShowUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,18 @@ export async function logAndShowError(
return
}
const logsItem = localize('AWS.generic.message.viewLogs', 'View Logs...')
const viewInTerminalItem = localize('AWS.generic.message.viewInTerminal', 'View Logs In Terminal')
const logId = getLogger().error(`${topic}: %s`, error)
const message = resolveErrorMessageToDisplay(error, defaultMessage)

if (error instanceof ToolkitError && error.documentationUri) {
await showMessageWithUrl(message, error.documentationUri, 'View Documentation', 'error')
} else if (error instanceof ToolkitError && (error.details?.['terminal'] as unknown as vscode.Terminal)) {
await vscode.window.showErrorMessage(message, viewInTerminalItem).then(async (resp) => {
if (resp === viewInTerminalItem) {
;(error.details?.['terminal'] as unknown as vscode.Terminal).show()
}
})
} else {
await vscode.window.showErrorMessage(message, logsItem).then(async (resp) => {
if (resp === logsItem) {
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/test/shared/sam/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ describe('SAM runBuild', () => {
}
})

it('should throw ToolkitError when sync command fail', async () => {
it('should throw ToolkitError when build command fail', async () => {
const prompterTester = PrompterTester.init()
.handleQuickPick('Specify parameter source for build', async (quickPick) => {
await quickPick.untilReady()
Expand All @@ -532,7 +532,7 @@ describe('SAM runBuild', () => {
value: sandbox.stub().resolves({
exitCode: -1,
stdout: 'Mock build command execution failure',
stderr: '',
stderr: 'Docker is unreachable.',
}),
},
})
Expand All @@ -544,6 +544,8 @@ describe('SAM runBuild', () => {
} catch (error: any) {
assert(error instanceof ToolkitError)
assert.strictEqual(error.message, 'Failed to build SAM template')
assert(error.details?.['terminal'] as unknown as vscode.Terminal)
assert.strictEqual((error.details?.['terminal'] as unknown as vscode.Terminal).name, 'SAM build')
}
prompterTester.assertCallAll()
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "Bug Fix",
"description": "'View Logs...' action redirects to output channel instead of Terminal"
}
Loading