Skip to content

Commit 489599c

Browse files
committed
fix: service handlers check for client error chains
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
1 parent adf7dbd commit 489599c

78 files changed

Lines changed: 345 additions & 458 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/agent/service/nodesChainDataGet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function nodesChainDataGet({
5151
return;
5252
} catch (e) {
5353
callback(grpcUtils.fromError(e, true));
54-
!agentUtils.isClientError(e) && logger.error(e);
54+
!agentUtils.isAgentClientError(e) && logger.error(e);
5555
return;
5656
}
5757
};

src/agent/service/nodesClosestLocalNodesGet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function nodesClosestLocalNodesGet({
6767
return;
6868
} catch (e) {
6969
callback(grpcUtils.fromError(e, true));
70-
!agentUtils.isClientError(e) && logger.error(e);
70+
!agentUtils.isAgentClientError(e) && logger.error(e);
7171
return;
7272
}
7373
};

src/agent/service/nodesCrossSignClaim.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ function nodesCrossSignClaim({
173173
});
174174
} catch (e) {
175175
await genClaims.throw(e);
176-
!agentUtils.isClientError(e, [
176+
!agentUtils.isAgentClientError(e, [
177177
claimsErrors.ErrorEmptyStream,
178178
claimsErrors.ErrorUndefinedSinglySignedClaim,
179179
claimsErrors.ErrorUndefinedSignature,

src/agent/service/nodesHolePunchMessageSend.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function nodesHolePunchMessageSend({
7474
return;
7575
} catch (e) {
7676
callback(grpcUtils.fromError(e, true));
77-
!agentUtils.isClientError(e) && logger.error(e);
77+
!agentUtils.isAgentClientError(e) && logger.error(e);
7878
return;
7979
}
8080
};

src/agent/service/notificationsSend.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function notificationsSend({
3636
return;
3737
} catch (e) {
3838
callback(grpcUtils.fromError(e, true));
39-
!agentUtils.isClientError(e, [
39+
!agentUtils.isAgentClientError(e, [
4040
notificationsErrors.ErrorNotificationsInvalidType,
4141
notificationsErrors.ErrorNotificationsValidationFailed,
4242
notificationsErrors.ErrorNotificationsParse,

src/agent/service/vaultsGitInfoGet.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { DB } from '@matrixai/db';
2-
import type { VaultName, VaultId, VaultAction } from '../../vaults/types';
2+
import type { VaultName, VaultAction } from '../../vaults/types';
33
import type VaultManager from '../../vaults/VaultManager';
44
import type ACL from '../../acl/ACL';
55
import type { ConnectionInfoGet } from '../../agent/types';
@@ -39,25 +39,24 @@ function vaultsGitInfoGet({
3939
call.request.getVault()?.getNameOrId() as VaultName,
4040
tran,
4141
);
42+
const vaultId =
43+
vaultIdFromName ??
44+
vaultsUtils.decodeVaultId(call.request.getVault()?.getNameOrId());
45+
if (vaultId == null) {
46+
throw new vaultsErrors.ErrorVaultsVaultUndefined();
47+
}
4248
const {
43-
vaultId,
4449
actionType,
4550
}: {
46-
vaultId: VaultId;
4751
actionType: VaultAction;
4852
} = validateSync(
4953
(keyPath, value) => {
5054
return matchSync(keyPath)(
51-
[
52-
['vaultId'],
53-
() => vaultIdFromName ?? validationUtils.parseVaultId(value),
54-
],
5555
[['actionType'], () => validationUtils.parseVaultAction(value)],
5656
() => value,
5757
);
5858
},
5959
{
60-
vaultId: call.request.getVault()?.getNameOrId(),
6160
actionType: call.request.getAction(),
6261
},
6362
);
@@ -108,7 +107,7 @@ function vaultsGitInfoGet({
108107
return;
109108
} catch (e) {
110109
await genWritable.throw(e);
111-
!agentUtils.isClientError(e, [
110+
!agentUtils.isAgentClientError(e, [
112111
vaultsErrors.ErrorVaultsVaultUndefined,
113112
agentErrors.ErrorConnectionInfoMissing,
114113
vaultsErrors.ErrorVaultsPermissionDenied,

src/agent/service/vaultsGitPackGet.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type * as grpc from '@grpc/grpc-js';
22
import type { DB } from '@matrixai/db';
3-
import type { VaultName, VaultId, VaultAction } from '../../vaults/types';
3+
import type { VaultName, VaultAction } from '../../vaults/types';
44
import type VaultManager from '../../vaults/VaultManager';
55
import type { ConnectionInfoGet } from '../../agent/types';
66
import type ACL from '../../acl/ACL';
@@ -62,25 +62,22 @@ function vaultsGitPackGet({
6262
nameOrId as VaultName,
6363
tran,
6464
);
65+
const vaultId = vaultIdFromName ?? vaultsUtils.decodeVaultId(nameOrId);
66+
if (vaultId == null) {
67+
throw new vaultsErrors.ErrorVaultsVaultUndefined();
68+
}
6569
const {
66-
vaultId,
6770
actionType,
6871
}: {
69-
vaultId: VaultId;
7072
actionType: VaultAction;
7173
} = validateSync(
7274
(keyPath, value) => {
7375
return matchSync(keyPath)(
74-
[
75-
['vaultId'],
76-
() => vaultIdFromName ?? validationUtils.parseVaultId(value),
77-
],
7876
[['actionType'], () => validationUtils.parseVaultAction(value)],
7977
() => value,
8078
);
8179
},
8280
{
83-
vaultId: nameOrId,
8481
actionType: meta.get('vaultAction').pop()!.toString(),
8582
},
8683
);
@@ -123,7 +120,7 @@ function vaultsGitPackGet({
123120
return;
124121
} catch (e) {
125122
await genDuplex.throw(e);
126-
!agentUtils.isClientError(e, [
123+
!agentUtils.isAgentClientError(e, [
127124
agentErrors.ErrorConnectionInfoMissing,
128125
vaultsErrors.ErrorVaultsPermissionDenied,
129126
vaultsErrors.ErrorVaultsVaultUndefined,

src/agent/service/vaultsScan.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function vaultsScan({
5353
return;
5454
} catch (e) {
5555
await genWritable.throw(e);
56-
!agentUtils.isClientError(e, [
56+
!agentUtils.isAgentClientError(e, [
5757
agentErrors.ErrorConnectionInfoMissing,
5858
vaultsErrors.ErrorVaultsPermissionDenied,
5959
]) && logger.error(e);

src/agent/types.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import type { ConnectionInfo } from 'network/types';
21
import type { ServerSurfaceCall } from '@grpc/grpc-js/build/src/server-call';
2+
import type { Class } from '@matrixai/errors';
3+
import type { ConnectionInfo } from '../network/types';
4+
import type ErrorPolykey from '../ErrorPolykey';
35

46
type ConnectionInfoGet = (
57
call: ServerSurfaceCall,
68
) => ConnectionInfo | undefined;
79

8-
export type { ConnectionInfoGet };
10+
type AgentClientErrors = Array<
11+
Class<ErrorPolykey<any>> | Array<Class<ErrorPolykey<any>>>
12+
>;
13+
14+
export type { ConnectionInfoGet, AgentClientErrors };

src/agent/utils.ts

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import type { ServerSurfaceCall } from '@grpc/grpc-js/build/src/server-call';
2-
import type { Class } from '@matrixai/errors';
3-
import type { Host, Port } from 'network/types';
4-
import type Proxy from 'network/Proxy';
5-
import type { ConnectionInfoGet } from './types';
2+
import type ErrorPolykey from '../ErrorPolykey';
3+
import type { Host, Port } from '../network/types';
4+
import type Proxy from '../network/Proxy';
5+
import type { ConnectionInfoGet, AgentClientErrors } from './types';
66
import * as validationErrors from '../validation/errors';
77

88
/**
99
* Array of errors that are always considered to be "client errors"
1010
* (4xx errors in HTTP) in the context of the agent service
1111
*/
12-
const defaultClientErrors: Array<Class<Error>> = [
12+
const defaultClientErrors: AgentClientErrors = [
1313
validationErrors.ErrorValidation,
1414
];
1515

@@ -33,19 +33,53 @@ function connectionInfoGetter(proxy: Proxy): ConnectionInfoGet {
3333
* context of a given handler can be supplied in the `extraClientErrors`
3434
* argument
3535
*/
36-
function isClientError(
37-
e: Error,
38-
extraClientErrors?: Array<Class<Error>>,
36+
function isAgentClientError(
37+
thrownError: ErrorPolykey<any>,
38+
extraClientErrors?: AgentClientErrors,
3939
): boolean {
4040
for (const error of defaultClientErrors) {
41-
if (e instanceof error) return true;
41+
if (Array.isArray(error)) {
42+
let e = thrownError;
43+
let matches = true;
44+
for (const eType of error) {
45+
if (e == null) {
46+
matches = false;
47+
break;
48+
}
49+
if (!(e instanceof eType)) {
50+
matches = false;
51+
break;
52+
}
53+
e = e.cause;
54+
}
55+
if (matches) return true;
56+
} else if (thrownError instanceof error) {
57+
return true;
58+
}
4259
}
4360
if (extraClientErrors) {
4461
for (const error of extraClientErrors) {
45-
if (e instanceof error) return true;
62+
if (Array.isArray(error)) {
63+
let e = thrownError;
64+
let matches = true;
65+
for (const eType of error) {
66+
if (e == null) {
67+
matches = false;
68+
break;
69+
}
70+
if (!(e instanceof eType)) {
71+
matches = false;
72+
break;
73+
}
74+
e = e.cause;
75+
}
76+
if (matches) return true;
77+
} else if (thrownError instanceof error) {
78+
return true;
79+
}
4680
}
4781
}
4882
return false;
4983
}
5084

51-
export { connectionInfoGetter, isClientError };
85+
export { connectionInfoGetter, isAgentClientError };

0 commit comments

Comments
 (0)