Skip to content

Commit

Permalink
chore: added error handling for invalid patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Aug 20, 2024
1 parent a5fe659 commit e0f062d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/client/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class ErrorClientAuthDenied<T> extends ErrorClient<T> {
exitCode = sysexits.NOPERM;
}

class ErrorClientFileReadFailed<T> extends ErrorClient<T> {
static description = 'Failed to read file or directory';
exitCode = sysexits.IOERR;
}

class ErrorClientService<T> extends ErrorClient<T> {}

class ErrorClientServiceRunning<T> extends ErrorClientService<T> {
Expand Down Expand Up @@ -45,6 +50,7 @@ export {
ErrorClientAuthMissing,
ErrorClientAuthFormat,
ErrorClientAuthDenied,
ErrorClientFileReadFailed,
ErrorClientService,
ErrorClientServiceRunning,
ErrorClientServiceNotRunning,
Expand Down
37 changes: 26 additions & 11 deletions src/client/handlers/VaultsSecretsGetFileTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ServerHandler } from '@matrixai/rpc';
import { generateStats } from '../../vaults/fileTree';
import * as vaultsUtils from '../../vaults/utils';
import * as vaultsErrors from '../../vaults/errors';
import * as clientErrors from '../errors';

class VaultsSecretsGetFileTree extends ServerHandler<
{
Expand Down Expand Up @@ -38,18 +39,32 @@ class VaultsSecretsGetFileTree extends ServerHandler<
void,
void
> {
// @ts-ignore: While the types don't fully match, it matches enough for our usage.
let files: Array<string> = await fs.promises.readdir(input.pattern);
files = files.map((file) => path.join(input.pattern, file));
try {
// @ts-ignore: While the types don't fully match, it matches enough for our usage.
let files: Array<string> = await fs.promises.readdir(input.pattern);
files = files.map((file) => path.join(input.pattern, file));

for await (const file of files) {
const stat = await fs.promises.stat(file);
const type = stat.isFile() ? 'FILE' : 'DIRECTORY';
yield {
path: file,
type: type,
stat: input.yieldStats ? generateStats(stat) : undefined,
};
for await (const file of files) {
try {
const stat = await fs.promises.stat(file);
const type = stat.isFile() ? 'FILE' : 'DIRECTORY';
yield {
path: file,
type: type,
stat: input.yieldStats ? generateStats(stat) : undefined,
};
} catch (e) {
throw new clientErrors.ErrorClientFileReadFailed(
`Failed to read file: ${file}`,
{ cause: e },
);
}
}
} catch (e) {
throw new clientErrors.ErrorClientFileReadFailed(
`Failed to read directory: ${input.pattern}`,
{ cause: e },
);
}
});
});
Expand Down
15 changes: 15 additions & 0 deletions tests/client/handlers/vaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import * as nodesUtils from '@/nodes/utils';
import * as vaultsUtils from '@/vaults/utils';
import * as vaultsErrors from '@/vaults/errors';
import * as networkUtils from '@/network/utils';
import * as clientErrors from '@/client/errors';
import * as testsUtils from '../../utils';

describe('vaultsClone', () => {
Expand Down Expand Up @@ -1588,6 +1589,20 @@ describe('vaultsSecretsNewDir and vaultsSecretsList', () => {
});
expect(addResponse.success).toBeTruthy();

expect(async () => {
const files = await rpcClient.methods.vaultsSecretsGetFileTree({
nameOrId: vaultsIdEncoded,
pattern: 'doesntExist',
yieldStats: false,
});
try {
for await (const _ of files); // Consume values
}
catch (e) {
throw e.cause;
}
}).rejects.toThrow(clientErrors.ErrorClientFileReadFailed);

// List secrets with names of directories
const secrets = await rpcClient.methods.vaultsSecretsGetFileTree({
nameOrId: vaultsIdEncoded,
Expand Down

0 comments on commit e0f062d

Please sign in to comment.