Skip to content

Commit

Permalink
feat: added logger to service handlers
Browse files Browse the repository at this point in the history
All of the agent and client service handlers are now passed a shared logger that is used to log errors to stderr.

#304
  • Loading branch information
emmacasolin committed Jun 6, 2022
1 parent 477d111 commit 0538ef9
Show file tree
Hide file tree
Showing 127 changed files with 666 additions and 316 deletions.
18 changes: 9 additions & 9 deletions src/ErrorPolykey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ class ErrorPolykey<T> extends AbstractError<T> {
_key: string = '',
options: {
description?: boolean;
message?: boolean,
exitCode?: boolean,
message?: boolean;
exitCode?: boolean;
timestamp?: boolean;
data?: boolean;
cause?: boolean;
stack?: boolean;
} = {}
} = {},
): {
type: string;
data: {
description?: string;
message?: string;
exitCode?: number,
timestamp?: Date,
exitCode?: number;
timestamp?: Date;
data?: POJO;
cause?: T,
stack?: string
}
cause?: T;
stack?: string;
};
} {
options.description ??= true;
options.message ??= true;
Expand Down Expand Up @@ -54,7 +54,7 @@ class ErrorPolykey<T> extends AbstractError<T> {
if (options.stack) data.stack = this.stack;
return {
type: this.name,
data
data,
};
}
}
Expand Down
55 changes: 30 additions & 25 deletions src/agent/service/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import type { KeyManager } from '../../keys';
import type { VaultManager } from '../../vaults';
import type {
NodeGraph,
NodeManager,
NodeConnectionManager,
} from '../../nodes';
import type { NotificationsManager } from '../../notifications';
import type { Sigchain } from '../../sigchain';
import type { ACL } from '../../acl';
import type { GestaltGraph } from '../../gestalts';
import type KeyManager from '../../keys/KeyManager';
import type VaultManager from '../../vaults/VaultManager';
import type NodeGraph from '../../nodes/NodeGraph';
import type NodeManager from '../../nodes/NodeManager';
import type NodeConnectionManager from '../../nodes/NodeConnectionManager';
import type NotificationsManager from '../../notifications/NotificationsManager';
import type Sigchain from '../../sigchain/Sigchain';
import type ACL from '../../acl/ACL';
import type GestaltGraph from '../../gestalts/GestaltGraph';
import type { IAgentServiceServer } from '../../proto/js/polykey/v1/agent_service_grpc_pb';
import type Proxy from '../../network/Proxy';
import Logger from '@matrixai/logger';
import echo from './echo';
import nodesChainDataGet from './nodesChainDataGet';
import nodesClaimsGet from './nodesClaimsGet';
Expand All @@ -24,7 +23,11 @@ import vaultsScan from './vaultsScan';
import { AgentServiceService } from '../../proto/js/polykey/v1/agent_service_grpc_pb';
import * as agentUtils from '../utils';

