Skip to content

Commit

Permalink
chore: added error type for mkdir
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Oct 8, 2024
1 parent 488b565 commit deef86e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
26 changes: 14 additions & 12 deletions src/client/handlers/VaultsSecretsMkdir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
SecretDirMessage,
SuccessWithErrorMessage,
SuccessOrErrorMessage,
} from '../types';
import type VaultManager from '../../vaults/VaultManager';
import { DuplexHandler } from '@matrixai/rpc';
Expand All @@ -18,16 +18,16 @@ class VaultsSecretsMkdir extends DuplexHandler<
db: DB;
},
ClientRPCRequestParams<SecretDirMessage>,
ClientRPCResponseResult<SuccessWithErrorMessage>
ClientRPCResponseResult<SuccessOrErrorMessage>
> {
public handle = async function* (
input: AsyncIterable<ClientRPCRequestParams<SecretDirMessage>>,
): AsyncGenerator<ClientRPCResponseResult<SuccessWithErrorMessage>> {
): AsyncGenerator<ClientRPCResponseResult<SuccessOrErrorMessage>> {
const { vaultManager, db }: { vaultManager: VaultManager; db: DB } =
this.container;
let metadata: JSONObject;
yield* db.withTransactionG(
async function* (tran): AsyncGenerator<SuccessWithErrorMessage> {
async function* (tran): AsyncGenerator<SuccessOrErrorMessage> {
for await (const secretDirMessage of input) {
// Unpack input
if (metadata == null) metadata = secretDirMessage.metadata ?? {};
Expand All @@ -41,20 +41,22 @@ class VaultsSecretsMkdir extends DuplexHandler<
throw new vaultsErrors.ErrorVaultsVaultUndefined();
}
// Write directories. This doesn't need to be grouped by vault names,
// as no commit is created for empty directories anyways.
let response: SuccessWithErrorMessage;
await vaultManager.withVaults(
// as no commit is created for empty directories anyways. The
// vaultOps.mkdir() method also returns an object of type
// SuccessOrErrorMessage. As such, we can return the result without
// doing any type conversion or extra processing.
yield await vaultManager.withVaults(
[vaultId],
async (vault) => {
const options = metadata?.options as { recursive?: boolean };
response = await vaultOps.mkdir(vault, dirName, {
recursive: options.recursive,
const options = metadata?.options as
| { recursive?: boolean }
| undefined;
return await vaultOps.mkdir(vault, dirName, {
recursive: options?.recursive,
});
},
tran,
);
if (response!.success) yield { success: true };
else yield { success: false, error: response!.error };
}
},
);
Expand Down
17 changes: 12 additions & 5 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,16 @@ type SuccessMessage = {
success: boolean;
};

type ErrorMessage = {
type: 'error';
success: false;
code: string;
reason?: string;
data?: JSONObject;
};

type SuccessOrErrorMessage = SuccessMessage | ErrorMessage;

// Notifications messages

type NotificationReadMessage = {
Expand Down Expand Up @@ -321,10 +331,6 @@ type SecretDirMessage = VaultIdentifierMessage & {
dirName: string;
};

type SuccessWithErrorMessage = SuccessMessage & {
error?: string;
};

type SecretRenameMessage = SecretIdentifierMessage & {
newSecretName: string;
};
Expand Down Expand Up @@ -397,6 +403,8 @@ export type {
NodesGetMessage,
NodesAddMessage,
SuccessMessage,
ErrorMessage,
SuccessOrErrorMessage,
NotificationInboxMessage,
NotificationOutboxMessage,
NotificationReadMessage,
Expand All @@ -423,7 +431,6 @@ export type {
ContentWithErrorMessage,
SecretContentMessage,
SecretDirMessage,
SuccessWithErrorMessage,
SecretRenameMessage,
SecretFilesMessage,
SecretStatMessage,
Expand Down
12 changes: 8 additions & 4 deletions src/vaults/VaultOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import type Logger from '@matrixai/logger';
import type { Vault } from './Vault';
import type { Stat } from 'encryptedfs';
import type { SuccessWithErrorMessage } from '../client/types';
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 +174,7 @@ async function mkdir(
dirPath: string,
fileOptions?: FileOptions,
logger?: Logger,
): Promise<SuccessWithErrorMessage> {
): Promise<SuccessOrErrorMessage> {
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 @@ -189,13 +189,17 @@ async function mkdir(
logger?.error(`Failed to create directory '${dirPath}'. Reason: ${e.code}`);
if (e.code === 'ENOENT' && !recursive) {
return {
type: 'error',
success: false,
error: `${e.code}: cannot create directory ${dirPath}: No such secret or directory`,
code: e.code,
reason: dirPath,
};
} else if (e.code === 'EEXIST') {
return {
type: 'error',
success: false,
error: `${e.code}: cannot create directory ${dirPath}: Secret or directory exists`,
code: e.code,
reason: dirPath,
};
} else {
throw e;
Expand Down
19 changes: 11 additions & 8 deletions tests/client/handlers/vaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { FileSystem } from '@/types';
import type { VaultId } from '@/ids';
import type NodeManager from '@/nodes/NodeManager';
import type {
ErrorMessage,
LogEntryMessage,
SecretContentMessage,
VaultListMessage,
Expand Down Expand Up @@ -1352,7 +1353,6 @@ describe('vaultsSecretsMkdir', () => {

for await (const data of response.readable) {
expect(data.success).toBeTruthy();
expect(data.error).toBeUndefined();
}
await vaultManager.withVaults([vaultId], async (vault) => {
await vault.readF(async (efs) => {
Expand All @@ -1371,7 +1371,9 @@ describe('vaultsSecretsMkdir', () => {
await writer.close();
for await (const data of response.readable) {
expect(data.success).toBeFalsy();
expect(data.error?.substring(0, 6)).toStrictEqual('ENOENT');
const error = data as ErrorMessage;
expect(error.code).toEqual('ENOENT');
expect(error.reason).toEqual(dirPath);
}
await vaultManager.withVaults([vaultId], async (vault) => {
await vault.readF(async (efs) => {
Expand Down Expand Up @@ -1403,7 +1405,6 @@ describe('vaultsSecretsMkdir', () => {
// Check if the operation concluded as expected
for await (const data of response.readable) {
expect(data.success).toBeTruthy();
expect(data.error).toBeUndefined();
}
await vaultManager.withVaults(
[vaultId1, vaultId2],
Expand Down Expand Up @@ -1437,10 +1438,10 @@ describe('vaultsSecretsMkdir', () => {
await writer.close();
// Check if the operation concluded as expected
for await (const data of response.readable) {
if (data.success) {
expect(data.error).toBeUndefined();
} else {
expect(data.error?.substring(0, 6)).toStrictEqual('ENOENT');
if (!data.success) {
const error = data as ErrorMessage;
expect(error.code).toEqual('ENOENT');
expect(error.reason).toEqual(dirPath3);
}
}
await vaultManager.withVaults(
Expand Down Expand Up @@ -1474,7 +1475,9 @@ describe('vaultsSecretsMkdir', () => {
// Check if the operation concluded as expected
for await (const data of response.readable) {
expect(data.success).toBeFalsy();
expect(data.error?.substring(0, 6)).toStrictEqual('EEXIST');
const error = data as ErrorMessage;
expect(error.code).toEqual('EEXIST');
expect(error.reason).toEqual(dirPath);
}
await vaultManager.withVaults([vaultId], async (vault) => {
await vault.readF(async (efs) => {
Expand Down

0 comments on commit deef86e

Please sign in to comment.