Skip to content

Commit

Permalink
CR - change function structure
Browse files Browse the repository at this point in the history
1. get_identity_by_id_and_stat_file simplify and avoid the same try-catch as stat_account_config_file_by_identity
2. stat_account_config_file_by_identity avoid the nested try-catch

Signed-off-by: shirady <[email protected]>
  • Loading branch information
shirady committed Jan 6, 2025
1 parent 0785809 commit 5a85901
Showing 1 changed file with 30 additions and 32 deletions.
62 changes: 30 additions & 32 deletions src/sdk/config_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,11 @@ class ConfigFS {
identity = await this.get_identity_by_id(id, type, options);
const stat = await this.stat_account_config_file_by_identity(id, identity.name);
identity.stat = stat;
return identity; // this identity object should have also a stat property
} catch (err) {
dbg.error('get_identity_by_id_and_stat_file: got an error', err);
if (!identity && !options.silent_if_missing) {
const err_to_throw = new Error(`Could not find identity by id ${id}`);
err_to_throw.code = 'ENOENT';
throw err_to_throw;
}
throw err;
}
return identity; // this identity object should have also a stat property
}

/**
Expand Down Expand Up @@ -504,36 +500,38 @@ class ConfigFS {
*/
async stat_account_config_file_by_identity(id, account_name) {
const identity_path = this.get_identity_path_by_id(id);
let stat;
// stat by identity path
try {
const stat_by_identity_path = await nb_native().fs.stat(this.fs_context, identity_path);
stat = stat_by_identity_path;
} catch (err_stat_by_identity_path) {
if (err_stat_by_identity_path.code === 'ENOENT') {
dbg.warn('get_identity_by_id_and_stat_file: could not stat by identity ID will try to stat by account name');
const account_name_new_path = this.get_account_path_by_name(account_name);
try {
const stat_by_account_name = await nb_native().fs.stat(this.fs_context, account_name_new_path);
stat = stat_by_account_name;
} catch (err_stat_by_account_name_new_path) {
if (err_stat_by_identity_path.code === 'ENOENT') {
dbg.warn('get_identity_by_id_and_stat_file: could not stat by by account name (new accounts path)');
const account_name_old_path = this._get_old_account_path_by_name(account_name);
try {
const stat_by_account_name_old = await nb_native().fs.stat(this.fs_context, account_name_old_path);
stat = stat_by_account_name_old;
} catch (err_stat_by_account_name_old_path) {
dbg.warn('get_identity_by_id_and_stat_file: could not stat by by account name (old accounts path)');
// eslint-disable-next-line max-depth
const error_to_throw = new Error(`Could not stat identity by id ${id} or by account name ${account_name}`);
error_to_throw.code = 'ENOENT';
throw error_to_throw;
}
}
}
return stat_by_identity_path;
} catch (err) {
if (err.code !== 'ENOENT') {
dbg.error('get_identity_by_id_and_stat_file: could not stat by identity ID, got an error', err);
throw err;
}
}
// stat by account name (new path)
dbg.log2('get_identity_by_id_and_stat_file: will try to stat by account name (new accounts path)');
const account_name_new_path = this.get_account_path_by_name(account_name);
try {
const stat_by_account_name = await nb_native().fs.stat(this.fs_context, account_name_new_path);
return stat_by_account_name;
} catch (err) {
if (err.code !== 'ENOENT') {
dbg.error('get_identity_by_id_and_stat_file: could not stat by iby account name (new accounts path), got an error', err);
throw err;
}
}
return stat;
// stat by account name (old path)
dbg.log2('get_identity_by_id_and_stat_file: will try to stat by account name (old accounts path)');
const account_name_old_path = this._get_old_account_path_by_name(account_name);
try {
const stat_by_account_name_old = await nb_native().fs.stat(this.fs_context, account_name_old_path);
return stat_by_account_name_old;
} catch (err) {
dbg.error(`get_identity_by_id_and_stat_file: could not stat identity by id ${id} or by account name ${account_name} (new and old accounts path)`);
throw err;
}
}

/**
Expand Down

0 comments on commit 5a85901

Please sign in to comment.