@dxos/protocols 0.1.58-main.f69fa2f → 0.1.58-main.f860ba2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/dist/cjs/src/errors/base-errors.d.ts +27 -0
  2. package/dist/cjs/src/errors/base-errors.d.ts.map +1 -0
  3. package/dist/cjs/src/errors/base-errors.js +43 -0
  4. package/dist/cjs/src/errors/base-errors.js.map +1 -0
  5. package/dist/cjs/src/errors/encoding.d.ts +1 -1
  6. package/dist/cjs/src/errors/encoding.d.ts.map +1 -1
  7. package/dist/cjs/src/errors/errors.d.ts +2 -28
  8. package/dist/cjs/src/errors/errors.d.ts.map +1 -1
  9. package/dist/cjs/src/errors/errors.js +52 -55
  10. package/dist/cjs/src/errors/errors.js.map +1 -1
  11. package/dist/cjs/src/errors/errors.test.js +2 -2
  12. package/dist/cjs/src/errors/errors.test.js.map +1 -1
  13. package/dist/cjs/src/errors/helpers.d.ts +7 -3
  14. package/dist/cjs/src/errors/helpers.d.ts.map +1 -1
  15. package/dist/cjs/src/errors/helpers.js +21 -58
  16. package/dist/cjs/src/errors/helpers.js.map +1 -1
  17. package/dist/cjs/src/errors/index.d.ts +1 -0
  18. package/dist/cjs/src/errors/index.d.ts.map +1 -1
  19. package/dist/cjs/src/errors/index.js +1 -0
  20. package/dist/cjs/src/errors/index.js.map +1 -1
  21. package/dist/cjs/src/proto/gen/dxos/error.d.ts +2 -0
  22. package/dist/cjs/src/proto/gen/dxos/error.d.ts.map +1 -1
  23. package/dist/cjs/src/proto/gen/index.d.ts.map +1 -1
  24. package/dist/cjs/src/proto/gen/index.js +1 -1
  25. package/dist/cjs/src/proto/gen/index.js.map +1 -1
  26. package/dist/cjs/tsconfig.tsbuildinfo +1 -1
  27. package/dist/esm/src/errors/base-errors.d.ts +27 -0
  28. package/dist/esm/src/errors/base-errors.d.ts.map +1 -0
  29. package/dist/esm/src/errors/base-errors.js +38 -0
  30. package/dist/esm/src/errors/base-errors.js.map +1 -0
  31. package/dist/esm/src/errors/encoding.d.ts +1 -1
  32. package/dist/esm/src/errors/encoding.d.ts.map +1 -1
  33. package/dist/esm/src/errors/errors.d.ts +2 -28
  34. package/dist/esm/src/errors/errors.d.ts.map +1 -1
  35. package/dist/esm/src/errors/errors.js +30 -34
  36. package/dist/esm/src/errors/errors.js.map +1 -1
  37. package/dist/esm/src/errors/errors.test.js +1 -1
  38. package/dist/esm/src/errors/errors.test.js.map +1 -1
  39. package/dist/esm/src/errors/helpers.d.ts +7 -3
  40. package/dist/esm/src/errors/helpers.d.ts.map +1 -1
  41. package/dist/esm/src/errors/helpers.js +17 -57
  42. package/dist/esm/src/errors/helpers.js.map +1 -1
  43. package/dist/esm/src/errors/index.d.ts +1 -0
  44. package/dist/esm/src/errors/index.d.ts.map +1 -1
  45. package/dist/esm/src/errors/index.js +1 -0
  46. package/dist/esm/src/errors/index.js.map +1 -1
  47. package/dist/esm/src/proto/gen/dxos/error.d.ts +2 -0
  48. package/dist/esm/src/proto/gen/dxos/error.d.ts.map +1 -1
  49. package/dist/esm/src/proto/gen/index.d.ts.map +1 -1
  50. package/dist/esm/src/proto/gen/index.js +1 -1
  51. package/dist/esm/src/proto/gen/index.js.map +1 -1
  52. package/package.json +5 -5
  53. package/src/errors/base-errors.ts +34 -0
  54. package/src/errors/errors.test.ts +1 -1
  55. package/src/errors/errors.ts +50 -32
  56. package/src/errors/helpers.ts +20 -94
  57. package/src/errors/index.ts +1 -0
  58. package/src/proto/dxos/error.proto +1 -0
  59. package/src/proto/gen/dxos/error.ts +2 -0
  60. package/src/proto/gen/index.ts +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/proto/gen/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAuDhE,OAAO,aAAa,MAAM,kBAAkB,CAAC;AAgf7C,MAAM,CAAC,MAAM,UAAU,GAAG,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,6s2FAA6s2F,CAAC,CAAC,CAAC;AACtw2F,MAAM,CAAC,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAkB,UAAU,EAAE,aAAa,CAAC,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/proto/gen/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAuDhE,OAAO,aAAa,MAAM,kBAAkB,CAAC;AAgf7C,MAAM,CAAC,MAAM,UAAU,GAAG,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,uy2FAAuy2F,CAAC,CAAC,CAAC;AACh22F,MAAM,CAAC,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAkB,UAAU,EAAE,aAAa,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/protocols",
