Skip to content

Commit

Permalink
[main] error handling for exists
Browse files Browse the repository at this point in the history
  • Loading branch information
dubdia committed Nov 6, 2024
1 parent fe9aead commit accf6d2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
26 changes: 6 additions & 20 deletions src/main/script-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,17 +861,9 @@ export class ScriptManager {
downloadFile: (remoteFilePath, localFilePath, options) =>
execAsync("remote download file", options?.ignoreErrors, async () => {
// check
try {
const stats = await remote.connection.sftp.exists(remoteFilePath);
if (stats == null || !stats.isFile()) {
throw new Error("File on remote does not exists or is not a file");
}
} catch (err) {
if (err == "No such file") {
throw new Error("File on remote does not exists");
} else {
throw err;
}
const stats = await remote.connection.sftp.exists(remoteFilePath);
if (stats == null || !stats.isFile()) {
throw new Error("File on remote does not exists or is not a file");
}
if (options?.overwrite !== true) {
if (fs.existsSync(localFilePath)) {
Expand All @@ -888,15 +880,9 @@ export class ScriptManager {
throw new Error("Source local file does not exists");
}
if (options?.overwrite !== true) {
try {
const stats = await remote.connection.sftp.exists(remoteFilePath);
if (stats) {
throw new Error("Target remote file already exists");
}
} catch (err) {
if (err != "No such file") {
throw err;
}
const stats = await remote.connection.sftp.exists(remoteFilePath);
if (stats) {
throw new Error("Target remote file already exists");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/ssh2-promise/src/sftp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ export class SFTP extends BaseSFTP {
/** returns whether given path exists */
public async exists(path: string): Promise<Stats | null> {
try {
const stats = await this.stat(path);
const stats = await this.lstat(path);
if (stats) {
return stats;
} else {
throw new Error("No stats and no error");
}
} catch (err) {
if (err && (err.code === "ENOENT" || err.code == 2)) {
if (err?.code === "ENOENT" || err?.code == 2 || err == "No such file") {
return null;
} else {
throw err;
Expand Down

0 comments on commit accf6d2

Please sign in to comment.