Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read Bug 404 #85

Merged
merged 5 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions lib/handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 new Error(error);
}
const document = await readDocument(Key, token, credentials.uri);
return document;
}

async function readDocument(Key, token, uri) {
Expand All @@ -30,12 +26,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 == "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;
}
}

Expand Down
10 changes: 3 additions & 7 deletions lib/sdm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 new Error(error);
}
const Key = response?.url;
const content = await readAttachment(Key, token, this.creds);
return content;
}

async draftSaveHandler(req) {
Expand Down
42 changes: 29 additions & 13 deletions test/lib/handler/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "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", () => {
Expand Down
14 changes: 9 additions & 5 deletions test/lib/sdm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,23 +158,27 @@ cds.context = {
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,
"tokenValue"
);
expect(readAttachment).toHaveBeenCalledWith(
"mockUrl",
token,
"mockToken", // Passing the mocked token value
service.creds
);
});
Expand Down
Loading