3
- "version": "0.1.58-main.f69fa2f",
3
+ "version": "0.1.58-main.f860ba2",
4
4
  "description": "Protobuf definitions for DXOS protocols.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -33,10 +33,10 @@
33
33
  "src"
34
34
  ],
35
35
  "dependencies": {
36
- "@dxos/codec-protobuf": "0.1.58-main.f69fa2f",
37
- "@dxos/invariant": "0.1.58-main.f69fa2f",
38
- "@dxos/keys": "0.1.58-main.f69fa2f",
39
- "@dxos/timeframe": "0.1.58-main.f69fa2f"
36
+ "@dxos/codec-protobuf": "0.1.58-main.f860ba2",
37
+ "@dxos/invariant": "0.1.58-main.f860ba2",
38
+ "@dxos/keys": "0.1.58-main.f860ba2",
39
+ "@dxos/timeframe": "0.1.58-main.f860ba2"
40
40
  },
41
41
  "devDependencies": {
42
42
  "glob": "~7.1.6"
@@ -0,0 +1,34 @@
1
+ //
2
+ // Copyright 2021 DXOS.org
3
+ //
4
+
5
+ /**
6
+ * NOTE: Messages should be sentences (Start with a capital letter and end with a period).
7
+ * Errors can optionally include a JSON context object.
8
+ */
9
+ export class BaseError extends Error {
10
+ constructor(readonly code: string, message?: string, readonly context?: Record<string, any>) {
11
+ super(message ?? code);
12
+ this.name = code;
13
+ // NOTE: Restores prototype chain (https://stackoverflow.com/a/48342359).
14
+ Object.setPrototypeOf(this, new.target.prototype);
15
+ }
16
+ }
17
+
18
+ /**
19
+ * User facing API Errors.
20
+ * E.g., something was misconfigured.
21
+ */
22
+ export class ApiError extends BaseError {}
23
+
24
+ /**
25
+ * Internal system errors.
26
+ * E.g., unexpected/unrecoverable runtime error.
27
+ */
28
+ export class SystemError extends BaseError {}
29
+
30
+ /**
31
+ * Database errors.
32
+ */
33
+ // TODO(wittjosiah): Same as ApiError?
34
+ export class DatabaseError extends BaseError {}
@@ -7,7 +7,7 @@ import chaiAsPromised from 'chai-as-promised';
7
7
 
8
8
  import { describe, test } from '@dxos/test';
9
9
 
10
- import { ApiError } from './errors';
10
+ import { ApiError } from './base-errors';
11
11
 
12
12
  chai.use(chaiAsPromised);
13
13
 
@@ -2,41 +2,12 @@
2
2
  // Copyright 2021 DXOS.org
3
3
  //
4
4
 
5
- import type { PublicKey } from '@dxos/keys';
5
+ import { PublicKey } from '@dxos/keys';
6
6
 
7
+ import { ApiError, DatabaseError, SystemError, BaseError } from './base-errors';
8
+ import { registerError, registerErrorMessageContext, registerErrorNoArgs } from './helpers';
7
9
  import type { ItemID } from '../types';
8
10
 
9
- /**
10
- * NOTE: Messages should be sentences (Start with a capital letter and end with a period).
11
- * Errors can optionally include a JSON context object.
12
- */
13
- class BaseError extends Error {
14
- constructor(readonly code: string, message?: string, readonly context?: Record<string, any>) {
15
- super(message ?? code);
16
- this.name = code;
17
- // NOTE: Restores prototype chain (https://stackoverflow.com/a/48342359).
18
- Object.setPrototypeOf(this, new.target.prototype);
19
- }
20
- }
21
-
22
- /**
23
- * User facing API Errors.
24
- * E.g., something was misconfigured.
25
- */
26
- export class ApiError extends BaseError {}
27
-
28
- /**
29
- * Internal system errors.
30
- * E.g., unexpected/unrecoverable runtime error.
31
- */
32
- export class SystemError extends BaseError {}
33
-
34
- /**
35
- * Database errors.
36
- */
37
- // TODO(wittjosiah): Same as ApiError?
38
- export class DatabaseError extends BaseError {}
39
-
40
11
  /**
41
12
  * Thrown when request was terminated because the RPC endpoint has been closed.
42
13
  */
@@ -46,6 +17,8 @@ export class RpcClosedError extends SystemError {
46
17
  }
47
18
  }
