Skip to content

Commit b5c4528

Browse files
authored
Merge pull request #184 from codacy/add-logs-CF-2161
chore: Add try...catch and logs CF-2161
2 parents 3d8b8c4 + d6aa639 commit b5c4528

File tree

4 files changed

+121
-58
lines changed

4 files changed

+121
-58
lines changed

src/extension.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,27 @@ export async function activate(context: vscode.ExtensionContext) {
272272

273273
// add views
274274
const statusBar = new StatusBar(context, codacyCloud)
275-
context.subscriptions.push(new PullRequestSummaryTree(context, codacyCloud))
275+
276+
// Create tree views with error handling - if one fails, others can still work
277+
try {
278+
context.subscriptions.push(new PullRequestSummaryTree(context, codacyCloud))
279+
} catch (error) {
280+
Logger.error(`Failed to initialize PullRequestView: ${error}`)
281+
}
282+
276283
context.subscriptions.push(statusBar)
277-
context.subscriptions.push(new PullRequestsTree(context, codacyCloud))
278-
context.subscriptions.push(new BranchIssuesTree(context, codacyCloud))
284+
285+
try {
286+
context.subscriptions.push(new PullRequestsTree(context, codacyCloud))
287+
} catch (error) {
288+
Logger.error(`Failed to initialize OpenPullRequestsView: ${error}`)
289+
}
290+
291+
try {
292+
context.subscriptions.push(new BranchIssuesTree(context, codacyCloud))
293+
} catch (error) {
294+
Logger.error(`Failed to initialize BranchIssuesView: ${error}`)
295+
}
279296

280297
// initialize the problems diagnostic collection with status bar reference
281298
context.subscriptions.push(new ProblemsDiagnosticCollection(codacyCloud, statusBar))

src/views/BranchIssuesTree.ts

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import {
99
import { BranchIssue, MAX_FETCH_BRANCH_ISSUES } from '../git/IssuesManager'
1010
import { differenceInDays } from 'date-fns'
1111
import { Account } from '../codacy/Account'
12+
import Logger from '../common/logger'
1213

1314
const RECENTLY_ADDED_DAYS = 60
1415

1516
export class BranchIssuesTree extends vscode.Disposable implements vscode.TreeDataProvider<BranchIssuesTreeNode> {
1617
private _onDidChangeTreeData = new vscode.EventEmitter<BranchIssuesTreeNode | void>()
1718
readonly onDidChangeTreeData = this._onDidChangeTreeData.event
1819
private _disposables: vscode.Disposable[] = []
19-
private _view: vscode.TreeView<BranchIssuesTreeNode>
20+
private _view: vscode.TreeView<BranchIssuesTreeNode> | undefined
2021

2122
private _allIssues: BranchIssue[] = []
2223

@@ -27,29 +28,44 @@ export class BranchIssuesTree extends vscode.Disposable implements vscode.TreeDa
2728
super(() => this.dispose())
2829

2930
// create the tree view
30-
this._view = vscode.window.createTreeView('codacy:branchIssues', {
31-
treeDataProvider: this,
32-
showCollapseAll: true,
33-
})
34-
35-
this._context.subscriptions.push(this._view)
36-
37-
// create a command to open the pull request summaryToo
38-
this._context.subscriptions.push(
39-
vscode.commands.registerCommand('codacy.branchIssues.open', () => {
40-
this._view.reveal(this._view.selection[0])
31+
try {
32+
this._view = vscode.window.createTreeView('codacy:branchIssues', {
33+
treeDataProvider: this,
34+
showCollapseAll: true,
4135
})
42-
)
4336

44-
// subsribe to changes in the current branch
45-
this._codacyCloud.branchIssues.onDidUpdateBranchIssues((issues: BranchIssue[]) => {
46-
this._allIssues = issues
47-
this._onDidChangeTreeData.fire()
48-
49-
this._view.title = `${this._codacyCloud.head?.name} - ${issues.length}${
50-
issues.length >= MAX_FETCH_BRANCH_ISSUES ? '+' : ''
51-
} issues`
52-
})
37+
this._context.subscriptions.push(this._view)
38+
Logger.debug('BranchIssuesView created successfully')
39+
40+
// create a command to open the pull request summary
41+
this._context.subscriptions.push(
42+
vscode.commands.registerCommand('codacy.branchIssues.open', () => {
43+
if (this._view) {
44+
this._view.reveal(this._view.selection[0])
45+
}
46+
})
47+
)
48+
49+
// subscribe to changes in the current branch
50+
this._codacyCloud.branchIssues.onDidUpdateBranchIssues((issues: BranchIssue[]) => {
51+
this._allIssues = issues
52+
this._onDidChangeTreeData.fire()
53+
54+
if (this._view) {
55+
this._view.title = `${this._codacyCloud.head?.name} - ${issues.length}${
56+
issues.length >= MAX_FETCH_BRANCH_ISSUES ? '+' : ''
57+
} issues`
58+
}
59+
})
60+
} catch (error) {
61+
const errorMessage = error instanceof Error ? error.message : String(error)
62+
Logger.error(`Failed to create BranchIssuesView: ${errorMessage}`)
63+
Logger.error(
64+
`[BranchIssuesView] Error stack: ${error instanceof Error ? error.stack : '[BranchIssuesView] No stack trace'}`
65+
)
66+
this._view = undefined
67+
// Don't re-throw - let extension continue even if this view fails
68+
}
5369
}
5470

5571
getTreeItem(element: BranchIssuesTreeNode): vscode.TreeItem | Thenable<vscode.TreeItem> {

src/views/PullRequestSummaryTree.ts

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
PullRequestIssuesNode,
99
PullRequestSummaryNode,
1010
} from './nodes'
11+
import Logger from '../common/logger'
1112

1213
export class PullRequestSummaryTree
1314
extends vscode.Disposable
@@ -16,7 +17,7 @@ export class PullRequestSummaryTree
1617
private _onDidChangeTreeData = new vscode.EventEmitter<PullRequestSummaryNode | void>()
1718
readonly onDidChangeTreeData = this._onDidChangeTreeData.event
1819
private _disposables: vscode.Disposable[] = []
19-
private _view: vscode.TreeView<PullRequestSummaryNode>
20+
private _view: vscode.TreeView<PullRequestSummaryNode> | undefined
2021

2122
constructor(
2223
private _context: vscode.ExtensionContext,
@@ -25,30 +26,45 @@ export class PullRequestSummaryTree
2526
super(() => this.dispose())
2627

2728
// create the tree view
28-
this._view = vscode.window.createTreeView('codacy:prSummary', {
29-
treeDataProvider: this,
30-
showCollapseAll: true,
31-
})
29+
try {
30+
this._view = vscode.window.createTreeView('codacy:prSummary', {
31+
treeDataProvider: this,
32+
showCollapseAll: true,
33+
})
3234

33-
this._context.subscriptions.push(this._view)
35+
this._context.subscriptions.push(this._view)
36+
Logger.debug('PullRequestView created successfully')
3437

35-
// create a command to open the pull request summary
36-
this._context.subscriptions.push(
37-
vscode.commands.registerCommand('codacy.pr.openSummary', () => {
38-
this._view.reveal(this._view.selection[0])
39-
})
40-
)
38+
// create a command to open the pull request summary
39+
this._context.subscriptions.push(
40+
vscode.commands.registerCommand('codacy.pr.openSummary', () => {
41+
if (this._view) {
42+
this._view.reveal(this._view.selection[0])
43+
}
44+
})
45+
)
4146

42-
// subscribe to changes in the pull request
43-
this._codacyCloud.onDidUpdatePullRequest((pr) => {
44-
this._onDidChangeTreeData.fire()
47+
// subscribe to changes in the pull request
48+
this._codacyCloud.onDidUpdatePullRequest((pr) => {
49+
this._onDidChangeTreeData.fire()
4550

46-
if (pr) {
47-
this._view.title = `Pull Request #${pr.meta.number}`
48-
} else {
49-
this._view.title = 'Pull Request'
50-
}
51-
})
51+
if (this._view) {
52+
if (pr) {
53+
this._view.title = `Pull Request #${pr.meta.number}`
54+
} else {
55+
this._view.title = 'Pull Request'
56+
}
57+
}
58+
})
59+
} catch (error) {
60+
const errorMessage = error instanceof Error ? error.message : String(error)
61+
Logger.error(`Failed to create PullRequestView: ${errorMessage}`)
62+
Logger.error(
63+
`[PullRequestView] Error stack: ${error instanceof Error ? error.stack : '[PullRequestView] No stack trace'}`
64+
)
65+
this._view = undefined
66+
// Don't re-throw - let extension continue even if this view fails
67+
}
5268
}
5369

5470
getTreeItem(element: PullRequestSummaryNode): vscode.TreeItem | Thenable<vscode.TreeItem> {

src/views/PullRequestsTree.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import * as vscode from 'vscode'
22
import { CodacyCloud } from '../git/CodacyCloud'
33
import { PullRequestNode } from './nodes/PullRequestNode'
44
import { PullRequestSummaryNode } from './nodes'
5+
import Logger from '../common/logger'
56

67
type PullRequestTreeNodeType = PullRequestNode | PullRequestSummaryNode
78
export class PullRequestsTree extends vscode.Disposable implements vscode.TreeDataProvider<PullRequestTreeNodeType> {
89
private _onDidChangeTreeData = new vscode.EventEmitter<PullRequestTreeNodeType | void>()
910
readonly onDidChangeTreeData = this._onDidChangeTreeData.event
1011
private _disposables: vscode.Disposable[] = []
11-
private _view: vscode.TreeView<PullRequestTreeNodeType>
12+
private _view: vscode.TreeView<PullRequestTreeNodeType> | undefined
1213

1314
constructor(
1415
private _context: vscode.ExtensionContext,
@@ -17,19 +18,32 @@ export class PullRequestsTree extends vscode.Disposable implements vscode.TreeDa
1718
super(() => this.dispose())
1819

1920
// create the tree view
20-
this._view = vscode.window.createTreeView('codacy:pullRequests', {
21-
treeDataProvider: this,
22-
showCollapseAll: true,
23-
})
21+
try {
22+
this._view = vscode.window.createTreeView('codacy:pullRequests', {
23+
treeDataProvider: this,
24+
showCollapseAll: true,
25+
})
2426

25-
this._context.subscriptions.push(this._view)
27+
this._context.subscriptions.push(this._view)
28+
Logger.debug('OpenPullRequestsView created successfully')
2629

27-
// create a command to open the pull requests list
28-
this._context.subscriptions.push(
29-
vscode.commands.registerCommand('codacy.pullRequests.open', () => {
30-
this._view.reveal(this._view.selection[0])
31-
})
32-
)
30+
// create a command to open the pull requests list
31+
this._context.subscriptions.push(
32+
vscode.commands.registerCommand('codacy.pullRequests.open', () => {
33+
if (this._view) {
34+
this._view.reveal(this._view.selection[0])
35+
}
36+
})
37+
)
38+
} catch (error) {
39+
const errorMessage = error instanceof Error ? error.message : String(error)
40+
Logger.error(`Failed to create OpenPullRequestsView: ${errorMessage}`)
41+
Logger.error(
42+
`[OpenPullRequestsView] Error stack: ${error instanceof Error ? error.stack : '[OpenPullRequestsView] No stack trace'}`
43+
)
44+
this._view = undefined
45+
// Don't re-throw - let extension continue even if this view fails
46+
}
3347

3448
// subscribe to changes in the pull request
3549
this._codacyCloud.onDidUpdatePullRequests(() => {

0 commit comments

Comments
 (0)