Skip to content

Commit

Permalink
chore: added comments and cleaned up VaultOps
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Nov 4, 2024
1 parent dc9e56c commit 0fb5db3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 49 deletions.
7 changes: 1 addition & 6 deletions src/client/handlers/VaultsSecretsWriteFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ class VaultsSecretsWriteFile extends UnaryHandler<
await vaultManager.withVaults(
[vaultId],
async (vault) => {
try {
await vaultOps.writeSecret(vault, input.secretName, secretContent);
} catch (e) {
// console.error('from rpc', e)
throw e;
}
await vaultOps.writeSecret(vault, input.secretName, secretContent);
},
tran,
);
Expand Down
22 changes: 13 additions & 9 deletions src/network/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,22 +573,26 @@ function toError(
}
const eClass = errors[errorData.type];
let e: any;
// If the error is a Polykey error, then we can instantiate that error.
// Otherwise, do special processing for non-Polykey errors.
if (eClass != null) {
e = eClass.fromJSON(errorData);
if (errorData.data != null && 'cause' in errorData.data) {
e.cause = toError(errorData.data.cause, clientMetadata, false);
}
} else {
// If we are at the top and we encounter an unwrapped error, then wrap it
// inside a `ErrorPolykeyUnexpected`. This will allow wrapping the error
// inside a `ErrorPolykeyRemote` and be properly transferred over the RPC.
// Otherwise, if we are not at the top, then it must mean that we are
// encountering an already-wrapped error. Return the error as-is.
if (top) {
let message = '';
if (errorData.type != null && typeof errorData.type === 'string') {
message = `Unexpected error of type ${errorData.type} occured`
} else {
message = `Unexpected error occured`
}
e = new errors.ErrorPolykeyUnexpected(message, {
cause: toError(errorData, clientMetadata, false),
});
e = new errors.ErrorPolykeyUnexpected(
`Unexpected error of type ${errorData.type} occured`,
{
cause: toError(errorData, clientMetadata, false),
},
);
} else {
return errorData;
}
Expand Down
54 changes: 21 additions & 33 deletions src/vaults/VaultOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,46 +264,34 @@ async function listSecrets(vault: Vault): Promise<string[]> {
});
}

// /**
// * Changes the contents of a secret. Creates a new file if it doesn't exist.
// */
// async function writeSecret(
// vault: Vault,
// secretName: string,
// content: Buffer | string,
// logger?: Logger,
// ): Promise<void> {
// await vault.writeF(async (efs) => {
// try {
// await efs.writeFile(secretName, content);
// logger?.info(`Wrote secret ${secretName} in vault ${vault.vaultId}`);
// } catch (e) {
// if (e.code === 'ENOENT') {
// throw new vaultsErrors.ErrorSecretsSecretUndefined(
// `One or more parent directories for '${secretName}' do not exist`,
// { cause: e },
// );
// }
// if (e.code === 'EISDIR') {
// throw new vaultsErrors.ErrorSecretsIsDirectory(
// `Secret path '${secretName}' is a directory`,
// { cause: e },
// );
// }
// throw e;
// }
// });
// }

/**
* Changes the contents of a secret. Creates a new file if it doesn't exist.
*/
async function writeSecret(
vault: Vault,
secretName: string,
content: Buffer | string,
logger?: Logger,
): Promise<void> {
await vault.writeF(async (efs) => {
await efs.writeFile(secretName, content);
logger?.info(`Wrote secret ${secretName} in vault ${vault.vaultId}`);
try {
await efs.writeFile(secretName, content);
logger?.info(`Wrote secret ${secretName} in vault ${vault.vaultId}`);
} catch (e) {
if (e.code === 'ENOENT') {
throw new vaultsErrors.ErrorSecretsSecretUndefined(
`One or more parent directories for '${secretName}' do not exist`,
{ cause: e },
);
}
if (e.code === 'EISDIR') {
throw new vaultsErrors.ErrorSecretsIsDirectory(
`Secret path '${secretName}' is a directory`,
{ cause: e },
);
}
throw e;
}
});
}

Expand Down
1 change: 0 additions & 1 deletion tests/client/handlers/vaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,6 @@ describe('vaultsSecretsWriteFile', () => {
secretContent: Buffer.from(dirName).toString('binary'),
});
} catch (e) {
console.error(e);
throw e.cause;
}
};
Expand Down

0 comments on commit 0fb5db3

Please sign in to comment.