Skip to content

Commit

Permalink
fix: service handlers check for client error chains
Browse files Browse the repository at this point in the history
Sometimes an error is only considered a client error when part of the chain of another error (for example ErrorPolykeyRemote) so it is now possible to specify this array.

#304
  • Loading branch information
emmacasolin committed Jun 6, 2022
1 parent 7a294e1 commit c1f08b5
Show file tree
Hide file tree
Showing 78 changed files with 345 additions and 458 deletions.
2 changes: 1 addition & 1 deletion src/agent/service/nodesChainDataGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function nodesChainDataGet({
return;
} catch (e) {
callback(grpcUtils.fromError(e, true));
!agentUtils.isClientError(e) && logger.error(e);
!agentUtils.isAgentClientError(e) && logger.error(e);
return;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/agent/service/nodesClosestLocalNodesGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function nodesClosestLocalNodesGet({
return;
} catch (e) {
callback(grpcUtils.fromError(e, true));
!agentUtils.isClientError(e) && logger.error(e);
!agentUtils.isAgentClientError(e) && logger.error(e);
return;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/agent/service/nodesCrossSignClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ function nodesCrossSignClaim({
});
} catch (e) {
await genClaims.throw(e);
!agentUtils.isClientError(e, [
!agentUtils.isAgentClientError(e, [
claimsErrors.ErrorEmptyStream,
claimsErrors.ErrorUndefinedSinglySignedClaim,
claimsErrors.ErrorUndefinedSignature,
Expand Down
2 changes: 1 addition & 1 deletion src/agent/service/nodesHolePunchMessageSend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function nodesHolePunchMessageSend({
return;
} catch (e) {
callback(grpcUtils.fromError(e, true));
!agentUtils.isClientError(e) && logger.error(e);
!agentUtils.isAgentClientError(e) && logger.error(e);
return;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/agent/service/notificationsSend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function notificationsSend({
return;
} catch (e) {
callback(grpcUtils.fromError(e, true));
!agentUtils.isClientError(e, [
!agentUtils.isAgentClientError(e, [
notificationsErrors.ErrorNotificationsInvalidType,
notificationsErrors.ErrorNotificationsValidationFailed,
notificationsErrors.ErrorNotificationsParse,
Expand Down
17 changes: 8 additions & 9 deletions src/agent/service/vaultsGitInfoGet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { DB } from '@matrixai/db';
import type { VaultName, VaultId, VaultAction } from '../../vaults/types';
import type { VaultName, VaultAction } from '../../vaults/types';
import type VaultManager from '../../vaults/VaultManager';
import type ACL from '../../acl/ACL';
import type { ConnectionInfoGet } from '../../agent/types';
Expand Down Expand Up @@ -39,25 +39,24 @@ function vaultsGitInfoGet({
call.request.getVault()?.getNameOrId() as VaultName,
tran,
);
const vaultId =
vaultIdFromName ??
vaultsUtils.decodeVaultId(call.request.getVault()?.getNameOrId());
if (vaultId == null) {
throw new vaultsErrors.ErrorVaultsVaultUndefined();
}
const {
vaultId,
actionType,
}: {
vaultId: VaultId;
actionType: VaultAction;
} = validateSync(
(keyPath, value) => {
return matchSync(keyPath)(
[
['vaultId'],
() => vaultIdFromName ?? validationUtils.parseVaultId(value),
],
[['actionType'], () => validationUtils.parseVaultAction(value)],
() => value,
);
},
{
vaultId: call.request.getVault()?.getNameOrId(),
actionType: call.request.getAction(),
},
);
Expand Down Expand Up @@ -108,7 +107,7 @@ function vaultsGitInfoGet({
return;
} catch (e) {
await genWritable.throw(e);
!agentUtils.isClientError(e, [
!agentUtils.isAgentClientError(e, [
vaultsErrors.ErrorVaultsVaultUndefined,
agentErrors.ErrorConnectionInfoMissing,
vaultsErrors.ErrorVaultsPermissionDenied,
Expand Down
15 changes: 6 additions & 9 deletions src/agent/service/vaultsGitPackGet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as grpc from '@grpc/grpc-js';
import type { DB } from '@matrixai/db';
import type { VaultName, VaultId, VaultAction } from '../../vaults/types';
import type { VaultName, VaultAction } from '../../vaults/types';
import type VaultManager from '../../vaults/VaultManager';
import type { ConnectionInfoGet } from '../../agent/types';
import type ACL from '../../acl/ACL';
Expand Down Expand Up @@ -62,25 +62,22 @@ function vaultsGitPackGet({
nameOrId as VaultName,
tran,
);
const vaultId = vaultIdFromName ?? vaultsUtils.decodeVaultId(nameOrId);
if (vaultId == null) {
throw new vaultsErrors.ErrorVaultsVaultUndefined();
}
const {
vaultId,
actionType,
}: {
vaultId: VaultId;
actionType: VaultAction;
} = validateSync(
(keyPath, value) => {
return matchSync(keyPath)(
[
['vaultId'],
() => vaultIdFromName ?? validationUtils.parseVaultId(value),
],
[['actionType'], () => validationUtils.parseVaultAction(value)],
() => value,
);
},
{
vaultId: nameOrId,
actionType: meta.get('vaultAction').pop()!.toString(),
},
);
Expand Down Expand Up @@ -123,7 +120,7 @@ function vaultsGitPackGet({
return;
} catch (e) {
await genDuplex.throw(e);
!agentUtils.isClientError(e, [
!agentUtils.isAgentClientError(e, [
agentErrors.ErrorConnectionInfoMissing,
vaultsErrors.ErrorVaultsPermissionDenied,
vaultsErrors.ErrorVaultsVaultUndefined,
Expand Down
2 changes: 1 addition & 1 deletion src/agent/service/vaultsScan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function vaultsScan({
return;
} catch (e) {
await genWritable.throw(e);
!agentUtils.isClientError(e, [
!agentUtils.isAgentClientError(e, [
agentErrors.ErrorConnectionInfoMissing,
vaultsErrors.ErrorVaultsPermissionDenied,
]) && logger.error(e);
Expand Down
10 changes: 8 additions & 2 deletions src/agent/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import type { ConnectionInfo } from 'network/types';
import type { ServerSurfaceCall } from '@grpc/grpc-js/build/src/server-call';
import type { Class } from '@matrixai/errors';
import type { ConnectionInfo } from '../network/types';
import type ErrorPolykey from '../ErrorPolykey';

type ConnectionInfoGet = (
call: ServerSurfaceCall,
) => ConnectionInfo | undefined;

export type { ConnectionInfoGet };
type AgentClientErrors = Array<
Class<ErrorPolykey<any>> | Array<Class<ErrorPolykey<any>>>
>;

export type { ConnectionInfoGet, AgentClientErrors };
56 changes: 45 additions & 11 deletions src/agent/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { ServerSurfaceCall } from '@grpc/grpc-js/build/src/server-call';
import type { Class } from '@matrixai/errors';
import type { Host, Port } from 'network/types';
import type Proxy from 'network/Proxy';
import type { ConnectionInfoGet } from './types';
import type ErrorPolykey from '../ErrorPolykey';
import type { Host, Port } from '../network/types';
import type Proxy from '../network/Proxy';
import type { ConnectionInfoGet, AgentClientErrors } from './types';
import * as validationErrors from '../validation/errors';

/**
* Array of errors that are always considered to be "client errors"
* (4xx errors in HTTP) in the context of the agent service
*/
const defaultClientErrors: Array<Class<Error>> = [
const defaultClientErrors: AgentClientErrors = [
validationErrors.ErrorValidation,
];

Expand All @@ -33,19 +33,53 @@ function connectionInfoGetter(proxy: Proxy): ConnectionInfoGet {
* context of a given handler can be supplied in the `extraClientErrors`
* argument
*/
function isClientError(
e: Error,
extraClientErrors?: Array<Class<Error>>,
function isAgentClientError(
thrownError: ErrorPolykey<any>,
extraClientErrors?: AgentClientErrors,
): boolean {
for (const error of defaultClientErrors) {
if (e instanceof error) return true;
if (Array.isArray(error)) {
let e = thrownError;
let matches = true;
for (const eType of error) {
if (e == null) {
matches = false;
break;
}
if (!(e instanceof eType)) {
matches = false;
break;
}
e = e.cause;
}
if (matches) return true;
} else if (thrownError instanceof error) {
return true;
}
}
if (extraClientErrors) {
for (const error of extraClientErrors) {
if (e instanceof error) return true;
if (Array.isArray(error)) {
let e = thrownError;
let matches = true;
for (const eType of error) {
if (e == null) {
matches = false;
break;
}
if (!(e instanceof eType)) {
matches = false;
break;
}
e = e.cause;
}
if (matches) return true;
} else if (thrownError instanceof error) {
return true;
}
}
}
return false;
}

export { connectionInfoGetter, isClientError };
export { connectionInfoGetter, isAgentClientError };
2 changes: 1 addition & 1 deletion src/client/service/agentLockAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function agentLockAll({
return;
} catch (e) {
callback(grpcUtils.fromError(e));
!clientUtils.isClientError(e) && logger.error(e);
!clientUtils.isClientClientError(e) && logger.error(e);
return;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/agentStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function agentStatus({
return;
} catch (e) {
callback(grpcUtils.fromError(e));
!clientUtils.isClientError(e) && logger.error(e);
!clientUtils.isClientClientError(e) && logger.error(e);
return;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/agentStop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function agentStop({
callback(null, response);
} catch (e) {
callback(grpcUtils.fromError(e));
!clientUtils.isClientError(e) && logger.error(e);
!clientUtils.isClientClientError(e) && logger.error(e);
return;
}
// Stop is called after GRPC resources are cleared
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/agentUnlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function agentUnlock({
return;
} catch (e) {
callback(grpcUtils.fromError(e));
!clientUtils.isClientError(e) && logger.error(e);
!clientUtils.isClientClientError(e) && logger.error(e);
return;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsActionsGetByIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function gestaltsActionsGetByIdentity({
return;
} catch (e) {
callback(grpcUtils.fromError(e));
!clientUtils.isClientError(e) && logger.error(e);
!clientUtils.isClientClientError(e) && logger.error(e);
return;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsActionsGetByNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function gestaltsActionsGetByNode({
return;
} catch (e) {
callback(grpcUtils.fromError(e));
!clientUtils.isClientError(e) && logger.error(e);
!clientUtils.isClientClientError(e) && logger.error(e);
return;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsActionsSetByIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function gestaltsActionsSetByIdentity({
return;
} catch (e) {
callback(grpcUtils.fromError(e));
!clientUtils.isClientError(e, [
!clientUtils.isClientClientError(e, [
gestaltsErrors.ErrorGestaltsGraphIdentityIdMissing,
gestaltsErrors.ErrorGestaltsGraphNodeIdMissing,
]) && logger.error(e);
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsActionsSetByNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function gestaltsActionsSetByNode({
return;
} catch (e) {
callback(grpcUtils.fromError(e));
!clientUtils.isClientError(e, [
!clientUtils.isClientClientError(e, [
gestaltsErrors.ErrorGestaltsGraphNodeIdMissing,
]) && logger.error(e);
return;
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsActionsUnsetByIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function gestaltsActionsUnsetByIdentity({
return;
} catch (e) {
callback(grpcUtils.fromError(e));
!clientUtils.isClientError(e, [
!clientUtils.isClientClientError(e, [
gestaltsErrors.ErrorGestaltsGraphIdentityIdMissing,
gestaltsErrors.ErrorGestaltsGraphNodeIdMissing,
]) && logger.error(e);
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsActionsUnsetByNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function gestaltsActionsUnsetByNode({
return;
} catch (e) {
callback(grpcUtils.fromError(e));
!clientUtils.isClientError(e, [
!clientUtils.isClientClientError(e, [
gestaltsErrors.ErrorGestaltsGraphNodeIdMissing,
]) && logger.error(e);
return;
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsDiscoveryByIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function gestaltsDiscoveryByIdentity({
return;
} catch (e) {
callback(grpcUtils.fromError(e));
!clientUtils.isClientError(e) && logger.error(e);
!clientUtils.isClientClientError(e) && logger.error(e);
return;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsDiscoveryByNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function gestaltsDiscoveryByNode({
return;
} catch (e) {
callback(grpcUtils.fromError(e));
!clientUtils.isClientError(e) && logger.error(e);
!clientUtils.isClientClientError(e) && logger.error(e);
return;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsGestaltGetByIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function gestaltsGestaltGetByIdentity({
return;
} catch (e) {
callback(grpcUtils.fromError(e));
!clientUtils.isClientError(e) && logger.error(e);
!clientUtils.isClientClientError(e) && logger.error(e);
return;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsGestaltGetByNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function gestaltsGestaltGetByNode({
return;
} catch (e) {
callback(grpcUtils.fromError(e));
!clientUtils.isClientError(e) && logger.error(e);
!clientUtils.isClientClientError(e) && logger.error(e);
return;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsGestaltList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function gestaltsGestaltList({
return;
} catch (e) {
await genWritable.throw(e);
!clientUtils.isClientError(e) && logger.error(e);
!clientUtils.isClientClientError(e) && logger.error(e);
return;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsGestaltTrustByIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function gestaltsGestaltTrustByIdentity({
return;
} catch (e) {
callback(grpcUtils.fromError(e));
!clientUtils.isClientError(e, [
!clientUtils.isClientClientError(e, [
gestaltsErrors.ErrorGestaltsGraphIdentityIdMissing,
gestaltsErrors.ErrorGestaltsGraphNodeIdMissing,
]) && logger.error(e);
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsGestaltTrustByNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function gestaltsGestaltTrustByNode({
return;
} catch (e) {
callback(grpcUtils.fromError(e));
!clientUtils.isClientError(e, [
!clientUtils.isClientClientError(e, [
gestaltsErrors.ErrorGestaltsGraphNodeIdMissing,
]) && logger.error(e);
return;
Expand Down
Loading

0 comments on commit c1f08b5

Please sign in to comment.