Skip to content

Commit

Permalink
Redirect view logs action to open the terminal instead of output channel
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfreder committed Nov 25, 2024
1 parent db097d0 commit 73e0812
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
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

0 comments on commit 73e0812

Please sign in to comment.