jazz-tools 0.19.7 → 0.19.8

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 (110) hide show
  1. package/.turbo/turbo-build.log +61 -57
  2. package/CHANGELOG.md +15 -0
  3. package/dist/{chunk-CUS6O5NE.js → chunk-2S3Z2CN6.js} +116 -49
  4. package/dist/chunk-2S3Z2CN6.js.map +1 -0
  5. package/dist/expo/polyfills.js +22 -0
  6. package/dist/expo/polyfills.js.map +1 -0
  7. package/dist/index.js +26 -6
  8. package/dist/index.js.map +1 -1
  9. package/dist/react-core/hooks.d.ts.map +1 -1
  10. package/dist/react-core/index.js +17 -6
  11. package/dist/react-core/index.js.map +1 -1
  12. package/dist/react-native/index.d.ts +1 -1
  13. package/dist/react-native/index.d.ts.map +1 -1
  14. package/dist/react-native/index.js +713 -9
  15. package/dist/react-native/index.js.map +1 -1
  16. package/dist/react-native/polyfills.js +22 -0
  17. package/dist/react-native/polyfills.js.map +1 -0
  18. package/dist/react-native-core/index.d.ts.map +1 -1
  19. package/dist/react-native-core/index.js.map +1 -1
  20. package/dist/testing.js +1 -1
  21. package/dist/tools/coValues/account.d.ts +3 -3
  22. package/dist/tools/coValues/account.d.ts.map +1 -1
  23. package/dist/tools/coValues/coFeed.d.ts +3 -3
  24. package/dist/tools/coValues/coFeed.d.ts.map +1 -1
  25. package/dist/tools/coValues/coList.d.ts +4 -4
  26. package/dist/tools/coValues/coList.d.ts.map +1 -1
  27. package/dist/tools/coValues/coMap.d.ts +7 -7
  28. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  29. package/dist/tools/coValues/coPlainText.d.ts +2 -2
  30. package/dist/tools/coValues/coPlainText.d.ts.map +1 -1
  31. package/dist/tools/coValues/coVector.d.ts +2 -2
  32. package/dist/tools/coValues/coVector.d.ts.map +1 -1
  33. package/dist/tools/coValues/deepLoading.d.ts +24 -0
  34. package/dist/tools/coValues/deepLoading.d.ts.map +1 -1
  35. package/dist/tools/coValues/group.d.ts +2 -2
  36. package/dist/tools/coValues/group.d.ts.map +1 -1
  37. package/dist/tools/coValues/interfaces.d.ts +6 -6
  38. package/dist/tools/coValues/interfaces.d.ts.map +1 -1
  39. package/dist/tools/coValues/schemaUnion.d.ts +2 -2
  40. package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
  41. package/dist/tools/config.d.ts +3 -0
  42. package/dist/tools/config.d.ts.map +1 -0
  43. package/dist/tools/exports.d.ts +2 -0
  44. package/dist/tools/exports.d.ts.map +1 -1
  45. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +2 -2
  46. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
  47. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +2 -2
  48. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -1
  49. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +2 -2
  50. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  51. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +4 -4
  52. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  53. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +4 -4
  54. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  55. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +4 -4
  56. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  57. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +2 -2
  58. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  59. package/dist/tools/implementation/zodSchema/schemaTypes/GroupSchema.d.ts +2 -2
  60. package/dist/tools/implementation/zodSchema/schemaTypes/GroupSchema.d.ts.map +1 -1
  61. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +2 -2
  62. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
  63. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +2 -2
  64. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
  65. package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
  66. package/dist/tools/subscribe/JazzError.d.ts.map +1 -1
  67. package/dist/tools/subscribe/SubscriptionScope.d.ts +10 -1
  68. package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
  69. package/dist/tools/subscribe/errorReporting.d.ts +31 -0
  70. package/dist/tools/subscribe/errorReporting.d.ts.map +1 -0
  71. package/dist/tools/testing.d.ts.map +1 -1
  72. package/dist/tools/tests/errorReporting.test.d.ts +2 -0
  73. package/dist/tools/tests/errorReporting.test.d.ts.map +1 -0
  74. package/package.json +13 -5
  75. package/src/react-core/hooks.ts +16 -0
  76. package/src/react-native/index.ts +1 -1
  77. package/src/react-native-core/index.ts +2 -0
  78. package/src/react-native-core/polyfills/index.js +28 -0
  79. package/src/tools/coValues/account.ts +3 -4
  80. package/src/tools/coValues/coFeed.ts +3 -2
  81. package/src/tools/coValues/coList.ts +4 -4
  82. package/src/tools/coValues/coMap.ts +4 -4
  83. package/src/tools/coValues/coPlainText.ts +2 -2
  84. package/src/tools/coValues/coVector.ts +2 -2
  85. package/src/tools/coValues/deepLoading.ts +31 -0
  86. package/src/tools/coValues/group.ts +2 -2
  87. package/src/tools/coValues/interfaces.ts +19 -23
  88. package/src/tools/coValues/schemaUnion.ts +2 -2
  89. package/src/tools/config.ts +9 -0
  90. package/src/tools/exports.ts +4 -0
  91. package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +2 -2
  92. package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +2 -2
  93. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +2 -2
  94. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +4 -4
  95. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +4 -4
  96. package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +4 -10
  97. package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +2 -2
  98. package/src/tools/implementation/zodSchema/schemaTypes/GroupSchema.ts +2 -2
  99. package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +2 -2
  100. package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +2 -2
  101. package/src/tools/subscribe/JazzError.ts +9 -6
  102. package/src/tools/subscribe/SubscriptionScope.ts +110 -27
  103. package/src/tools/subscribe/errorReporting.ts +67 -0
  104. package/src/tools/tests/deepLoading.test.ts +47 -47
  105. package/src/tools/tests/errorReporting.test.ts +103 -0
  106. package/src/tools/tests/load.test.ts +21 -1
  107. package/src/tools/tests/request.test.ts +2 -1
  108. package/src/tools/tests/subscribe.test.ts +44 -0
  109. package/tsup.config.ts +16 -0
  110. package/dist/chunk-CUS6O5NE.js.map +0 -1
