Skip to content

Commit

Permalink
chore: updated tests and added new kinds of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Aug 26, 2024
1 parent 2787ac0 commit cb67eda
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
6 changes: 0 additions & 6 deletions src/client/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ class ErrorClientAuthDenied<T> extends ErrorClient<T> {
exitCode = sysexits.NOPERM;
}

class ErrorClientFSReadFailed<T> extends ErrorClient<T> {
static description = 'Failed to read from filesystem';
exitCode = sysexits.IOERR;
}

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

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

class VaultsSecretsList extends ServerHandler<
{
Expand Down Expand Up @@ -42,16 +41,20 @@ class VaultsSecretsList extends ServerHandler<
void,
void
> {
let files;
let files: Array<string | Buffer>;
try {
files = await fs.promises.readdir(input.secretName);
} catch (e) {
// throw new clientErrors.ErrorClientFSReadFailed(e.message, {
// cause: e,
// });
// const er = new Error('Message of error');
// console.error('error from handler', er);
// throw er;
if (e.code === 'ENOENT') {
throw new vaultsErrors.ErrorSecretsDirectoryUndefined(e.message, {
cause: e,
});
}
if (e.code === 'ENOTDIR') {
throw new vaultsErrors.ErrorSecretsDirectoryUndefined(e.message, {
cause: e,
});
}
throw e;
}
for await (const file of files) {
Expand Down
6 changes: 6 additions & 0 deletions src/vaults/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ class ErrorSecretsIsDirectory<T> extends ErrorSecrets<T> {
exitCode = sysexits.USAGE;
}

class ErrorSecretsDirectoryUndefined<T> extends ErrorSecrets<T> {
static description = 'Directory does not exist';
exitCode = sysexits.USAGE;
}

export {
ErrorVaults,
ErrorVaultManagerRunning,
Expand All @@ -144,4 +149,5 @@ export {
ErrorSecretsSecretUndefined,
ErrorSecretsSecretDefined,
ErrorSecretsIsDirectory,
ErrorSecretsDirectoryUndefined,
};
16 changes: 5 additions & 11 deletions tests/client/handlers/vaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import Logger, { formatting, LogLevel, StreamHandler } from '@matrixai/logger';
import { DB } from '@matrixai/db';
import { RPCClient } from '@matrixai/rpc';
import { WebSocketClient } from '@matrixai/ws';
import { ErrorEncryptedFSError } from 'encryptedfs/dist/errors';
import TaskManager from '@/tasks/TaskManager';
import ACL from '@/acl/ACL';
import KeyRing from '@/keys/KeyRing';
Expand Down Expand Up @@ -1591,21 +1590,16 @@ describe('vaultsSecretsNewDir and vaultsSecretsList', () => {

const noFiles = await rpcClient.methods.vaultsSecretsList({
nameOrId: vaultsIdEncoded,
secretName: 'alsdfjpaosdifhpaos',
secretName: 'doesntExist',
});

console.log('before expect')
await expect((async () => {
await expect(async () => {
try {
for await (const f of noFiles) {
console.log(`file: ${f}`);
};
for await (const _ of noFiles);
} catch (e) {
console.error('error from test', e);
throw e;
throw e.cause;
}
})()).rejects.toThrow(Error);
console.log('after expect')
}).rejects.toThrow(vaultsErrors.ErrorSecretsDirectoryUndefined);

const secrets = await rpcClient.methods.vaultsSecretsList({
nameOrId: vaultsIdEncoded,
Expand Down

0 comments on commit cb67eda

Please sign in to comment.