Skip to content

Commit

Permalink
fix: handle errors from Git extension APIs (#562)
Browse files Browse the repository at this point in the history
fix: handle errors from Git extension APIs

Wrap calls to Git extension APIs in try/catch blocks to avoid crashes if
APIs throw errors or are unavailable.

Log warnings if extension is unavailable or inactive instead of throwing
errors.

## Test plan

<!-- Required. See
https://docs.sourcegraph.com/dev/background-information/testing_principles.
-->

added error handling
  • Loading branch information
abeatrix authored Aug 3, 2023
1 parent 6f402ef commit f3ab7cb
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions vscode/src/repository/repositoryHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,27 @@ export function repositoryRemoteUrl(uri: vscode.Uri): string | undefined {
}

function gitRepositoryRemoteUrl(uri: vscode.Uri): string | undefined {
const extension = vscode.extensions.getExtension<GitExtension>('vscode.git')
if (!extension) {
console.warn('Git extension not available')
return undefined
}
if (!extension.isActive) {
console.warn('Git extension not active')
return undefined
}
try {
const extension = vscode.extensions.getExtension<GitExtension>('vscode.git')
if (!extension) {
console.warn('Git extension not available')
return undefined
}
if (!extension.isActive) {
console.warn('Git extension not active')
return undefined
}

const git = extension.exports.getAPI(1)
const repository = git.getRepository(uri)
if (!repository) {
console.warn('No Git repository for URI', uri)
return undefined
}

const git = extension.exports.getAPI(1)
const repository = git.getRepository(uri)
if (!repository) {
console.warn('No Git repository for URI', uri)
return repository.state.remotes[0]?.fetchUrl
} catch (error) {
console.error(error)
return undefined
}

return repository.state.remotes[0]?.fetchUrl
}

0 comments on commit f3ab7cb

Please sign in to comment.