@@ -24,6 +24,10 @@ import type {
24
24
  } from "./types.js";
25
25
  import { CoValueLoadingState, NotLoadedCoValueState } from "./types.js";
26
26
  import { createCoValue, myRoleForRawValue } from "./utils.js";
27
+ import {
28
+ captureError,
29
+ isCustomErrorReportingEnabled,
30
+ } from "./errorReporting.js";
27
31
 
28
32
  export class SubscriptionScope<D extends CoValue> {
29
33
  childNodes = new Map<string, SubscriptionScope<CoValue>>();
@@ -58,6 +62,13 @@ export class SubscriptionScope<D extends CoValue> {
58
62
 
59
63
  silenceUpdates = false;
60
64
 
65
+ /**
66
+ * Stack trace captured at subscription creation time.
67
+ * This helps identify which component/hook created the subscription
68
+ * when debugging "value unavailable" errors.
69
+ */
70
+ callerStack: Error | undefined;
71
+
61
72
  constructor(
62
73
  public node: LocalNode,
63
74
  resolve: RefsToResolve<D>,
@@ -66,7 +77,10 @@ export class SubscriptionScope<D extends CoValue> {
66
77
  public skipRetry = false,
67
78
  public bestEffortResolution = false,
68
79
  public unstable_branch?: BranchDefinition,
80
+ callerStack?: Error | undefined,
69
81
  ) {
82
+ // Use caller stack if provided, otherwise capture here (less useful but better than nothing)
83
+ this.callerStack = callerStack;
70
84
  this.resolve = resolve;
71
85
  this.value = { type: CoValueLoadingState.LOADING, id };
72
86
 
@@ -126,37 +140,40 @@ export class SubscriptionScope<D extends CoValue> {
126
140
  handleUpdate(update: RawCoValue | typeof CoValueLoadingState.UNAVAILABLE) {
127
141
  if (update === CoValueLoadingState.UNAVAILABLE) {
128
142
  if (this.value.type === CoValueLoadingState.LOADING) {
129
- this.updateValue(
130
- new JazzError(this.id, CoValueLoadingState.UNAVAILABLE, [
131
- {
132
- code: CoValueLoadingState.UNAVAILABLE,
133
- message: "The value is unavailable",
134
- params: {
135
- id: this.id,
136
- },
137
- path: [],
143
+ const error = new JazzError(this.id, CoValueLoadingState.UNAVAILABLE, [
144
+ {
145
+ code: CoValueLoadingState.UNAVAILABLE,
146
+ message: `Jazz Unavailable Error: unable to load ${this.id}`,
147
+ params: {
148
+ id: this.id,
138
149
  },
139
- ]),
140
- );
150
+ path: [],
151
+ },
152
+ ]);
153
+
154
+ this.updateValue(error);
141
155
  }
156
+
142
157
  this.triggerUpdate();
143
158
  return;
144
159
  }
145
160
 
146
161
  if (!hasAccessToCoValue(update)) {
147
162
  if (this.value.type !== CoValueLoadingState.UNAUTHORIZED) {
148
- this.updateValue(
149
- new JazzError(this.id, CoValueLoadingState.UNAUTHORIZED, [
150
- {
151
- code: CoValueLoadingState.UNAUTHORIZED,
152
- message: `The current user (${this.node.getCurrentAgent().id}) is not authorized to access this value`,
153
- params: {
154
- id: this.id,
155
- },
156
- path: [],
163
+ const message = `Jazz Authorization Error: The current user (${this.node.getCurrentAgent().id}) is not authorized to access ${this.id}`;
164
+
165
+ const error = new JazzError(this.id, CoValueLoadingState.UNAUTHORIZED, [
166
+ {
167
+ code: CoValueLoadingState.UNAUTHORIZED,
168
+ message,
169
+ params: {
170
+ id: this.id,
157
171
  },
158
- ]),
159
- );
172
+ path: [],
173
+ },
174
+ ]);
175
+
176
+ this.updateValue(error);
160
177
  this.triggerUpdate();
161
178
  }
162
179
  return;
@@ -296,6 +313,8 @@ export class SubscriptionScope<D extends CoValue> {
296
313
  return unloadedValue;
297
314
  }
298
315
 
316
+ lastErrorLogged: JazzError | undefined;
317
+
299
318
  getCurrentValue(): MaybeLoaded<D> {
300
319
  const rawValue = this.getCurrentRawValue();
301
320
 
@@ -304,6 +323,7 @@ export class SubscriptionScope<D extends CoValue> {
304
323
  rawValue === CoValueLoadingState.UNAVAILABLE ||
305
324
  rawValue === CoValueLoadingState.LOADING
306
325
  ) {
326
+ this.logError();
307
327
  return this.getUnloadedValue(rawValue);
308
328
  }
309
329
 
@@ -315,7 +335,6 @@ export class SubscriptionScope<D extends CoValue> {
315
335
  this.value.type === CoValueLoadingState.UNAUTHORIZED ||
316
336
  this.value.type === CoValueLoadingState.UNAVAILABLE
317
337
  ) {
318
- console.error(this.value.toString());
319
338
  return this.value.type;
320
339
  }
321
340
 
@@ -324,7 +343,6 @@ export class SubscriptionScope<D extends CoValue> {
324
343
  }
325
344
 
326
345
  if (this.errorFromChildren) {
327
- console.error(this.errorFromChildren.toString());
328
346
  return this.errorFromChildren.type;
329
347
  }
330
348
 
@@ -335,6 +353,71 @@ export class SubscriptionScope<D extends CoValue> {
335
353
  return CoValueLoadingState.LOADING;
336
354
  }
337
355
 
356
+ getCreationStackLines() {
357
+ const stack = this.callerStack?.stack;
358
+
359
+ if (!stack) {
360
+ return "";
361
+ }
362
+
363
+ const creationStackLines = stack.split("\n").slice(2, 15);
364
+ const creationAppFrame = creationStackLines.find(
365
+ (line) =>
366
+ !line.includes("node_modules") &&
367
+ !line.includes("useCoValueSubscription") &&
368
+ !line.includes("useCoState") &&
369
+ !line.includes("useAccount") &&
370
+ !line.includes("jazz-tools"),
371
+ );
372
+
373
+ let result = "\n\n";
374
+
375
+ if (creationAppFrame) {
376
+ (result += "Subscription created "), (result += creationAppFrame.trim());
377
+ }
378
+
379
+ result += "\nFull subscription creation stack:";
380
+ for (const line of creationStackLines.slice(0, 8)) {
381
+ result += "\n " + line.trim();
382
+ }
383
+
384
+ return result;
385
+ }
386
+
387
+ logError() {
388
+ let error: JazzError | undefined;
389
+
390
+ if (
391
+ this.value.type === CoValueLoadingState.UNAUTHORIZED ||
392
+ this.value.type === CoValueLoadingState.UNAVAILABLE
393
+ ) {
394
+ error = this.value;
395
+ }
396
+
397
+ if (this.errorFromChildren) {
398
+ error = this.errorFromChildren;
399
+ }
400
+
401
+ if (!error || this.lastErrorLogged === error) {
402
+ return;
403
+ }
404
+
405
+ if (error.type === CoValueLoadingState.UNAVAILABLE && this.skipRetry) {
406
+ return;
407
+ }
408
+
409
+ this.lastErrorLogged = error;
410
+
411
+ if (isCustomErrorReportingEnabled()) {
412
+ captureError(new Error(error.toString(), { cause: this.callerStack }), {
413
+ getPrettyStackTrace: () => this.getCreationStackLines(),
414
+ jazzError: error,
415
+ });
416
+ } else {
417
+ console.error(`${error.toString()}${this.getCreationStackLines()}`);
418
+ }
419
+ }
420
+
338
421
  triggerUpdate() {
339
422
  if (!this.shouldSendUpdates()) return;
340
423
  if (!this.dirty) return;
@@ -577,7 +660,7 @@ export class SubscriptionScope<D extends CoValue> {
577
660
  new JazzError(undefined, CoValueLoadingState.UNAVAILABLE, [
578
661
  {
579
662
  code: "validationError",
580
- message: `The ref on position ${key} requested on ${stream.constructor.name} is missing`,
663
+ message: `Jazz Validation Error: The ref on position ${key} is missing`,
581
664
  params: {},
582
665
  path: [key],
583
666
  },
@@ -642,7 +725,7 @@ export class SubscriptionScope<D extends CoValue> {
642
725
  new JazzError(undefined, CoValueLoadingState.UNAVAILABLE, [
643
726
  {
644
727
  code: "validationError",
645
- message: `The ref ${key} requested on ${map.constructor.name} is missing`,
728
+ message: `Jazz Validation Error: The ref ${key} is required but missing`,
646
729
  params: {},
647
730
  path: [key],
648
731
  },
@@ -681,7 +764,7 @@ export class SubscriptionScope<D extends CoValue> {
681
764
  new JazzError(undefined, CoValueLoadingState.UNAVAILABLE, [
682
765
  {
683
766
  code: "validationError",
684
- message: `The ref on position ${key} requested on ${list.constructor.name} is missing`,
767
+ message: `Jazz Validation Error: The ref on position ${key} is required but missing`,
685
768
  params: {},
686
769
  path: [key],
687
770
  },
@@ -0,0 +1,67 @@
1
+ import type { JazzError } from "./JazzError";
2
+
3
+ /**
4
+ * A platform agnostic way to check if we're in development mode
5
+ *
6
+ * Works in Node.js and bundled code, falls back to false if process is not available
7
+ */
8
+ const isDev = (function () {
9
+ try {
10
+ return process.env.NODE_ENV === "development";
11
+ } catch {
12
+ return false;
13
+ }
14
+ })();
15
+
16
+ type CustomErrorReporterProps = {
17
+ getPrettyStackTrace: () => string;
18
+ jazzError: JazzError;
19
+ };
20
+
21
+ type CustomErrorReporter = (
22
+ error: Error,
23
+ props: CustomErrorReporterProps,
24
+ ) => void;
25
+
26
+ let customErrorReporter: CustomErrorReporter | undefined;
27
+ let captureErrorCause: boolean = isDev;
28
+
29
+ /**
30
+ * Turns on the additonal debug info coming from React hooks on the original subscription of the errors.
31
+ *
32
+ * Enabled by default in development mode.
33
+ */
34
+ export function enableCaptureErrorCause(capture: boolean) {
35
+ captureErrorCause = capture;
36
+ }
37
+
38
+ /**
39
+ * Set a custom error reporter to be used instead of the default console.error.
40
+ *
41
+ * Useful for sending errors to a logging service or silence some annoying errors in production.
42
+ */
43
+ export function setCustomErrorReporter(reporter?: CustomErrorReporter) {
44
+ customErrorReporter = reporter;
45
+ }
46
+
47
+ /**
48
+ * Check if we're in development mode.
49
+ * Stack traces are only captured in development to avoid overhead in production.
50
+ */
51
+ export function isCustomErrorReportingEnabled(): boolean {
52
+ return customErrorReporter !== undefined;
53
+ }
54
+
55
+ /**
56
+ * Capture a stack trace only in development mode.
57
+ * Returns undefined in production to avoid overhead.
58
+ */
59
+ export function captureStack() {
60
+ return captureErrorCause ? new Error() : undefined;
61
+ }
62
+
63
+ export function captureError(error: Error, props: CustomErrorReporterProps) {
64
+ if (customErrorReporter) {
65
+ customErrorReporter(error, props);
66
+ }
67
+ }
@@ -1,6 +1,14 @@
1
1
  import { cojsonInternals } from "cojson";
2
2
  import { WasmCrypto } from "cojson/crypto/WasmCrypto";
3
- import { assert, describe, expect, expectTypeOf, test, vi } from "vitest";
3
+ import {
4
+ assert,
5
+ beforeEach,
6
+ describe,
7
+ expect,
8
+ expectTypeOf,
9
+ test,
10
+ vi,
11
+ } from "vitest";
4
12
  import {
5
13
  Group,
6
14
  ID,
@@ -11,17 +19,17 @@ import {
11
19
  } from "../index.js";
12
20
  import {
13
21
  Account,
14
- CoList,
15
22
  Loaded,
16
23
  MaybeLoaded,
17
- NotLoaded,
24
+ Settled,
18
25
  co,
19
26
  randomSessionProvider,
20
27
  CoValueLoadingState,
21
- NotLoadedCoValueState,
28
+ CoValueErrorState,
22
29
  } from "../internal.js";
23
30
  import { createJazzTestAccount, linkAccounts } from "../testing.js";
24
31
  import { assertLoaded, waitFor } from "./utils.js";
32
+ import { setCustomErrorReporter } from "../config.js";
25
33
 
26
34
  const Crypto = await WasmCrypto.create();
27
35
  const { connectedPeers } = cojsonInternals;
@@ -43,6 +51,15 @@ const TestMap = co.map({
43
51
  optionalRef: co.optional(InnermostMap),
44
52
  });
45
53
 
54
+ let lastError: Error | undefined;
55
+
56
+ beforeEach(() => {
57
+ lastError = undefined;
58
+ setCustomErrorReporter((error) => {
59
+ lastError = error;
60
+ });
61
+ });
62
+
46
63
  describe("Deep loading with depth arg", async () => {
47
64
  const me = await Account.create({
48
65
  creationProps: { name: "Hermes Puggington" },
@@ -345,7 +362,7 @@ test("Deep loading a record-like coMap", async () => {
345
362
  },
346
363
  });
347
364
  expectTypeOf(recordLoaded).branded.toEqualTypeOf<
348
- MaybeLoaded<
365
+ Settled<
349
366
  Loaded<typeof RecordLike> & {
350
367
  readonly [key: string]: Loaded<typeof TestMap> & {
351
368
  readonly list: Loaded<typeof TestList> &
@@ -494,17 +511,14 @@ describe("Deep loading with unauthorized account", async () => {
494
511
  CoValueLoadingState.UNAUTHORIZED,
495
512
  );
496
513
 
497
- expect(errorSpy).toHaveBeenCalledWith(
498
- `The current user (${alice.$jazz.id}) is not authorized to access this value from ${map.$jazz.id}`,
514
+ expect(lastError?.message).toBe(
515
+ `Jazz Authorization Error: The current user (${alice.$jazz.id}) is not authorized to access ${map.$jazz.id}`,
499
516
  );
500
-
501
- errorSpy.mockReset();
502
517
  });
503
518
 
504
519
  test("unaccessible list", async () => {
505
- const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
506
-
507
- const map = TestMap.create({ list: TestList.create([], onlyBob) }, group);
520
+ const innerList = TestList.create([], onlyBob);
521
+ const map = TestMap.create({ list: innerList }, group);
508
522
 
509
523
  const mapOnAlice = await TestMap.load(map.$jazz.id, { loadAs: alice });
510
524
  expect(mapOnAlice).toBeTruthy();
@@ -518,16 +532,12 @@ describe("Deep loading with unauthorized account", async () => {
518
532
  CoValueLoadingState.UNAUTHORIZED,
519
533
  );
520
534
 
521
- expect(errorSpy).toHaveBeenCalledWith(
522
- `The current user (${alice.$jazz.id}) is not authorized to access this value from ${map.$jazz.id} on path list`,
535
+ expect(lastError?.message).toBe(
536
+ `Jazz Authorization Error: The current user (${alice.$jazz.id}) is not authorized to access ${innerList.$jazz.id}. Subscription starts from ${map.$jazz.id} and the value is on path list`,
523
537
  );
524
-
525
- errorSpy.mockReset();
526
538
  });
527
539
 
528
540
  test("unaccessible list element", async () => {
529
- const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
530
-
531
541
  const map = TestMap.create(
532
542
  {
533
543
  list: TestList.create(
@@ -554,16 +564,12 @@ describe("Deep loading with unauthorized account", async () => {
554
564
  CoValueLoadingState.UNAUTHORIZED,
555
565
  );
556
566
 
557
- expect(errorSpy).toHaveBeenCalledWith(
558
- `The current user (${alice.$jazz.id}) is not authorized to access this value from ${map.$jazz.id} on path list.0`,
567
+ expect(lastError?.message).toBe(
568
+ `Jazz Authorization Error: The current user (${alice.$jazz.id}) is not authorized to access ${map.list[0]!.$jazz.id}. Subscription starts from ${map.$jazz.id} and the value is on path list.0`,
559
569
  );
560
-
561
- errorSpy.mockReset();
562
570
  });
563
571
 
564
572
  test("unaccessible optional element", async () => {
565
- const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
566
-
567
573
  const map = TestMap.create(
568
574
  {
569
575
  list: TestList.create([], group),
@@ -579,11 +585,9 @@ describe("Deep loading with unauthorized account", async () => {
579
585
  expect(mapOnAlice.$jazz.loadingState).toBe(
580
586
  CoValueLoadingState.UNAUTHORIZED,
581
587
  );
582
- expect(errorSpy).toHaveBeenCalledWith(
583
- `The current user (${alice.$jazz.id}) is not authorized to access this value from ${map.$jazz.id} on path optionalRef`,
588
+ expect(lastError?.message).toBe(
589
+ `Jazz Authorization Error: The current user (${alice.$jazz.id}) is not authorized to access ${map.optionalRef!.$jazz.id}. Subscription starts from ${map.$jazz.id} and the value is on path optionalRef`,
584
590
  );
585
-
586
- errorSpy.mockReset();
587
591
  });
588
592
 
589
593
  test("unaccessible optional element via autoload", async () => {
@@ -611,17 +615,20 @@ describe("Deep loading with unauthorized account", async () => {
611
615
  });
612
616
 
613
617
  expect(result?.$jazz.loadingState).toBe(CoValueLoadingState.UNAUTHORIZED);
618
+ expect(lastError?.message).toBe(
619
+ `Jazz Authorization Error: The current user (${alice.$jazz.id}) is not authorized to access ${map.optionalRef!.$jazz.id}`,
620
+ );
614
621
  });
615
622
 
616
623
  test("unaccessible stream", async () => {
617
- const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
624
+ const stream = TestFeed.create([], onlyBob);
618
625
  const map = TestMap.create(
619
626
  {
620
627
  list: TestList.create(
621
628
  [
622
629
  InnerMap.create(
623
630
  {
624
- stream: TestFeed.create([], onlyBob),
631
+ stream,
625
632
  },
626
633
  group,
627
634
  ),
@@ -641,16 +648,12 @@ describe("Deep loading with unauthorized account", async () => {
641
648
  CoValueLoadingState.UNAUTHORIZED,
642
649
  );
643
650
 
644
- expect(errorSpy).toHaveBeenCalledWith(
645
- `The current user (${alice.$jazz.id}) is not authorized to access this value from ${map.$jazz.id} on path list.0.stream`,
651
+ expect(lastError?.message).toBe(
652
+ `Jazz Authorization Error: The current user (${alice.$jazz.id}) is not authorized to access ${stream.$jazz.id}. Subscription starts from ${map.$jazz.id} and the value is on path list.0.stream`,
646
653
  );
647
-
648
- errorSpy.mockReset();
649
654
  });
650
655
 
651
656
  test("unaccessible stream element", async () => {
652
- const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
653
-
654
657
  const value = InnermostMap.create({ value: "hello" }, onlyBob);
655
658
 
656
659
  const map = TestMap.create(
@@ -679,11 +682,9 @@ describe("Deep loading with unauthorized account", async () => {
679
682
  CoValueLoadingState.UNAUTHORIZED,
680
683
  );
681
684
 
682
- expect(errorSpy).toHaveBeenCalledWith(
683
- `The current user (${alice.$jazz.id}) is not authorized to access this value from ${map.$jazz.id} on path list.0.stream.${value.$jazz.id}`,
685
+ expect(lastError?.message).toBe(
686
+ `Jazz Authorization Error: The current user (${alice.$jazz.id}) is not authorized to access ${value.$jazz.id}. Subscription starts from ${map.$jazz.id} and the value is on path list.0.stream.${value.$jazz.id}`,
684
687
  );
685
-
686
- errorSpy.mockReset();
687
688
  });
688
689
 
689
690
  test("setting undefined via proxy", async () => {
@@ -1229,7 +1230,7 @@ describe("$isLoaded", async () => {
1229
1230
 
1230
1231
  const map = TestMap.create({ list: [] }, { owner: me });
1231
1232
 
1232
- test("$isLoaded narrows MaybeLoaded to loaded CoValue", async () => {
1233
+ test("$isLoaded narrows a maybe-loaded CoValue to a loaded CoValue", async () => {
1233
1234
  const maybeLoadedMap = await TestMap.load(map.$jazz.id, {
1234
1235
  loadAs: me,
1235
1236
  });
@@ -1244,19 +1245,18 @@ describe("$isLoaded", async () => {
1244
1245
  } else {
1245
1246
  expectTypeOf(
1246
1247
  maybeLoadedMap.$jazz.loadingState,
1247
- ).toEqualTypeOf<NotLoadedCoValueState>();
1248
+ ).toEqualTypeOf<CoValueErrorState>();
1248
1249
  }
1249
1250
  });
1250
1251
 
1251
- test("$isLoaded narrows MaybeLoaded to not loaded CoValue", async () => {
1252
+ test("$isLoaded narrows a maybe-loaded CoValue to a not loaded CoValue", async () => {
1252
1253
  const otherAccount = await Account.create({
1253
1254
  creationProps: { name: "Other Account" },
1254
1255
  crypto: Crypto,
1255
1256
  });
1256
- const unloadedMap: MaybeLoaded<Loaded<typeof TestMap>> = await TestMap.load(
1257
- map.$jazz.id,
1258
- { loadAs: otherAccount },
1259
- );
1257
+ const unloadedMap = await TestMap.load(map.$jazz.id, {
1258
+ loadAs: otherAccount,
1259
+ });
1260
1260
 
1261
1261
  expect(unloadedMap.$isLoaded).toBe(false);
1262
1262
  if (!unloadedMap.$isLoaded) {
@@ -0,0 +1,103 @@
1
+ import { beforeEach, describe, expect, test, vi } from "vitest";
2
+ import { Group, z } from "../index.js";
3
+ import { setCustomErrorReporter } from "../config.js";
4
+ import { CoValueLoadingState } from "../internal.js";
5
+ import { createJazzTestAccount, linkAccounts } from "../testing.js";
6
+ import { co } from "../exports.js";
7
+
8
+ const TestMap = co.map({
9
+ value: z.string(),
10
+ });
11
+
12
+ describe("Custom error reporter", () => {
13
+ beforeEach(() => {
14
+ // Clean up error reporter before each test
15
+ setCustomErrorReporter(undefined);
16
+ });
17
+
18
+ test("with custom error reporter enabled, console.error is not called", async () => {
19
+ const bob = await createJazzTestAccount({
20
+ creationProps: { name: "Bob" },
21
+ });
22
+
23
+ const alice = await createJazzTestAccount({
24
+ creationProps: { name: "Alice" },
25
+ });
26
+
27
+ linkAccounts(bob, alice);
28
+
29
+ await alice.$jazz.waitForAllCoValuesSync();
30
+
31
+ const onlyBob = bob;
32
+ const group = Group.create(bob);
33
+
34
+ group.addMember(alice, "reader");
35
+
36
+ let capturedError: Error | undefined;
37
+ let capturedProps: { jazzError: any } | undefined;
38
+
39
+ setCustomErrorReporter((error, props) => {
40
+ capturedError = error;
41
+ capturedProps = props;
42
+ });
43
+
44
+ const consoleErrorSpy = vi
45
+ .spyOn(console, "error")
46
+ .mockImplementation(() => {});
47
+
48
+ const map = TestMap.create({ value: "hello" }, onlyBob);
49
+
50
+ const mapOnAlice = await TestMap.load(map.$jazz.id, { loadAs: alice });
51
+
52
+ // Access the value to trigger error logging
53
+ expect(mapOnAlice.$jazz.loadingState).toBe(
54
+ CoValueLoadingState.UNAUTHORIZED,
55
+ );
56
+
57
+ expect(consoleErrorSpy).not.toHaveBeenCalled();
58
+ expect(capturedError).toBeDefined();
59
+ expect(capturedProps).toBeDefined();
60
+ expect(capturedProps?.jazzError).toBeDefined();
61
+
62
+ consoleErrorSpy.mockRestore();
63
+ });
64
+
65
+ test("without custom error reporter, console.error is called", async () => {
66
+ const bob = await createJazzTestAccount({
67
+ creationProps: { name: "Bob" },
68
+ });
69
+
70
+ const alice = await createJazzTestAccount({
71
+ creationProps: { name: "Alice" },
72
+ });
73
+
74
+ linkAccounts(bob, alice);
75
+
76
+ await alice.$jazz.waitForAllCoValuesSync();
77
+
78
+ const onlyBob = bob;
79
+ const group = Group.create(bob);
80
+
81
+ group.addMember(alice, "reader");
82
+
83
+ // Ensure no custom error reporter is set
84
+ setCustomErrorReporter(undefined);
85
+
86
+ const consoleErrorSpy = vi
87
+ .spyOn(console, "error")
88
+ .mockImplementation(() => {});
89
+
90
+ const map = TestMap.create({ value: "hello" }, onlyBob);
91
+
92
+ const mapOnAlice = await TestMap.load(map.$jazz.id, { loadAs: alice });
93
+
94
+ // Access the value to trigger error logging
95
+ expect(mapOnAlice.$jazz.loadingState).toBe(
96
+ CoValueLoadingState.UNAUTHORIZED,
97
+ );
98
+
99
+ expect(consoleErrorSpy).toHaveBeenCalled();
100
+
101
+ consoleErrorSpy.mockRestore();
102
+ });
103
+ });
@@ -1,6 +1,13 @@
1
1
  import { cojsonInternals, emptyKnownState } from "cojson";
2
2
  import { assert, beforeEach, expect, test } from "vitest";
3
- import { Account, Group, co, exportCoValue, z } from "../exports.js";
3
+ import {
4
+ Account,
5
+ Group,
6
+ co,
7
+ exportCoValue,
8
+ jazzConfig,
9
+ z,
10
+ } from "../exports.js";
4
11
  import { CoValueLoadingState } from "../internal.js";
5
12
  import {
6
13
  createJazzTestAccount,
@@ -12,6 +19,13 @@ import { assertLoaded, waitFor } from "./utils.js";
12
19
 
13
20
  cojsonInternals.CO_VALUE_LOADING_CONFIG.RETRY_DELAY = 10;
14
21
 
22
+ let lastError: Error | undefined;
23
+ beforeEach(() => {
24
+ lastError = undefined;
25
+ jazzConfig.setCustomErrorReporter((error) => {
26
+ lastError = error;
27
+ });
28
+ });
15
29
  beforeEach(async () => {
16
30
  await setupJazzTestSync();
17
31
  await createJazzTestAccount({
@@ -42,6 +56,9 @@ test("return 'unavailable' if id is invalid", async () => {
42
56
 
43
57
  const john = await Person.load("test");
44
58
  expect(john.$jazz.loadingState).toBe(CoValueLoadingState.UNAVAILABLE);
59
+ expect(lastError?.message).toBe(
60
+ "Jazz Unavailable Error: unable to load test",
61
+ );
45
62
  });
46
63
 
47
64
  test("load a missing optional value (co.optional)", async () => {
@@ -185,6 +202,9 @@ test("returns 'unavailable' if the value is unavailable after retries", async ()
185
202
  const john = await Person.load(map.$jazz.id, { loadAs: alice });
186
203
 
187
204
  expect(john.$jazz.loadingState).toBe(CoValueLoadingState.UNAVAILABLE);
205
+ expect(lastError?.message).toBe(
206
+ "Jazz Unavailable Error: unable to load " + map.$jazz.id,
207
+ );
188
208
  });
189
209
 
190
210
  test("load works even when the coValue access is granted after the creation", async () => {
@@ -14,6 +14,7 @@ import {
14
14
  CoValueLoadingState,
15
15
  exportCoValue,
16
16
  importContentPieces,
17
+ Inaccessible,
17
18
  } from "../internal.js";
18
19
  import { createJazzTestAccount, linkAccounts } from "../testing.js";
19
20
 
@@ -663,7 +664,7 @@ describe("JazzRequestError handling", () => {
663
664
  createUnloadedCoValue(
664
665
  "some-covalue-id",
665
666
  CoValueLoadingState.UNAVAILABLE,
666
- ),
667
+ ) as Inaccessible<Account>,
667
668
  );
668
669
 
669
670
  return userRequest.handle(request, worker, async () => {