From f60f79b013b4bdcea408c326424c98723886020c Mon Sep 17 00:00:00 2001 From: Rishi Kunnath <82925475+rishikunnath2747@users.noreply.github.com> Date: Mon, 13 Jan 2025 18:30:32 +0530 Subject: [PATCH 1/4] adding changes + UT --- lib/handler/index.js | 11 ++++----- lib/sdm.js | 2 +- test/lib/handler/index.test.js | 42 +++++++++++++++++++++++----------- test/lib/sdm.test.js | 14 ++++++++---- 4 files changed, 44 insertions(+), 25 deletions(-) diff --git a/lib/handler/index.js b/lib/handler/index.js index 6c78294..e79de63 100644 --- a/lib/handler/index.js +++ b/lib/handler/index.js @@ -7,7 +7,7 @@ async function readAttachment(Key, token, credentials) { const document = await readDocument(Key, token, credentials.uri); return document; } catch (error) { - throw new Error(error); + throw error; } } @@ -30,12 +30,11 @@ async function readDocument(Key, token, uri) { const responseBuffer = Buffer.from(response.data, "binary"); return responseBuffer; } catch (error) { - let statusText = "An Error Occurred"; - if (error.response?.statusText) { - statusText = error.response.statusText; + if (error.message = "Error: AxiosError: Request failed with status code 404" && error.status == 404){ + error.message = "Attachment not found in the repository" } - - throw new Error(statusText); + error.code = error.status + throw error; } } diff --git a/lib/sdm.js b/lib/sdm.js index f16f88d..bc3deb6 100644 --- a/lib/sdm.js +++ b/lib/sdm.js @@ -66,7 +66,7 @@ module.exports = class SDMAttachmentsService extends ( const content = await readAttachment(Key, token, this.creds); return content; } catch (error) { - throw new Error(error); + throw error; } } diff --git a/test/lib/handler/index.test.js b/test/lib/handler/index.test.js index 5b082a8..b13b907 100644 --- a/test/lib/handler/index.test.js +++ b/test/lib/handler/index.test.js @@ -68,27 +68,43 @@ describe("handlers", () => { axios.get.mockImplementationOnce(() => Promise.reject({ response: { - statusText: "something bad happened", - }, + code: 500, + message: "Could not read the attachment", + } }) ); - + await expect( readAttachment("123", "a1b2c3", { uri: "http://example.com/" }) - ).rejects.toThrow("something bad happened"); - }); - - it('throws error with "An Error Occurred" message when statusText is missing', async () => { + ).rejects.toMatchObject({ + response: { + code: 500, + message: "Could not read the attachment", + }, + }); + }); + + it("throws specific error message for 404 status", async () => { + let actualError = { + message: "Error: AxiosError: Request failed with status code 404", + code: "AN ERROR OCCURRED", + status: 404, + }; + + let checkError = { + message: "Attachment not found in the repository", + code: 404, + status: 404, + }; + axios.get.mockImplementationOnce(() => - Promise.reject({ - response: {}, - }) + Promise.reject(actualError) ); - + await expect( readAttachment("123", "a1b2c3", { uri: "http://example.com/" }) - ).rejects.toThrow("An Error Occurred"); - }); + ).rejects.toMatchObject(checkError); + }); }); describe("getRepositoryInfo", () => { diff --git a/test/lib/sdm.test.js b/test/lib/sdm.test.js index 8a2ae1d..b7c1e6f 100644 --- a/test/lib/sdm.test.js +++ b/test/lib/sdm.test.js @@ -135,15 +135,19 @@ describe("SDMAttachmentsService", () => { const attachments = ["attachment1", "attachment2"]; const keys = ["key1", "key2"]; const response = { url: "mockUrl" }; + const errorMessage = new Error("Attachment not found in the repository"); + errorMessage.code = 404; + getURLFromAttachments.mockResolvedValueOnce(response); + fetchAccessToken.mockResolvedValueOnce("mockToken"); readAttachment.mockImplementationOnce(() => { - throw new Error("Error reading attachment"); + throw errorMessage; }); - + await expect(service.get(attachments, keys, req)).rejects.toThrow( - "Error reading attachment" + errorMessage ); - + expect(getURLFromAttachments).toHaveBeenCalledWith(keys, attachments); expect(fetchAccessToken).toHaveBeenCalledWith( service.creds, @@ -151,7 +155,7 @@ describe("SDMAttachmentsService", () => { ); expect(readAttachment).toHaveBeenCalledWith( "mockUrl", - token, + "mockToken", // Passing the mocked token value service.creds ); }); From bc83384b9cb4e87c38e9c106548925bac186eb61 Mon Sep 17 00:00:00 2001 From: Rishi Kunnath <82925475+rishikunnath2747@users.noreply.github.com> Date: Thu, 16 Jan 2025 10:36:12 +0530 Subject: [PATCH 2/4] sonar fix --- lib/handler/index.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/handler/index.js b/lib/handler/index.js index e79de63..7618b49 100644 --- a/lib/handler/index.js +++ b/lib/handler/index.js @@ -3,12 +3,8 @@ const axios = require("axios").default; const FormData = require("form-data"); async function readAttachment(Key, token, credentials) { - try { - const document = await readDocument(Key, token, credentials.uri); - return document; - } catch (error) { - throw error; - } + const document = await readDocument(Key, token, credentials.uri); + return document; } async function readDocument(Key, token, uri) { @@ -30,7 +26,7 @@ async function readDocument(Key, token, uri) { const responseBuffer = Buffer.from(response.data, "binary"); return responseBuffer; } catch (error) { - if (error.message = "Error: AxiosError: Request failed with status code 404" && error.status == 404){ + if (error.message == "Error: AxiosError: Request failed with status code 404" && error.status == 404){ error.message = "Attachment not found in the repository" } error.code = error.status From 8bdff57a859c3e4a84b371a2809bdc4b129e1127 Mon Sep 17 00:00:00 2001 From: Rishi Kunnath <82925475+rishikunnath2747@users.noreply.github.com> Date: Thu, 16 Jan 2025 11:10:48 +0530 Subject: [PATCH 3/4] lint fix --- lib/handler/index.js | 2 +- lib/sdm.js | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/handler/index.js b/lib/handler/index.js index 7618b49..f9438f9 100644 --- a/lib/handler/index.js +++ b/lib/handler/index.js @@ -26,7 +26,7 @@ async function readDocument(Key, token, uri) { const responseBuffer = Buffer.from(response.data, "binary"); return responseBuffer; } catch (error) { - if (error.message == "Error: AxiosError: Request failed with status code 404" && error.status == 404){ + if (error.message == "Request failed with status code 404" && error.status == 404){ error.message = "Attachment not found in the repository" } error.code = error.status diff --git a/lib/sdm.js b/lib/sdm.js index 57554a1..9600ce3 100644 --- a/lib/sdm.js +++ b/lib/sdm.js @@ -62,13 +62,9 @@ module.exports = class SDMAttachmentsService extends ( this.creds, req.user.tokenInfo.getTokenValue() ); - try { - const Key = response?.url; - const content = await readAttachment(Key, token, this.creds); - return content; - } catch (error) { - throw error; - } + const Key = response?.url; + const content = await readAttachment(Key, token, this.creds); + return content; } async draftSaveHandler(req) { From 6335b9c46993bc7959d58cbd007e31ae2bb6f8a4 Mon Sep 17 00:00:00 2001 From: Rishi Kunnath <82925475+rishikunnath2747@users.noreply.github.com> Date: Thu, 16 Jan 2025 11:22:16 +0530 Subject: [PATCH 4/4] fix ut --- test/lib/handler/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lib/handler/index.test.js b/test/lib/handler/index.test.js index b13b907..127b149 100644 --- a/test/lib/handler/index.test.js +++ b/test/lib/handler/index.test.js @@ -86,7 +86,7 @@ describe("handlers", () => { it("throws specific error message for 404 status", async () => { let actualError = { - message: "Error: AxiosError: Request failed with status code 404", + message: "Request failed with status code 404", code: "AN ERROR OCCURRED", status: 404, };