48
19
 
20
+ registerErrorNoArgs('RPC_CLOSED', RpcClosedError);
21
+
49
22
  /**
50
23
  * Thrown when `request` is called when RPC has not been opened.
51
24
  */
@@ -55,18 +28,24 @@ export class RpcNotOpenError extends SystemError {
55
28
  }
56
29
  }
57
30
 
31
+ registerErrorNoArgs('RPC_NOT_OPEN', RpcNotOpenError);
32
+
58
33
  export class CancelledError extends SystemError {
59
34
  constructor(message?: string, context?: Record<string, any>) {
60
35
  super('CANCELLED', message, context);
61
36
  }
62
37
  }
63
38
 
39
+ registerErrorMessageContext('CANCELLED', CancelledError);
40
+
64
41
  export class InvalidConfigError extends ApiError {
65
42
  constructor(message?: string, context?: Record<string, any>) {
66
43
  super('INVALID_CONFIG', message, context);
67
44
  }
68
45
  }
69
46
 
47
+ registerErrorMessageContext('INVALID_CONFIG', InvalidConfigError);
48
+
70
49
  /**
71
50
  * Explicit failure to connect with remote client services.
72
51
  */
@@ -76,6 +55,8 @@ export class RemoteServiceConnectionError extends ApiError {
76
55
  }
77
56
  }
78
57
 
58
+ registerErrorMessageContext('REMOTE_SERVICE_CONNECTION_ERROR', RemoteServiceConnectionError);
59
+
79
60
  /**
80
61
  * Failed to open a connection to remote client services.
81
62
  */
@@ -85,42 +66,56 @@ export class RemoteServiceConnectionTimeout extends ApiError {
85
66
  }
86
67
  }
87
68
 
69
+ registerErrorMessageContext('REMOTE_SERVICE_CONNECTION_TIMEOUT', RemoteServiceConnectionTimeout);
70
+
88
71
  export class DataCorruptionError extends SystemError {
89
72
  constructor(message?: string, context?: Record<string, any>) {
90
73
  super('DATA_CORRUPTION', message, context);
91
74
  }
92
75
  }
93
76
 
77
+ registerErrorMessageContext('DATA_CORRUPTION', DataCorruptionError);
78
+
94
79
  export class InvalidInvitationExtensionRoleError extends SystemError {
95
80
  constructor(message?: string, context?: Record<string, any>) {
96
81
  super('INVALID_INVITATION_EXTENSION_ROLE', message, context);
97
82
  }
98
83
  }
99
84
 
85
+ registerErrorMessageContext('INVALID_INVITATION_EXTENSION_ROLE', InvalidInvitationExtensionRoleError);
86
+
100
87
  export class IdentityNotInitializedError extends DatabaseError {
101
88
  constructor(message?: string, context?: Record<string, any>) {
102
89
  super('IDENTITY_NOT_INITIALIZED', message, context);
103
90
  }
104
91
  }
105
92
 
93
+ registerErrorMessageContext('IDENTITY_NOT_INITIALIZED', IdentityNotInitializedError);
94
+
106
95
  export class InvalidInvitationError extends DatabaseError {
107
96
  constructor(message?: string, context?: Record<string, any>) {
108
97
  super('INVALID_INVITATION', message, context);
109
98
  }
110
99
  }
111
100
 
101
+ registerErrorMessageContext('INVALID_INVITATION', InvalidInvitationError);
102
+
112
103
  export class ConnectionResetError extends BaseError {
113
104
  constructor(message?: string, context?: any) {
114
105
  super('CONNECTION_RESET', message, context);
115
106
  }
116
107
  }
117
108
 
109
+ registerErrorMessageContext('CONNECTION_RESET', ConnectionResetError);
110
+
118
111
  export class TimeoutError extends BaseError {
119
112
  constructor(message?: string, context?: any) {
120
113
  super('TIMEOUT', message, context);
121
114
  }
122
115
  }
123
116
 
117
+ registerErrorMessageContext('TIMEOUT', TimeoutError);
118
+
124
119
  // General protocol error.
