Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 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)

// 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: 8 additions & 0 deletions packages/core/src/shared/sam/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ export function throwIfErrorMatches(result: ChildProcessResult) {
}
}

export function getTerminalFromError(error: any): vscode.Terminal {
if (error instanceof ToolkitError) {
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: 5 additions & 1 deletion packages/core/src/test/shared/sam/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,9 @@ describe('SAM runBuild', () => {
}
})

it('should throw ToolkitError when sync command fail', async () => {
it('should throw ToolkitError when build command fail', async () => {
getTestWindow().onDidShowMessage((m) => m.items.find((i) => i.title === 'View Logs In Terminal')?.select())

const prompterTester = PrompterTester.init()
.handleQuickPick('Specify parameter source for build', async (quickPick) => {
await quickPick.untilReady()
Expand Down Expand Up @@ -544,6 +546,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
Loading