From dc10fe936809657721421d6b806d5bbbb9ef6685 Mon Sep 17 00:00:00 2001 From: likhithanimma1 <142219673+likhithanimma1@users.noreply.github.com> Date: Tue, 11 Feb 2025 19:02:54 +0530 Subject: [PATCH] Add ErrorHandling for OpenWithEncoding function (#3438) * Add ErrorHandling Signed-off-by: likhithanimma1 <142219673+likhithanimma1@users.noreply.github.com> * Increase unit test coverage Signed-off-by: likhithanimma1 <142219673+likhithanimma1@users.noreply.github.com> --------- Signed-off-by: likhithanimma1 <142219673+likhithanimma1@users.noreply.github.com> Co-authored-by: Fernando Rijo Cedeno <37381190+zFernand0@users.noreply.github.com> --- .../__unit__/trees/job/JobTree.unit.test.ts | 13 +++++++++++++ packages/zowe-explorer/src/trees/job/JobTree.ts | 14 +++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/zowe-explorer/__tests__/__unit__/trees/job/JobTree.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/trees/job/JobTree.unit.test.ts index 546a78b186..8fc0683300 100644 --- a/packages/zowe-explorer/__tests__/__unit__/trees/job/JobTree.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/trees/job/JobTree.unit.test.ts @@ -1393,4 +1393,17 @@ describe("openWithEncoding", () => { expect(fetchSpoolAtUriMock).toHaveBeenCalledTimes(0); expect(executeCommandMock).toHaveBeenCalledTimes(0); }); + + it("should catch if error is thrown", async () => { + const testTree = new JobTree(); + const spoolNode = new ZoweSpoolNode({ label: "SPOOL", collapsibleState: vscode.TreeItemCollapsibleState.None, spool: createIJobFile() }); + const encoding: ZosEncoding = { kind: "other", codepage: "IBM-1147" }; + const promptMock = jest.spyOn(SharedUtils, "promptForEncoding").mockResolvedValue(encoding); + jest.spyOn(JobFSProvider.instance, "fetchSpoolAtUri").mockImplementationOnce(() => { + throw new Error("testError"); + }); + await testTree.openWithEncoding(spoolNode); + expect(promptMock).toHaveBeenCalledWith(spoolNode); + expect(promptMock).toHaveBeenCalledTimes(1); + }); }); diff --git a/packages/zowe-explorer/src/trees/job/JobTree.ts b/packages/zowe-explorer/src/trees/job/JobTree.ts index c508dee76d..58da4ac4a8 100644 --- a/packages/zowe-explorer/src/trees/job/JobTree.ts +++ b/packages/zowe-explorer/src/trees/job/JobTree.ts @@ -1186,11 +1186,15 @@ export class JobTree extends ZoweTreeProvider implements Types public async openWithEncoding(node: IZoweJobTreeNode, encoding?: ZosEncoding): Promise { encoding ??= await SharedUtils.promptForEncoding(node); - if (encoding !== undefined) { - // Set the encoding, fetch the new contents with the encoding, and open the spool file. - await node.setEncoding(encoding); - await JobFSProvider.instance.fetchSpoolAtUri(node.resourceUri); - await vscode.commands.executeCommand("vscode.open", node.resourceUri); + try { + if (encoding !== undefined) { + // Set the encoding, fetch the new contents with the encoding, and open the spool file. + await node.setEncoding(encoding); + await JobFSProvider.instance.fetchSpoolAtUri(node.resourceUri); + await vscode.commands.executeCommand("vscode.open", node.resourceUri); + } + } catch (err) { + await AuthUtils.errorHandling(err, { profile: node.getProfile() }); } } }