125
120
  export class ProtocolError extends BaseError {
126
121
  constructor(message?: string, context?: any) {
@@ -128,6 +123,8 @@ export class ProtocolError extends BaseError {
128
123
  }
129
124
  }
130
125
 
126
+ registerErrorMessageContext('PROTOCOL_ERROR', ProtocolError);
127
+
131
128
  // General connectivity errors.
132
129
  export class ConnectivityError extends BaseError {
133
130
  constructor(message?: string, context?: any) {
@@ -135,32 +132,53 @@ export class ConnectivityError extends BaseError {
135
132
  }
136
133
  }
137
134
 
135
+ registerErrorMessageContext('CONNECTIVITY_ERROR', ConnectivityError);
136
+
138
137
  // TODO(nf): Rename? the protocol isn't what's unknown...
139
138
  export class UnknownProtocolError extends BaseError {
140
139
  constructor(message?: string, innerError?: Error) {
141
140
  super('UNKNOWN_PROTOCOL_ERROR', message, innerError);
142
141
  }
143
142
  }
143
+
144
+ registerErrorMessageContext('UNKNOWN_PROTOCOL_ERROR', UnknownProtocolError);
145
+
144
146
  export class InvalidStorageVersionError extends DatabaseError {
145
147
  constructor(expected: number, actual: number) {
146
148
  super('INVALID_STORAGE_VERSION', 'Invalid storage version.', { expected, actual });
147
149
  }
148
150
  }
149
151
 
152
+ registerError('INVALID_STORAGE_VERSION', (_, context) => {
153
+ return new InvalidStorageVersionError(context.expected ?? NaN, context.actual ?? NaN);
154
+ });
155
+
150
156
  export class SpaceNotFoundError extends DatabaseError {
151
157
  constructor(spaceKey: PublicKey) {
152
158
  super('SPACE_NOT_FOUND', 'Space not found.', { spaceKey });
153
159
  }
154
160
  }
155
161
 
162
+ registerError('SPACE_NOT_FOUND', (_, context) => {
163
+ return new SpaceNotFoundError(PublicKey.safeFrom(context.spaceKey) ?? PublicKey.from('00'));
164
+ });
165
+
156
166
  export class EntityNotFoundError extends DatabaseError {
157
167
  constructor(entityId: ItemID) {
158
168
  super('ITEM_NOT_FOUND', 'Item not found.', { entityId });
159
169
  }
160
170
  }
161
171
 
172
+ registerError('ITEM_NOT_FOUND', (_, context) => {
173
+ return new EntityNotFoundError(context.entityId);
174
+ });
175
+
162
176
  export class UnknownModelError extends DatabaseError {
163
177
  constructor(model: string) {
164
178
  super('UNKNOWN_MODEL', 'Unknown model.', { model });
165
179
  }
166
180
  }
181
+
182
+ registerError('UNKNOWN_MODEL', (_, context) => {
183
+ return new UnknownModelError(context.model);
184
+ });
@@ -3,30 +3,8 @@
3
3
  //
4
4
 
5
5
  import { invariant } from '@dxos/invariant';
6
- import { PublicKey } from '@dxos/keys';
7
6
 
8
- import {
9
- CancelledError,
10
- ConnectionResetError,
11
- ConnectivityError,
12
- DataCorruptionError,
13
- EntityNotFoundError,
14
- IdentityNotInitializedError,
15
- InvalidConfigError,
16
- InvalidInvitationError,
17
- InvalidInvitationExtensionRoleError,
18
- InvalidStorageVersionError,
19
- ProtocolError,
20
- RemoteServiceConnectionError,
21
- RemoteServiceConnectionTimeout,
22
- RpcClosedError,
23
- RpcNotOpenError,
24
- SpaceNotFoundError,
25
- SystemError,
26
- TimeoutError,
27
- UnknownModelError,
28
- UnknownProtocolError,
29
- } from './errors';
7
+ import { SystemError } from './base-errors';
30
8
  import { Error as SerializedErrorProto } from '../proto/gen/dxos/error';
31
9
 
32
10
  export const reconstructError = (error: SerializedErrorProto) => {
@@ -34,80 +12,28 @@ export const reconstructError = (error: SerializedErrorProto) => {
34
12
  return errorFromCode(name, message, context);
35
13
  };
36
14
 
37
- export const errorFromCode = (code?: string, message?: string, context?: any) => {
38
- switch (code) {
39
- case 'RPC_CLOSED':
40
- return new RpcClosedError();
41
-
42
- case 'RPC_NOT_OPEN':
43
- return new RpcNotOpenError();
44
-
45
- case 'CANCELLED':
46
- return new CancelledError(message, context);
47
-
48
- case 'INVALID_CONFIG':
49
- return new InvalidConfigError(message, context);
50
-
51
- case 'REMOTE_SERVICE_CONNECTION_ERROR':
52
- return new RemoteServiceConnectionError(message, context);
53
-
54
- case 'REMOTE_SERVICE_CONNECTION_TIMEOUT':
55
- return new RemoteServiceConnectionTimeout(message, context);
56
-
57
- case 'DATA_CORRUPTION':
58
- return new DataCorruptionError(message, context);
59
-
60
- case 'INVALID_INVITATION_EXTENSION_ROLE':
61
- return new InvalidInvitationExtensionRoleError(message, context);
62
-
63
- case 'CONNECTION_RESET':
64
- return new ConnectionResetError(message, context);
65
-
66
- case 'TIMEOUT':
67
- return new TimeoutError(message, context);
15
+ const errorRegistry = new Map<string, (message?: string, context?: any) => Error>();
68
16
 
69
- case 'PROTOCOL_ERROR':
70
- return new ProtocolError(message, context);
71
-
72
- case 'CONNECTIVITY_ERROR':
73
- return new ConnectivityError(message, context);
74
-
75
- case 'UNKNOWN_PROTOCOL_ERROR':
76
- return new UnknownProtocolError(message, context);
77
-
78
- case 'IDENTITY_NOT_INITIALIZED':
79
- return new IdentityNotInitializedError(message, context);
80
-
81
- case 'INVALID_INVITATION':
82
- return new InvalidInvitationError(message, context);
83
-
84
- case 'INVALID_STORAGE_VERSION': {
85
- const { expected, actual } = context;
86
- invariant(expected);
87
- invariant(actual);
88
- return new InvalidStorageVersionError(expected, actual);
89
- }
90
-
91
- case 'SPACE_NOT_FOUND': {
92
- const { spaceKey: key } = context;
93
- const spaceKey = PublicKey.safeFrom(key);
94
- invariant(spaceKey);
95
- return new SpaceNotFoundError(spaceKey);
96
- }
17
+ export const registerError = (code: string, make: (message?: string, context?: any) => Error) => {
18
+ invariant(!errorRegistry.has(code), `Error code already registered: ${code}`);
19
+ errorRegistry.set(code, make);
20
+ };
97
21
 
98
- case 'ITEM_NOT_FOUND': {
99
- const { entityId } = context;
100
- invariant(typeof entityId === 'string');
101
- return new EntityNotFoundError(entityId);
102
- }
22
+ export const registerErrorNoArgs = (code: string, Constructor: { new (): Error }) => {
23
+ registerError(code, () => new Constructor());
24
+ };
103
25
 
104
- case 'UNKNOWN_MODEL': {
105
- const { model } = context;
106
- invariant(typeof model === 'string');
107
- return new UnknownModelError(model);
108
- }
26
+ export const registerErrorMessageContext = (
27
+ code: string,
28
+ Constructor: { new (message?: string, context?: any): Error },
29
+ ) => {
30
+ registerError(code, (message?: string, context?: string) => new Constructor(message, context));
31
+ };
109
32
 
110
- default:
111
- return new SystemError(code ?? 'Error', message, context);
33
+ export const errorFromCode = (code?: string, message?: string, context?: any) => {
34
+ if (code && errorRegistry.has(code)) {
35
+ return errorRegistry.get(code)!(message, context);
36
+ } else {
37
+ return new SystemError(code ?? 'Error', message, context);
112
38
  }
113
39
  };
@@ -5,3 +5,4 @@
5
5
  export * from './encoding';
6
6
  export * from './errors';
7
7
  export * from './helpers';
8
+ export * from './base-errors';
@@ -10,6 +10,7 @@ option go_package = "github.com/dxos/kube/proto/def/dxos/error";
10
10
 
11
11
  /// Serialized error.
12
12
  message Error {
13
+ /// Also error code. Will be used to reconstruct a known error type.
13
14
  optional string name = 1;
14
15
  optional string message = 2;
15
16
  optional google.protobuf.Struct context = 4;
@@ -61,6 +61,8 @@ import * as google_protobuf from "../google/protobuf";
61
61
  */
62
62
  export interface Error {
63
63
  /**
64
+ * Also error code. Will be used to reconstruct a known error type.
65
+ *
64
66
  * Options:
65
67
  * - proto3_optional = true
66
68
  */