Skip to content

Commit

Permalink
feat: static descriptions and exit codes for all errors
Browse files Browse the repository at this point in the history
The `data` POJO that was originally supplied in the constructor as the second parameter is now part of the `options` parameter and most places in the code have been updated to match this.

#304
  • Loading branch information
emmacasolin committed Jun 6, 2022
1 parent 620f8c4 commit bc8a3f7
Show file tree
Hide file tree
Showing 45 changed files with 1,052 additions and 622 deletions.
14 changes: 5 additions & 9 deletions src/ErrorPolykey.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import type { POJO } from './types';
import { CustomError } from 'ts-custom-error';
import { AbstractError } from '@matrixai/errors';
import sysexits from './utils/sysexits';

class ErrorPolykey extends CustomError {
data: POJO;
description: string = 'Polykey error';
class ErrorPolykey<T> extends AbstractError<T> {
static description: string = 'Polykey error';
exitCode: number = sysexits.GENERAL;
constructor(message: string = '', data: POJO = {}) {
super(message);
this.data = data;
}
toJSON(): string {
return JSON.stringify({
name: this.name,
description: this.description,
message: this.message,
exitCode: this.exitCode,
timestamp: this.timestamp,
data: this.data,
cause: this.cause,
stack: this.stack,
});
}
Expand Down
39 changes: 19 additions & 20 deletions src/acl/ACL.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import type {
DB,
DBTransaction,
KeyPath,
LevelPath
} from '@matrixai/db';
import type { DB, DBTransaction, KeyPath, LevelPath } from '@matrixai/db';
import type {
PermissionId,
PermissionIdString,
Expand All @@ -18,15 +13,14 @@ import type { Ref } from '../types';

import Logger from '@matrixai/logger';
import { IdInternal } from '@matrixai/id';
import { RWLockWriter } from '@matrixai/async-locks';
import {
CreateDestroyStartStop,
ready,
} from '@matrixai/async-init/dist/CreateDestroyStartStop';
import * as aclUtils from './utils';
import * as aclErrors from './errors';
import { utils as dbUtils } from '@matrixai/db';
import { withF } from '@matrixai/resources';
import * as aclUtils from './utils';
import * as aclErrors from './errors';

interface ACL extends CreateDestroyStartStop {}
@CreateDestroyStartStop(
Expand All @@ -36,17 +30,15 @@ interface ACL extends CreateDestroyStartStop {}
class ACL {
static async createACL({
db,
locks,
logger = new Logger(this.name),
fresh = false,
}: {
db: DB;
locks: Locks;
logger?: Logger;
fresh?: boolean;
}): Promise<ACL> {
logger.info(`Creating ${this.name}`);
const acl = new ACL({ db, locks, logger });
const acl = new ACL({ db, logger });
await acl.start({ fresh });
logger.info(`Created ${this.name}`);
return acl;
Expand All @@ -72,7 +64,7 @@ class ACL {
*/
protected aclVaultsDbPath: LevelPath = [this.constructor.name, 'vaults'];

// lock across the usages of the DB
// Lock across the usages of the DB
// it makes sense to use
// Symbol.for("key")
// symbol for is found in teh global registry, if it doesn't exist, it is added to the global registry and returned
Expand All @@ -99,10 +91,9 @@ class ACL {

protected generatePermId: () => PermissionId;

constructor({ db, locks, logger }: { db: DB; locks: Locks; logger: Logger }) {
constructor({ db, logger }: { db: DB; logger: Logger }) {
this.logger = logger;
this.db = db;
this.locks = locks;
}

public async start({
Expand Down Expand Up @@ -135,24 +126,32 @@ class ACL {
nodeId2: NodeId,
tran?: DBTransaction,
): Promise<boolean> {
const nodeId1Path = [...this.aclNodesDbPath, nodeId1.toBuffer()] as unknown as KeyPath;
const nodeId2Path = [...this.aclNodesDbPath, nodeId2.toBuffer()] as unknown as KeyPath;
const nodeId1Path = [
...this.aclNodesDbPath,
nodeId1.toBuffer(),
] as unknown as KeyPath;
const nodeId2Path = [
...this.aclNodesDbPath,
nodeId2.toBuffer(),
] as unknown as KeyPath;
if (tran == null) {
return withF(
[
this.db.transaction(),
this.locks.lockRead(
dbUtils.keyPathToKey(nodeId1Path).toString('binary'),
dbUtils.keyPathToKey(nodeId2Path).toString('binary')
dbUtils.keyPathToKey(nodeId2Path).toString('binary'),
),
],
async ([tran]) => this.sameNodePerm(nodeId1, nodeId2, tran)
async ([tran]) => this.sameNodePerm(nodeId1, nodeId2, tran),
);
}
const permId1 = await tran.get(nodeId1Path, true);
const permId2 = await tran.get(nodeId2Path, true);
if (permId1 != null && permId2 != null) {
return IdInternal.fromBuffer(permId1).equals(IdInternal.fromBuffer(permId2));
return IdInternal.fromBuffer(permId1).equals(
IdInternal.fromBuffer(permId2),
);
}
return false;
}
Expand Down
48 changes: 33 additions & 15 deletions src/acl/errors.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
import { ErrorPolykey } from '../errors';

class ErrorACL extends ErrorPolykey {}

class ErrorACLRunning extends ErrorACL {}

class ErrorACLNotRunning extends ErrorACL {}

class ErrorACLDestroyed extends ErrorACL {}

class ErrorACLNodeIdMissing extends ErrorACL {}

class ErrorACLVaultIdMissing extends ErrorACL {}

class ErrorACLNodeIdExists extends ErrorACL {}
import { ErrorPolykey, sysexits } from '../errors';

class ErrorACL<T> extends ErrorPolykey<T> {}

class ErrorACLRunning<T> extends ErrorACL<T> {
static description = 'ACL is running';
exitCode = sysexits.USAGE;
}

class ErrorACLNotRunning<T> extends ErrorACL<T> {
static description = 'ACL is not running';
exitCode = sysexits.USAGE;
}

class ErrorACLDestroyed<T> extends ErrorACL<T> {
static description = 'ACL is destroyed';
exitCode = sysexits.USAGE;
}

class ErrorACLNodeIdMissing<T> extends ErrorACL<T> {
static description = 'Could not find NodeId';
exitCode = sysexits.NOUSER;
}

class ErrorACLVaultIdMissing<T> extends ErrorACL<T> {
static description = 'Could not find VaultId';
exitCode = sysexits.DATAERR;
}

class ErrorACLNodeIdExists<T> extends ErrorACL<T> {
static description = 'NodeId already exists';
exitCode = sysexits.DATAERR;
}

export {
ErrorACL,
Expand Down
21 changes: 15 additions & 6 deletions src/agent/errors.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import { ErrorPolykey, sysexits } from '../errors';

class ErrorAgent extends ErrorPolykey {}
class ErrorAgent<T> extends ErrorPolykey<T> {}

class ErrorAgentRunning extends ErrorPolykey {}
class ErrorAgentRunning<T> extends ErrorPolykey<T> {
static description = 'Agent Client is running';
exitCode = sysexits.USAGE;
}

class ErrorAgentClientNotStarted extends ErrorAgent {}
class ErrorAgentClientNotStarted<T> extends ErrorAgent<T> {
static description = 'Agent Client is not started';
exitCode = sysexits.USAGE;
}

class ErrorAgentClientDestroyed extends ErrorAgent {}
class ErrorAgentClientDestroyed<T> extends ErrorAgent<T> {
static description = 'Agent Client is destroyed';
exitCode = sysexits.USAGE;
}

class ErrorConnectionInfoMissing extends ErrorAgent {
description = 'Vault already exists';
class ErrorConnectionInfoMissing<T> extends ErrorAgent<T> {
static description = 'Vault already exists';
exitCode = sysexits.UNAVAILABLE;
}

Expand Down
6 changes: 4 additions & 2 deletions src/bin/agent/CommandStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,10 @@ class CommandStart extends CommandPolykey {
new binErrors.ErrorCLIPolykeyAgentProcess(
'Agent process closed during fork',
{
code,
signal,
data: {
code,
signal,
},
},
),
);
Expand Down
42 changes: 21 additions & 21 deletions src/bin/errors.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
import ErrorPolykey from '../ErrorPolykey';
import sysexits from '../utils/sysexits';

class ErrorCLI extends ErrorPolykey {}
class ErrorCLI<T> extends ErrorPolykey<T> {}

class ErrorCLINodePath extends ErrorCLI {
description = 'Cannot derive default node path from unknown platform';
class ErrorCLINodePath<T> extends ErrorCLI<T> {
static description = 'Cannot derive default node path from unknown platform';
exitCode = sysexits.USAGE;
}

class ErrorCLIClientOptions extends ErrorCLI {
description = 'Missing required client options';
class ErrorCLIClientOptions<T> extends ErrorCLI<T> {
static description = 'Missing required client options';
exitCode = sysexits.USAGE;
}

class ErrorCLIPasswordMissing extends ErrorCLI {
description =
class ErrorCLIPasswordMissing<T> extends ErrorCLI<T> {
static description =
'Password is necessary, provide it via --password-file, PK_PASSWORD or when prompted';
exitCode = sysexits.USAGE;
}

class ErrorCLIPasswordFileRead extends ErrorCLI {
description = 'Failed to read password file';
class ErrorCLIPasswordFileRead<T> extends ErrorCLI<T> {
static description = 'Failed to read password file';
exitCode = sysexits.NOINPUT;
}

class ErrorCLIRecoveryCodeFileRead extends ErrorCLI {
description = 'Failed to read recovery code file';
class ErrorCLIRecoveryCodeFileRead<T> extends ErrorCLI<T> {
static description = 'Failed to read recovery code file';
exitCode = sysexits.NOINPUT;
}

class ErrorCLIFileRead extends ErrorCLI {
description = 'Failed to read file';
class ErrorCLIFileRead<T> extends ErrorCLI<T> {
static description = 'Failed to read file';
exitCode = sysexits.NOINPUT;
}

class ErrorCLIPolykeyAgentStatus extends ErrorCLI {
description = 'PolykeyAgent agent status';
class ErrorCLIPolykeyAgentStatus<T> extends ErrorCLI<T> {
static description = 'PolykeyAgent agent status';
exitCode = sysexits.TEMPFAIL;
}

class ErrorCLIPolykeyAgentProcess extends ErrorCLI {
description = 'PolykeyAgent process could not be started';
class ErrorCLIPolykeyAgentProcess<T> extends ErrorCLI<T> {
static description = 'PolykeyAgent process could not be started';
exitCode = sysexits.OSERR;
}

class ErrorNodeFindFailed extends ErrorCLI {
description = 'Failed to find the node in the DHT';
class ErrorNodeFindFailed<T> extends ErrorCLI<T> {
static description = 'Failed to find the node in the DHT';
exitCode = 1;
}

class ErrorNodePingFailed extends ErrorCLI {
description = 'Node was not online or not found.';
class ErrorNodePingFailed<T> extends ErrorCLI<T> {
static description = 'Node was not online or not found.';
exitCode = 1;
}

Expand Down
10 changes: 6 additions & 4 deletions src/bin/keys/CommandDecrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ class CommandDecrypt extends CommandPolykey {
});
} catch (e) {
throw new binErrors.ErrorCLIFileRead(e.message, {
errno: e.errno,
syscall: e.syscall,
code: e.code,
path: e.path,
data: {
errno: e.errno,
syscall: e.syscall,
code: e.code,
path: e.path,
},
});
}
cryptoMessage.setData(cipherText);
Expand Down
10 changes: 6 additions & 4 deletions src/bin/keys/CommandEncrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ class CommandEncypt extends CommandPolykey {
});
} catch (e) {
throw new binErrors.ErrorCLIFileRead(e.message, {
errno: e.errno,
syscall: e.syscall,
code: e.code,
path: e.path,
data: {
errno: e.errno,
syscall: e.syscall,
code: e.code,
path: e.path,
},
});
}
cryptoMessage.setData(plainText);
Expand Down
10 changes: 6 additions & 4 deletions src/bin/keys/CommandSign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ class CommandSign extends CommandPolykey {
});
} catch (e) {
throw new binErrors.ErrorCLIFileRead(e.message, {
errno: e.errno,
syscall: e.syscall,
code: e.code,
path: e.path,
data: {
errno: e.errno,
syscall: e.syscall,
code: e.code,
path: e.path,
},
});
}
cryptoMessage.setData(data);
Expand Down
10 changes: 6 additions & 4 deletions src/bin/keys/CommandVerify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ class CommandVerify extends CommandPolykey {
});
} catch (e) {
throw new binErrors.ErrorCLIFileRead(e.message, {
errno: e.errno,
syscall: e.syscall,
code: e.code,
path: e.path,
data: {
errno: e.errno,
syscall: e.syscall,
code: e.code,
path: e.path,
},
});
}
cryptoMessage.setData(data);
Expand Down
2 changes: 1 addition & 1 deletion src/bin/polykey-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function main(_argv = process.argv): Promise<number> {
const processSend = promisify<void>(process.send!.bind(process));
const { p: messageInP, resolveP: resolveMessageInP } =
promise<AgentChildProcessInput>();
process.once('message', (data) => {
process.once('message', (data: AgentChildProcessInput) => {
resolveMessageInP(data);
});
const messageIn = await messageInP;
Expand Down
Loading

0 comments on commit bc8a3f7

Please sign in to comment.