Skip to content

Commit

Permalink
fix: enhance error handling in ProfilesTreeItem (#303)
Browse files Browse the repository at this point in the history
* enhance error handling in ProfilesTreeItem

* added tests

---------

Co-authored-by: Alex Yang <[email protected]>
  • Loading branch information
alexyaang and yangalex1 authored Jan 9, 2025
1 parent 18aa2f8 commit 8060199
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Follow the official documents to install the required softwares:
### Build the project

1. Clone this repo locally. (`git clone https://github.com/Microsoft/vscode-azureapicenter.git`)
1. Open a terminal and build locally. (`npm install && npm run package`)
1. Open a terminal and build locally. (`npm install && npm run watch`)

### Debug the project

Expand Down
10 changes: 10 additions & 0 deletions src/test/unit/tree/rules/ProfilesTreeItem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,14 @@ describe('ProfilesTreeItem', () => {
it('should indicate no more children', () => {
assert.strictEqual(profilesTreeItem.hasMoreChildrenImpl(), false);
});

it('should handle no analysis configs gracefully', async () => {
const apiCenterServiceStub = sandbox.stub(ApiCenterService.prototype, 'getApiCenterAnalyzerConfigs').resolves({ value: [] });
sandbox.stub(profilesTreeItem, 'createTreeItemsWithErrorHandling').resolves([]);

const children = await profilesTreeItem.loadMoreChildrenImpl(false, <IActionContext>{});

assert(apiCenterServiceStub.calledOnce);
assert.strictEqual(children.length, 0);
});
});
3 changes: 2 additions & 1 deletion src/tree/rules/ProfilesTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export class ProfilesTreeItem extends AzExtParentTreeItem {
const resourceGroupName = getResourceGroupFromId(this.apiCenter.id);
const apiCenterService = new ApiCenterService(this.parent?.subscription!, resourceGroupName, this.apiCenter.name);

const analyzerConfigs = (await apiCenterService.getApiCenterAnalyzerConfigs()).value;
// Fetch analyzer configurations, default to an empty array if none are found
const analyzerConfigs = (await apiCenterService.getApiCenterAnalyzerConfigs())?.value || [];

return await this.createTreeItemsWithErrorHandling(
analyzerConfigs,
Expand Down

0 comments on commit 8060199

Please sign in to comment.