Skip to content

Commit

Permalink
feat: removed undefined errors from vaultops
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Nov 1, 2024
1 parent 8c1cc9a commit 995e1b3
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 35 deletions.
22 changes: 19 additions & 3 deletions src/client/handlers/VaultsSecretsMkdir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,25 @@ class VaultsSecretsMkdir extends DuplexHandler<
yield await vaultManager.withVaults(
[vaultId],
async (vault) => {
return await vaultOps.mkdir(vault, dirName, {
recursive: metadata?.options?.recursive,
});
try {
await vaultOps.mkdir(vault, dirName, {
recursive: metadata?.options?.recursive,
});
return { type: 'success', success: true };
} catch (e) {
if (
e instanceof vaultsErrors.ErrorVaultsRecursive ||
e instanceof vaultsErrors.ErrorSecretsSecretDefined
) {
return {
type: 'error',
code: e.cause.code,
reason: dirName,
};
} else {
throw e;
}
}
},
tran,
);
Expand Down
2 changes: 1 addition & 1 deletion src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ type SuccessMessage = {

type ErrorMessage = {
type: 'error';
code: string;
code?: string | number;
reason?: string;
data?: JSONObject;
};
Expand Down
22 changes: 9 additions & 13 deletions src/vaults/VaultOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import type Logger from '@matrixai/logger';
import type { Vault } from './Vault';
import type { Stat } from 'encryptedfs';
import type { SuccessOrErrorMessage } from '../client/types';
import path from 'path';
import * as vaultsErrors from './errors';
import * as vaultsUtils from './utils';
Expand Down Expand Up @@ -174,7 +173,7 @@ async function mkdir(
dirPath: string,
fileOptions?: FileOptions,
logger?: Logger,
): Promise<SuccessOrErrorMessage> {
): Promise<void> {
const recursive = fileOptions?.recursive ?? false;
// Technically, writing an empty directory won't make a commit, and doesn't
// need a write resource as git doesn't track empty directories. It is
Expand All @@ -184,22 +183,19 @@ async function mkdir(
await efs.mkdir(dirPath, fileOptions);
logger?.info(`Created secret directory at '${dirPath}'`);
});
return { type: 'success', success: true };
} catch (e) {
logger?.error(`Failed to create directory '${dirPath}'. Reason: ${e.code}`);
if (e.code === 'ENOENT' && !recursive) {
return {
type: 'error',
code: e.code,
reason: dirPath,
};
throw new vaultsErrors.ErrorVaultsRecursive(
`Could not create direcotry '${dirPath}' without recursive option`,
{ cause: e },
);
}
if (e.code === 'EEXIST') {
return {
type: 'error',
code: e.code,
reason: dirPath,
};
throw new vaultsErrors.ErrorSecretsSecretDefined(
`${dirPath} already exists`,
{ cause: e },
);
}
throw e;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/client/handlers/vaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,27 @@ describe('vaultsSecretsMkdir', () => {
recursive: true,
});
});
test('fails with invalid vault name', async () => {
const vaultName = 'test-vault';
const dirName = 'dir';
const response = await rpcClient.methods.vaultsSecretsMkdir();
const writer = response.writable.getWriter();
await writer.write({
nameOrId: vaultName,
dirName: dirName,
});
await writer.close();
const consumeP = async () => {
try {
for await (const _ of response.readable);
} catch (e) {
throw e.cause;
}
};
await expect(consumeP()).rejects.toThrow(
vaultsErrors.ErrorVaultsVaultUndefined,
);
});
test('makes a directory', async () => {
const vaultName = 'test-vault';
const vaultId = await vaultManager.createVault(vaultName);
Expand Down
32 changes: 14 additions & 18 deletions tests/vaults/VaultOps/mkdir.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { DB } from '@matrixai/db';
import VaultInternal from '@/vaults/VaultInternal';
import * as vaultOps from '@/vaults/VaultOps';
import * as vaultsUtils from '@/vaults/utils';
import * as vaultsErrors from '@/vaults/errors';
import * as keysUtils from '@/keys/utils';
import * as testNodesUtils from '../../nodes/utils';
import * as testVaultsUtils from '../utils';
Expand Down Expand Up @@ -89,44 +90,39 @@ describe('mkdir', () => {
});

test('can create directory', async () => {
const response = await vaultOps.mkdir(vault, dirName);
expect(response.type).toEqual('success');
await vaultOps.mkdir(vault, dirName);
await testVaultsUtils.expectDirExists(vault, dirName);
});
test('can create recursive directory', async () => {
const dirPath = path.join(dirName, dirName);
const response = await vaultOps.mkdir(vault, dirPath, {
await vaultOps.mkdir(vault, dirPath, {
recursive: true,
});
expect(response.type).toEqual('success');
await testVaultsUtils.expectDirExists(vault, dirPath);
});
test('creating directories fails without recursive', async () => {
const dirPath = path.join(dirName, dirName);
const response = await vaultOps.mkdir(vault, dirPath);
expect(response.type).toEqual('error');
const error = response as ErrorMessage;
expect(error.code).toEqual('ENOENT');
await expect(vaultOps.mkdir(vault, dirPath)).rejects.toThrow(
vaultsErrors.ErrorVaultsRecursive,
);
await testVaultsUtils.expectDirExistsNot(vault, dirPath);
});
test('creating existing directory should fail', async () => {
await testVaultsUtils.mkdir(vault, dirName);
const response = await vaultOps.mkdir(vault, dirName);
expect(response.type).toEqual('error');
const error = response as ErrorMessage;
expect(error.code).toEqual('EEXIST');
await expect(vaultOps.mkdir(vault, dirName)).rejects.toThrow(
vaultsErrors.ErrorSecretsSecretDefined,
);
await testVaultsUtils.expectDirExists(vault, dirName);
});
test('creating existing secret should fail', async () => {
await testVaultsUtils.writeSecret(vault, secretName, secretContent);
const response = await vaultOps.mkdir(vault, secretName);
expect(response.type).toEqual('error');
const error = response as ErrorMessage;
expect(error.code).toEqual('EEXIST');
await expect(vaultOps.mkdir(vault, secretName)).rejects.toThrow(
vaultsErrors.ErrorSecretsSecretDefined,
);
await testVaultsUtils.expectSecret(vault, secretName, secretContent);
});
test('can create a hidden directory', async () => {
const response = await vaultOps.mkdir(vault, dirNameHidden);
expect(response.type).toEqual('success');
await vaultOps.mkdir(vault, dirNameHidden);
await testVaultsUtils.expectDirExists(vault, dirNameHidden);
});
});

0 comments on commit 995e1b3

Please sign in to comment.