@johnhenry/browsermesh-apps 0.0.0

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.
@@ -0,0 +1,55 @@
1
+ /**
2
+ * silent-catch.mjs — debug-gated structured logger for the
3
+ * `try { … } catch { /* ignore *\/ }` pattern.
4
+ *
5
+ * Vendored from clawser-silent-catch.mjs (fully standalone, no other
6
+ * clawser-app imports) so this package doesn't need to reach into web/.
7
+ *
8
+ * Replaces silent catches with an opt-in inspectable record. Default
9
+ * behaviour is unchanged (silent), so this is a drop-in replacement.
10
+ * Users surface the events by enabling debug mode:
11
+ *
12
+ * localStorage.setItem('clawser_debug', 'true') // persistent
13
+ * // or, in DevTools: clawserDebug.enable()
14
+ */
15
+
16
+ let enabled = false;
17
+ try {
18
+ if (typeof localStorage !== 'undefined') {
19
+ enabled = localStorage.getItem('clawser_debug') === 'true';
20
+ }
21
+ } catch {
22
+ // localStorage may throw in privacy / sandbox contexts — treat as disabled
23
+ }
24
+
25
+ /** Re-check the localStorage flag; called by clawserDebug.enable/disable. */
26
+ export function refreshSilentCatchState() {
27
+ try {
28
+ if (typeof localStorage !== 'undefined') {
29
+ enabled = localStorage.getItem('clawser_debug') === 'true';
30
+ }
31
+ } catch {
32
+ enabled = false;
33
+ }
34
+ }
35
+
36
+ /**
37
+ * Log a silent-catch event. No-op unless debug mode is enabled.
38
+ *
39
+ * @param {string} module — file/component (e.g. 'clawser-pod')
40
+ * @param {string} operation — what was attempted (e.g. 'relay-disconnect')
41
+ * @param {*} error — the caught value
42
+ * @param {object} [context] — extra structured fields
43
+ */
44
+ export function silentCatch(module, operation, error, context) {
45
+ // Re-read the flag each call so toggles in DevTools take effect without
46
+ // restart. Cheap (one localStorage hit per silent catch in debug mode;
47
+ // catches happen rarely on hot paths).
48
+ if (!enabled) {
49
+ refreshSilentCatchState();
50
+ if (!enabled) return;
51
+ }
52
+ const entry = { module, operation, error: error?.message || String(error) };
53
+ if (context) Object.assign(entry, context);
54
+ console.warn('[clawser:silent-catch]', entry);
55
+ }