Skip to content

Commit

Permalink
fix: file write error when file is not present (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
pushpam5 authored Dec 2, 2024
1 parent 1d967b8 commit 651cce1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-pets-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"appwright": patch
---

fix: file write error when file is not present
17 changes: 10 additions & 7 deletions src/providers/browserstack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,13 @@ export class BrowserStackDeviceProvider implements DeviceProvider {
});
if (response.status !== 200) {
// Retry if not 200
logger.error(
throw new Error(
`Video not found: ${response.status} (URL: ${videoURL})`,
);
return;
}
const reader = response.body?.getReader();
if (!reader) {
logger.error("Failed to get reader from response body.");
return;
throw new Error("Failed to get reader from response body.");
}
const streamToFile = async () => {
// eslint-disable-next-line no-constant-condition
Expand All @@ -240,9 +238,14 @@ export class BrowserStackDeviceProvider implements DeviceProvider {
return new Promise((resolve, reject) => {
// Ensure file stream is closed even in case of an error
fileStream.on("finish", () => {
fs.renameSync(tempPathForWriting, pathToTestVideo);
logger.log(`Download finished and file closed: ${pathToTestVideo}`);
resolve({ path: pathToTestVideo, contentType: "video/mp4" });
try {
fs.renameSync(tempPathForWriting, pathToTestVideo);
logger.log(`Download finished and file closed: ${pathToTestVideo}`);
resolve({ path: pathToTestVideo, contentType: "video/mp4" });
} catch (err) {
logger.error(`Failed to rename file: `, err);
reject(err);
}
});

fileStream.on("error", (err) => {
Expand Down
17 changes: 10 additions & 7 deletions src/providers/lambdatest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,13 @@ export class LambdaTestDeviceProvider implements DeviceProvider {
});
if (response.status !== 200) {
// Retry if not 200
logger.error(
throw new Error(
`Video not found: ${response.status} (URL: ${videoURL})`,
);
return;
}
const reader = response.body?.getReader();
if (!reader) {
logger.error("Failed to get reader from response body.");
return;
throw new Error("Failed to get reader from response body.");
}
const streamToFile = async () => {
// eslint-disable-next-line no-constant-condition
Expand All @@ -223,9 +221,14 @@ export class LambdaTestDeviceProvider implements DeviceProvider {
return new Promise((resolve, reject) => {
// Ensure file stream is closed even in case of an error
fileStream.on("finish", () => {
fs.renameSync(tempPathForWriting, pathToTestVideo);
logger.log(`Download finished and file closed: ${pathToTestVideo}`);
resolve({ path: pathToTestVideo, contentType: "video/mp4" });
try {
fs.renameSync(tempPathForWriting, pathToTestVideo);
logger.log(`Download finished and file closed: ${pathToTestVideo}`);
resolve({ path: pathToTestVideo, contentType: "video/mp4" });
} catch (err) {
logger.error(`Failed to rename file: `, err);
reject(err);
}
});

fileStream.on("error", (err) => {
Expand Down

0 comments on commit 651cce1

Please sign in to comment.