function createService(container: {
function createService({
proxy,
logger = new Logger(createService.name),
...containerRest
}: {
keyManager: KeyManager;
vaultManager: VaultManager;
nodeConnectionManager: NodeConnectionManager;
Expand All @@ -35,23 +38,25 @@ function createService(container: {
acl: ACL;
gestaltGraph: GestaltGraph;
proxy: Proxy;
logger?: Logger;
}): IAgentServiceServer {
const connectionInfoGet = agentUtils.connectionInfoGetter(container.proxy);
const container_ = {
...container,
const connectionInfoGet = agentUtils.connectionInfoGetter(proxy);
const container = {
...containerRest,
logger,
connectionInfoGet: connectionInfoGet,
};
const service: IAgentServiceServer = {
echo: echo(container_),
nodesChainDataGet: nodesChainDataGet(container_),
nodesClaimsGet: nodesClaimsGet(container_),
nodesClosestLocalNodesGet: nodesClosestLocalNodesGet(container_),
nodesCrossSignClaim: nodesCrossSignClaim(container_),
nodesHolePunchMessageSend: nodesHolePunchMessageSend(container_),
notificationsSend: notificationsSend(container_),
vaultsGitInfoGet: vaultsGitInfoGet(container_),
vaultsGitPackGet: vaultsGitPackGet(container_),
vaultsScan: vaultsScan(container_),
echo: echo(container),
nodesChainDataGet: nodesChainDataGet(container),
nodesClaimsGet: nodesClaimsGet(container),
nodesClosestLocalNodesGet: nodesClosestLocalNodesGet(container),
nodesCrossSignClaim: nodesCrossSignClaim(container),
nodesHolePunchMessageSend: nodesHolePunchMessageSend(container),
notificationsSend: notificationsSend(container),
vaultsGitInfoGet: vaultsGitInfoGet(container),
vaultsGitPackGet: vaultsGitPackGet(container),
vaultsScan: vaultsScan(container),
};
return service;
}
Expand Down
10 changes: 9 additions & 1 deletion src/agent/service/nodesChainDataGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ import type * as grpc from '@grpc/grpc-js';
import type { Sigchain } from '../../sigchain';
import type * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb';
import type { ClaimIdEncoded } from '../../claims/types';
import type Logger from '@matrixai/logger';
import { utils as grpcUtils } from '../../grpc';
import * as nodesPB from '../../proto/js/polykey/v1/nodes/nodes_pb';

/**
* Retrieves the ChainDataEncoded of this node.
*/
function nodesChainDataGet({ sigchain }: { sigchain: Sigchain }) {
function nodesChainDataGet({
sigchain,
logger,
}: {
sigchain: Sigchain;
logger: Logger;
}) {
return async (
call: grpc.ServerUnaryCall<utilsPB.EmptyMessage, nodesPB.ChainData>,
callback: grpc.sendUnaryData<nodesPB.ChainData>,
Expand Down Expand Up @@ -38,6 +45,7 @@ function nodesChainDataGet({ sigchain }: { sigchain: Sigchain }) {
return;
} catch (e) {
callback(grpcUtils.fromError(e, true));
logger.error(e);
return;
}
};
Expand Down
4 changes: 4 additions & 0 deletions src/agent/service/nodesClosestLocalNodesGet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type * as grpc from '@grpc/grpc-js';
import type { NodeConnectionManager } from '../../nodes';
import type { NodeId } from '../../nodes/types';
import type Logger from '@matrixai/logger';
import { utils as grpcUtils } from '../../grpc';
import { utils as nodesUtils } from '../../nodes';
import { validateSync, utils as validationUtils } from '../../validation';
Expand All @@ -13,8 +14,10 @@ import * as nodesPB from '../../proto/js/polykey/v1/nodes/nodes_pb';
*/
function nodesClosestLocalNodesGet({
nodeConnectionManager,
logger,
}: {
nodeConnectionManager: NodeConnectionManager;
logger: Logger;
}) {
return async (
call: grpc.ServerUnaryCall<nodesPB.Node, nodesPB.NodeTable>,
Expand Down Expand Up @@ -54,6 +57,7 @@ function nodesClosestLocalNodesGet({
return;
} catch (e) {
callback(grpcUtils.fromError(e, true));
logger.error(e);
return;
}
};
Expand Down
12 changes: 5 additions & 7 deletions src/agent/service/nodesCrossSignClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { NodeId } from '../../nodes/types';
import type { Sigchain } from '../../sigchain';
import type { KeyManager } from '../../keys';
import type * as nodesPB from '../../proto/js/polykey/v1/nodes/nodes_pb';
import type Logger from '@matrixai/logger';
import { utils as grpcUtils } from '../../grpc';
import { utils as claimsUtils, errors as claimsErrors } from '../../claims';
import { utils as nodesUtils } from '../../nodes';
Expand All @@ -15,16 +16,16 @@ function nodesCrossSignClaim({
keyManager,
nodeManager,
sigchain,
logger,
}: {
keyManager: KeyManager;
nodeManager: NodeManager;
sigchain: Sigchain;
logger: Logger;
}) {
return async (
call: grpc.ServerDuplexStream<nodesPB.CrossSign, nodesPB.CrossSign>,
) => {
// TODO: Move all "await genClaims.throw" to a final catch(). Wrap this
// entire thing in a try block. And re-throw whatever error is caught
const genClaims = grpcUtils.generatorDuplex(call, true);
try {
await sigchain.transaction(async (sigchain) => {
Expand Down Expand Up @@ -149,9 +150,7 @@ function nodesCrossSignClaim({
senderPublicKey,
));
if (!verifiedDoubly) {
await genClaims.throw(
new claimsErrors.ErrorDoublySignedClaimVerificationFailed(),
);
throw new claimsErrors.ErrorDoublySignedClaimVerificationFailed();
}
// If verified, then we can safely add to our sigchain
await sigchain.addExistingClaim(constructedDoublySignedClaim);
Expand All @@ -161,8 +160,7 @@ function nodesCrossSignClaim({
});
} catch (e) {
await genClaims.throw(e);
// TODO: Handle the exception on this server - throw e?
// throw e;
logger.error(e);
return;
}
};
Expand Down
4 changes: 4 additions & 0 deletions src/agent/service/nodesHolePunchMessageSend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { NodeManager, NodeConnectionManager } from '../../nodes';
import type KeyManager from '../../keys/KeyManager';
import type { NodeId } from '../../nodes/types';
import type * as nodesPB from '../../proto/js/polykey/v1/nodes/nodes_pb';
import type Logger from '@matrixai/logger';
import * as networkUtils from '../../network/utils';
import { utils as grpcUtils } from '../../grpc';
import { validateSync, utils as validationUtils } from '../../validation';
Expand All @@ -13,10 +14,12 @@ function nodesHolePunchMessageSend({
keyManager,
nodeManager,
nodeConnectionManager,
logger,
}: {
keyManager: KeyManager;
nodeManager: NodeManager;
nodeConnectionManager: NodeConnectionManager;
logger: Logger;
}) {
return async (
call: grpc.ServerUnaryCall<nodesPB.Relay, utilsPB.EmptyMessage>,
Expand Down Expand Up @@ -63,6 +66,7 @@ function nodesHolePunchMessageSend({
return;
} catch (e) {
callback(grpcUtils.fromError(e, true));
logger.error(e);
return;
}
};
Expand Down
4 changes: 4 additions & 0 deletions src/agent/service/notificationsSend.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import type * as grpc from '@grpc/grpc-js';
import type { NotificationsManager } from '../../notifications';
import type * as notificationsPB from '../../proto/js/polykey/v1/notifications/notifications_pb';
import type Logger from '@matrixai/logger';
import { utils as grpcUtils } from '../../grpc';
import { utils as notificationsUtils } from '../../notifications';
import * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb';

function notificationsSend({
notificationsManager,
logger,
}: {
notificationsManager: NotificationsManager;
logger: Logger;
}) {
return async (
call: grpc.ServerUnaryCall<
Expand All @@ -26,6 +29,7 @@ function notificationsSend({
return;
} catch (e) {
callback(grpcUtils.fromError(e, true));
logger.error(e);
return;
}
};
Expand Down
Loading

0 comments on commit 0538ef9

Please sign in to comment.