Skip to content

Commit

Permalink
WIP - #340 - error descriptions updated
Browse files Browse the repository at this point in the history
  • Loading branch information
tegefaulkes committed Feb 21, 2022
1 parent acdb772 commit 4724a7f
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 35 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"@grpc/grpc-js": "1.3.7",
"@matrixai/async-init": "^1.6.0",
"@matrixai/db": "^1.1.5",
"@matrixai/id": "^3.3.0",
"@matrixai/id": "^3.3.1",
"@matrixai/logger": "^2.1.0",
"@matrixai/workers": "^1.2.5",
"ajv": "^7.0.4",
Expand Down
64 changes: 64 additions & 0 deletions src/utils/sysexits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,84 @@ const sysexits = Object.freeze({
OK: 0,
GENERAL: 1,
// Sysexit standard starts at 64 to avoid conflicts
/**
* The command was used incorrectly, e.g., with the wrong number of arguments,
* a bad flag, a bad syntax in a parameter, or whatever.
*/
USAGE: 64,
/**
* The input data was incorrect in some way. This should only be used for
* user's data and not system files.
*/
DATAERR: 65,
/**
* An input file (not a system file) did not exist or was not readable.
* This could also include errors like "No message" to a mailer
* (if it cared to catch it).
*/
NOINPUT: 66,
/**
* The user specified did not exist. This might be used for mail addresses
* or remote logins.
*/
NOUSER: 67,
/**
* The host specified did not exist. This is used in mail addresses or
* network requests.
*/
NOHOST: 68,
/**
* A service is unavailable. This can occur if a support program or file
* does not exist. This can also be used as a catchall message when
* something you wanted to do does not work, but you do not know why.
*/
UNAVAILABLE: 69,
/**
* An internal software error has been detected. This should be limited to
* non-operating system related errors as possible.
*/
SOFTWARE: 70,
/**
* An operating system error has been detected. This is intended to be used
* for such things as "cannot fork", "cannot create pipe", or the like.
* It in-cludes things like getuid returning a user that does not exist in
* the passwd file.
*/
OSERR: 71,
/**
* Some system file (e.g., /etc/passwd, /var/run/utx.active, etc.)
* does not exist, cannot be opened, or has some sort of error
* (e.g., syntax error).
*/
OSFILE: 72,
/**
* A (user specified) output file cannot be created.
*/
CANTCREAT: 73,
/**
* An error occurred while doing I/O on some file.
*/
IOERR: 74,
/**
* Temporary failure, indicating something that is not really an error.
* In sendmail, this means that a mailer (e.g.) could not create a connection,
* and the request should be reattempted later.
*/
TEMPFAIL: 75,
/**
* The remote system returned something that was "not possible" during a
* protocol exchange.
*/
PROTOCOL: 76,
/**
* You did not have sufficient permission to perform the operation. This is
* not intended for file system problems, which should use EX_NOINPUT or
* EX_CANTCREAT, but rather for higher level permissions.
*/
NOPERM: 77,
/**
* Something was found in an un-configured or mis-configured state.
*/
CONFIG: 78,
CANNOT_EXEC: 126,
COMMAND_NOT_FOUND: 127,
Expand Down
4 changes: 1 addition & 3 deletions src/vaults/VaultInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,9 +678,7 @@ class VaultInternal {
if (err instanceof git.Errors.SmartHttpError && error) {
throw error;
} else if (err instanceof git.Errors.MergeNotSupportedError) {
throw new vaultsErrors.ErrorVaultsMergeConflict(
'Merge Conflicts are not supported yet',
);
throw new vaultsErrors.ErrorVaultsMergeConflict();
}
throw err;
}
Expand Down
45 changes: 29 additions & 16 deletions src/vaults/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,47 @@ class ErrorVaultImmutable extends ErrorVaults {
exitCode = sysexits.USAGE;
}

// --- these need to be reviewed

class ErrorVaultsVaultUndefined extends ErrorVaults {
description = 'Vault does not exist';
exitCode = 10;
exitCode = sysexits.USAGE;
}

class ErrorVaultsVaultDefined extends ErrorVaults {}

class ErrorVaultsRecursive extends ErrorVaults {}

class ErrorVaultsVaultUnlinked extends ErrorVaults {}
class ErrorVaultsVaultDefined extends ErrorVaults {
description = 'Vault already exists';
exitCode = sysexits.USAGE;
}

class ErrorVaultsCreateVaultId extends ErrorVaults {}
class ErrorVaultsRecursive extends ErrorVaults {
description = '';
exitCode = sysexits.USAGE;
}

class ErrorVaultsInvalidVaultId extends ErrorVaults {} // TODO: Assign a proper error code and message.
class ErrorVaultsCreateVaultId extends ErrorVaults {
description = 'Failed to create unique VaultId';
exitCode = sysexits.SOFTWARE;
}

class ErrorVaultsMergeConflict extends ErrorVaults {}
class ErrorVaultsMergeConflict extends ErrorVaults {
description = 'Merge Conflicts are not supported yet';
exitCode = sysexits.SOFTWARE;
}

class ErrorVaultsPermissionDenied extends ErrorVaults {}
class ErrorVaultsPermissionDenied extends ErrorVaults {
description = 'Permission was denied';
exitCode = sysexits.NOPERM;
}

class ErrorSecrets extends ErrorPolykey {}

class ErrorSecretsSecretUndefined extends ErrorSecrets {}
class ErrorSecretsSecretUndefined extends ErrorSecrets {
description = 'Secret does not exist';
exitCode = sysexits.USAGE;
}

class ErrorSecretsSecretDefined extends ErrorSecrets {}
class ErrorSecretsSecretDefined extends ErrorSecrets {
description = 'Secret already exists';
exitCode = sysexits.USAGE;
}

export {
ErrorVaults,
Expand All @@ -106,9 +121,7 @@ export {
ErrorVaultsVaultUndefined,
ErrorVaultsVaultDefined,
ErrorVaultsRecursive,
ErrorVaultsVaultUnlinked,
ErrorVaultsCreateVaultId,
ErrorVaultsInvalidVaultId,
ErrorVaultsMergeConflict,
ErrorVaultsPermissionDenied,
ErrorSecrets,
Expand Down
14 changes: 6 additions & 8 deletions src/vaults/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ function encodeVaultId(vaultId: VaultId): VaultIdEncoded {
}

function decodeVaultId(vaultIdEncoded: any): VaultId | undefined {
if (typeof vaultIdEncoded !== 'string') {
return;
}
if (typeof vaultIdEncoded !== 'string') return;
const vaultId = IdInternal.fromMultibase<VaultId>(vaultIdEncoded);
if (vaultId == null) {
return;
}
if (vaultId == null) return;
// All VaultIds are 16 bytes long
if (vaultId.length !== 16) return;
return vaultId;
}

Expand Down Expand Up @@ -74,8 +72,8 @@ function commitAuthor(nodeId: NodeId): { name: string; email: string } {
// }

// TODO: remove or move?
async function* readdirRecursively(fs, dir = '.') {
throw Error('Not Implemented');
async function* readdirRecursively(fs, _dir = '.') {
yield Error('Not Implemented');
// Const dirents = await fs.promises.readdir(dir);
// for (const dirent of dirents) {
// const res = path.join(dir, dirent.toString());
Expand Down
16 changes: 12 additions & 4 deletions tests/vaults/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { VaultId } from '@/vaults/types';
import fs from 'fs';
import os from 'os';
import path from 'path';
Expand Down Expand Up @@ -52,7 +53,6 @@ describe('Vaults utils', () => {
}
expect(files.sort()).toStrictEqual([filePath1, filePath2].sort());
});

test('fs can be read recursively', async () => {
await fs.promises.mkdir(path.join(dataDir, 'dir'), { recursive: true });
await fs.promises.mkdir(path.join(dataDir, 'dir', 'dir2', 'dir3'), {
Expand All @@ -73,8 +73,16 @@ describe('Vaults utils', () => {
}
expect(files.sort()).toStrictEqual([filePath1, filePath2].sort());
});
test('makeVaultId converts a buffer', async () => {
const randomIdGen = new IdRandom();
Buffer.from(randomIdGen.get());
test('decodeNodeId does not throw an error', async () => {
const randomIdGen = new IdRandom<VaultId>();
const vaultId = randomIdGen.get();
const vaultIdEncoded = vaultsUtils.encodeVaultId(vaultId);

expect(vaultsUtils.decodeVaultId(vaultIdEncoded)).toBeDefined();
expect(vaultsUtils.decodeVaultId('invalidVaultIdEncoded')).toBeUndefined();
expect(
vaultsUtils.decodeVaultId('zF4VfF3uRhSqgxTOOLONGxTRdVKauV9'),
).toBeUndefined();
expect(vaultsUtils.decodeVaultId('zF4VfxTOOSHORTxTV9')).toBeUndefined();
});
});

0 comments on commit 4724a7f

Please sign in to comment.