@extn/segi-js 0.2.15 → 0.2.17

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.
package/dist/index.d.mts CHANGED
@@ -2,6 +2,29 @@ import { SegiClientOptions, SegiEventDto, SegiUser, SegiBreadcrumb } from '@extn
2
2
  export { SegiBreadcrumb, SegiClientOptions, SegiEventDto, SegiUser } from '@extn/segi-shared';
3
3
  import { R as ReplayConfig } from './types-CsgcNSc1.mjs';
4
4
 
5
+ /**
6
+ * Turn whatever `captureException` was handed into an Error worth reporting,
7
+ * or `null` when there is nothing to report.
8
+ *
9
+ * Before this, every non-Error value became `new Error(JSON.stringify(v))`.
10
+ * For the common cases — an empty object from a third-party callback, an
11
+ * `undefined` rejection, a DOM Event — that produced "Error: {}" with no
12
+ * message, no culprit and an SDK-only stack. One production site generated
13
+ * 397k of those in five weeks, all from a chat-widget boot callback.
14
+ *
15
+ * Order of preference:
16
+ * 1. Error instances pass through untouched.
17
+ * 2. Strings / numbers / booleans become the message.
18
+ * 3. Event-like wrappers (`.error`, `.reason`) are unwrapped.
19
+ * 4. Error-like objects (`.message`, plus optional `.name` / `.code` /
20
+ * `.stack`) become an Error carrying those fields — covers DOMException
21
+ * in old engines, axios/fetch error shapes, GraphQL errors, etc.
22
+ * 5. Plain data objects are serialised (truncated) so the payload survives.
23
+ * 6. Anything that serialises to nothing (`{}`, `[]`, `null`, a cyclic
24
+ * object, a class instance with no own data) is dropped: `null`.
25
+ */
26
+ declare function normalizeError(value: unknown): Error | null;
27
+
5
28
  interface InitOptions {
6
29
  /** Re-initialize even if already initialized. Replaces config and listeners. */
7
30
  force?: boolean;
@@ -57,4 +80,4 @@ declare const Segi: {
57
80
  addBreadcrumb(breadcrumb: SegiBreadcrumb): void;
58
81
  };
59
82
 
60
- export { type InitOptions, Segi, type SegiInitOptions, Segi as default };
83
+ export { type InitOptions, Segi, type SegiInitOptions, Segi as default, normalizeError };
package/dist/index.d.ts CHANGED
@@ -2,6 +2,29 @@ import { SegiClientOptions, SegiEventDto, SegiUser, SegiBreadcrumb } from '@extn
2
2
  export { SegiBreadcrumb, SegiClientOptions, SegiEventDto, SegiUser } from '@extn/segi-shared';
3
3
  import { R as ReplayConfig } from './types-CsgcNSc1.js';
4
4
 
5
+ /**
6
+ * Turn whatever `captureException` was handed into an Error worth reporting,
7
+ * or `null` when there is nothing to report.
8
+ *
9
+ * Before this, every non-Error value became `new Error(JSON.stringify(v))`.
10
+ * For the common cases — an empty object from a third-party callback, an
11
+ * `undefined` rejection, a DOM Event — that produced "Error: {}" with no
12
+ * message, no culprit and an SDK-only stack. One production site generated
13
+ * 397k of those in five weeks, all from a chat-widget boot callback.
14
+ *
15
+ * Order of preference:
16
+ * 1. Error instances pass through untouched.
17
+ * 2. Strings / numbers / booleans become the message.
18
+ * 3. Event-like wrappers (`.error`, `.reason`) are unwrapped.
19
+ * 4. Error-like objects (`.message`, plus optional `.name` / `.code` /
20
+ * `.stack`) become an Error carrying those fields — covers DOMException
21
+ * in old engines, axios/fetch error shapes, GraphQL errors, etc.
22
+ * 5. Plain data objects are serialised (truncated) so the payload survives.
23
+ * 6. Anything that serialises to nothing (`{}`, `[]`, `null`, a cyclic
24
+ * object, a class instance with no own data) is dropped: `null`.
25
+ */
26
+ declare function normalizeError(value: unknown): Error | null;
27
+
5
28
  interface InitOptions {
6
29
  /** Re-initialize even if already initialized. Replaces config and listeners. */
7
30
  force?: boolean;
@@ -57,4 +80,4 @@ declare const Segi: {
57
80
  addBreadcrumb(breadcrumb: SegiBreadcrumb): void;
58
81
  };
59
82
 
60
- export { type InitOptions, Segi, type SegiInitOptions, Segi as default };
83
+ export { type InitOptions, Segi, type SegiInitOptions, Segi as default, normalizeError };
package/dist/index.js CHANGED
@@ -47,7 +47,7 @@ function buildEvent(state2, base) {
47
47
  return { ...evt, _drop: true };
48
48
  }
49
49
  return next;
50
- } catch (e) {
50
+ } catch (e2) {
51
51
  return evt;
52
52
  }
53
53
  }
@@ -130,10 +130,108 @@ function send(endpoint, projectKey, event) {
130
130
  const blob = new Blob([body], { type: "application/json" });
131
131
  navigator.sendBeacon(beaconUrl, blob);
132
132
  }
133
- } catch (e2) {
133
+ } catch (e3) {
134
134
  }
135
135
  }
136
136
 
137
+ // src/normalize-error.ts
138
+ var MAX_JSON_MESSAGE = 1e3;
139
+ function str(v) {
140
+ return typeof v === "string" && v.trim().length > 0 ? v : null;
141
+ }
142
+ function withMeta(e, o) {
143
+ const name = _nullishCoalesce(str(o.name), () => ( (typeof o.code === "string" ? o.code : null)));
144
+ if (name) {
145
+ try {
146
+ e.name = name;
147
+ } catch (e4) {
148
+ }
149
+ }
150
+ const stack = str(o.stack);
151
+ if (stack) {
152
+ try {
153
+ e.stack = stack;
154
+ } catch (e5) {
155
+ }
156
+ }
157
+ return e;
158
+ }
159
+ function hasContent(v, depth = 0, seen = /* @__PURE__ */ new Set()) {
160
+ if (v === null || v === void 0) return false;
161
+ switch (typeof v) {
162
+ case "string":
163
+ return v.trim().length > 0;
164
+ case "number":
165
+ case "boolean":
166
+ case "bigint":
167
+ return true;
168
+ case "object":
169
+ break;
170
+ default:
171
+ return false;
172
+ }
173
+ if (depth > 4 || seen.has(v)) return false;
174
+ seen.add(v);
175
+ try {
176
+ for (const k of Object.keys(v)) {
177
+ if (hasContent(v[k], depth + 1, seen)) return true;
178
+ }
179
+ } catch (e6) {
180
+ }
181
+ return false;
182
+ }
183
+ function normalizeError(value) {
184
+ if (value instanceof Error) return value;
185
+ if (value === null || value === void 0) return null;
186
+ switch (typeof value) {
187
+ case "string":
188
+ return str(value) ? new Error(value) : null;
189
+ case "number":
190
+ case "boolean":
191
+ case "bigint":
192
+ return new Error(String(value));
193
+ case "symbol":
194
+ return new Error(value.toString());
195
+ case "function":
196
+ return null;
197
+ case "object":
198
+ break;
199
+ default:
200
+ return null;
201
+ }
202
+ const o = value;
203
+ if (o.error !== void 0 && o.error !== value) {
204
+ const inner = normalizeError(o.error);
205
+ if (inner) return inner;
206
+ }
207
+ if (o.reason !== void 0 && o.reason !== value) {
208
+ const inner = normalizeError(o.reason);
209
+ if (inner) return inner;
210
+ }
211
+ const message = _nullishCoalesce(_nullishCoalesce(str(o.message), () => ( str(o.description))), () => ( str(o.msg)));
212
+ if (message) return withMeta(new Error(message), o);
213
+ let json;
214
+ try {
215
+ json = hasContent(value) ? JSON.stringify(value) : void 0;
216
+ } catch (e7) {
217
+ json = void 0;
218
+ }
219
+ if (json && json !== "{}" && json !== "[]" && json !== "null") {
220
+ return withMeta(
221
+ new Error(json.length > MAX_JSON_MESSAGE ? `${json.slice(0, MAX_JSON_MESSAGE)}\u2026` : json),
222
+ o
223
+ );
224
+ }
225
+ try {
226
+ const s = String(value);
227
+ if (s && !/^\[object [A-Za-z]+\]$/.test(s) && s !== "{}") {
228
+ return withMeta(new Error(s), o);
229
+ }
230
+ } catch (e8) {
231
+ }
232
+ return null;
233
+ }
234
+
137
235
  // src/index.ts
138
236
  var state = createState();
139
237
  function safeStack(err) {
@@ -141,7 +239,7 @@ function safeStack(err) {
141
239
  if (err && typeof err === "object" && "stack" in err) {
142
240
  return String(_nullishCoalesce(err.stack, () => ( "")));
143
241
  }
144
- } catch (e3) {
242
+ } catch (e9) {
145
243
  }
146
244
  return void 0;
147
245
  }
@@ -165,7 +263,7 @@ function captureWindowError(event) {
165
263
  url: typeof location !== "undefined" ? location.href : void 0
166
264
  });
167
265
  }
168
- } catch (e4) {
266
+ } catch (e10) {
169
267
  }
170
268
  }
171
269
  function attachListeners() {
@@ -258,7 +356,7 @@ var Segi = {
258
356
  }).catch(() => {
259
357
  });
260
358
  }
261
- } catch (e5) {
359
+ } catch (e11) {
262
360
  }
263
361
  },
264
362
  /** True if init() has been called successfully on this bundle. */
@@ -282,13 +380,18 @@ var Segi = {
282
380
  const w = window;
283
381
  w.__segiInitialized = false;
284
382
  }
285
- } catch (e6) {
383
+ } catch (e12) {
286
384
  }
287
385
  },
288
386
  captureException(error, context = {}) {
289
387
  try {
290
388
  if (!state.initialized) return;
291
- const err = error instanceof Error ? error : new Error(typeof error === "string" ? error : JSON.stringify(error));
389
+ let err = normalizeError(error);
390
+ if (!err) {
391
+ const fallback = typeof context.message === "string" ? context.message.trim() : "";
392
+ if (!fallback) return;
393
+ err = new Error(fallback);
394
+ }
292
395
  const evt = buildEvent(state, {
293
396
  type: "error",
294
397
  level: "error",
@@ -309,7 +412,7 @@ var Segi = {
309
412
  Segi.replay._captureError({ ts: evt.timestamp || (/* @__PURE__ */ new Date()).toISOString(), url: evt.url });
310
413
  }
311
414
  send(state.options.endpoint, state.options.projectKey, evt);
312
- } catch (e7) {
415
+ } catch (e13) {
313
416
  }
314
417
  },
315
418
  captureMessage(message, context = {}) {
@@ -333,20 +436,20 @@ var Segi = {
333
436
  Segi.replay._captureError({ ts: evt.timestamp || (/* @__PURE__ */ new Date()).toISOString(), url: evt.url });
334
437
  }
335
438
  send(state.options.endpoint, state.options.projectKey, evt);
336
- } catch (e8) {
439
+ } catch (e14) {
337
440
  }
338
441
  },
339
442
  setUser(user) {
340
443
  try {
341
444
  state.user = user;
342
445
  Segi.replay.setUser(user ? { id: user.id, email: user.email } : null);
343
- } catch (e9) {
446
+ } catch (e15) {
344
447
  }
345
448
  },
346
449
  setTag(key, value) {
347
450
  try {
348
451
  state.tags[key] = value;
349
- } catch (e10) {
452
+ } catch (e16) {
350
453
  }
351
454
  },
352
455
  addBreadcrumb(breadcrumb) {
@@ -355,7 +458,7 @@ var Segi = {
355
458
  ...breadcrumb,
356
459
  timestamp: _nullishCoalesce(breadcrumb.timestamp, () => ( (/* @__PURE__ */ new Date()).toISOString()))
357
460
  });
358
- } catch (e11) {
461
+ } catch (e17) {
359
462
  }
360
463
  }
361
464
  };
@@ -363,4 +466,5 @@ var src_default = Segi;
363
466
 
364
467
 
365
468
 
366
- exports.Segi = Segi; exports.default = src_default;
469
+
470
+ exports.Segi = Segi; exports.default = src_default; exports.normalizeError = normalizeError;
package/dist/index.mjs CHANGED
@@ -134,6 +134,104 @@ function send(endpoint, projectKey, event) {
134
134
  }
135
135
  }
136
136
 
137
+ // src/normalize-error.ts
138
+ var MAX_JSON_MESSAGE = 1e3;
139
+ function str(v) {
140
+ return typeof v === "string" && v.trim().length > 0 ? v : null;
141
+ }
142
+ function withMeta(e, o) {
143
+ const name = str(o.name) ?? (typeof o.code === "string" ? o.code : null);
144
+ if (name) {
145
+ try {
146
+ e.name = name;
147
+ } catch {
148
+ }
149
+ }
150
+ const stack = str(o.stack);
151
+ if (stack) {
152
+ try {
153
+ e.stack = stack;
154
+ } catch {
155
+ }
156
+ }
157
+ return e;
158
+ }
159
+ function hasContent(v, depth = 0, seen = /* @__PURE__ */ new Set()) {
160
+ if (v === null || v === void 0) return false;
161
+ switch (typeof v) {
162
+ case "string":
163
+ return v.trim().length > 0;
164
+ case "number":
165
+ case "boolean":
166
+ case "bigint":
167
+ return true;
168
+ case "object":
169
+ break;
170
+ default:
171
+ return false;
172
+ }
173
+ if (depth > 4 || seen.has(v)) return false;
174
+ seen.add(v);
175
+ try {
176
+ for (const k of Object.keys(v)) {
177
+ if (hasContent(v[k], depth + 1, seen)) return true;
178
+ }
179
+ } catch {
180
+ }
181
+ return false;
182
+ }
183
+ function normalizeError(value) {
184
+ if (value instanceof Error) return value;
185
+ if (value === null || value === void 0) return null;
186
+ switch (typeof value) {
187
+ case "string":
188
+ return str(value) ? new Error(value) : null;
189
+ case "number":
190
+ case "boolean":
191
+ case "bigint":
192
+ return new Error(String(value));
193
+ case "symbol":
194
+ return new Error(value.toString());
195
+ case "function":
196
+ return null;
197
+ case "object":
198
+ break;
199
+ default:
200
+ return null;
201
+ }
202
+ const o = value;
203
+ if (o.error !== void 0 && o.error !== value) {
204
+ const inner = normalizeError(o.error);
205
+ if (inner) return inner;
206
+ }
207
+ if (o.reason !== void 0 && o.reason !== value) {
208
+ const inner = normalizeError(o.reason);
209
+ if (inner) return inner;
210
+ }
211
+ const message = str(o.message) ?? str(o.description) ?? str(o.msg);
212
+ if (message) return withMeta(new Error(message), o);
213
+ let json;
214
+ try {
215
+ json = hasContent(value) ? JSON.stringify(value) : void 0;
216
+ } catch {
217
+ json = void 0;
218
+ }
219
+ if (json && json !== "{}" && json !== "[]" && json !== "null") {
220
+ return withMeta(
221
+ new Error(json.length > MAX_JSON_MESSAGE ? `${json.slice(0, MAX_JSON_MESSAGE)}\u2026` : json),
222
+ o
223
+ );
224
+ }
225
+ try {
226
+ const s = String(value);
227
+ if (s && !/^\[object [A-Za-z]+\]$/.test(s) && s !== "{}") {
228
+ return withMeta(new Error(s), o);
229
+ }
230
+ } catch {
231
+ }
232
+ return null;
233
+ }
234
+
137
235
  // src/index.ts
138
236
  var state = createState();
139
237
  function safeStack(err) {
@@ -288,7 +386,12 @@ var Segi = {
288
386
  captureException(error, context = {}) {
289
387
  try {
290
388
  if (!state.initialized) return;
291
- const err = error instanceof Error ? error : new Error(typeof error === "string" ? error : JSON.stringify(error));
389
+ let err = normalizeError(error);
390
+ if (!err) {
391
+ const fallback = typeof context.message === "string" ? context.message.trim() : "";
392
+ if (!fallback) return;
393
+ err = new Error(fallback);
394
+ }
292
395
  const evt = buildEvent(state, {
293
396
  type: "error",
294
397
  level: "error",
@@ -362,5 +465,6 @@ var Segi = {
362
465
  var src_default = Segi;
363
466
  export {
364
467
  Segi,
365
- src_default as default
468
+ src_default as default,
469
+ normalizeError
366
470
  };
@@ -34,16 +34,14 @@ type ErrorSignal = {
34
34
  ts: string;
35
35
  url?: string;
36
36
  };
37
- /**
38
- * Internal entry-point invoked by Segi.init() when a ReplayConfig was
39
- * passed. Not part of the public surface.
40
- */
41
- declare function activateReplay(args: {
37
+ declare function isPathBlocked(opts: ReplayOptions, path: string): boolean;
38
+ type Activation = {
42
39
  projectKey: string;
43
- /** Optional — replay transport defaults to https://segiapi.extn.ai. */
44
40
  endpoint?: string;
45
41
  config: ReplayConfig;
46
- }): Promise<void>;
42
+ };
43
+ /** Fetch policy before recording. Failure keeps replay off; retry every 30s until configured. */
44
+ declare function activateReplay(args: Activation): Promise<void>;
47
45
  /** Public Segi.replay namespace, attached by the main index.ts. */
48
46
  declare const replayApi: {
49
47
  /** Internal bridge from the error SDK; no stack/message is copied to trace. */
@@ -65,4 +63,4 @@ declare const replayApi: {
65
63
  _sessionUid(): string | null;
66
64
  };
67
65
 
68
- export { ReplayConfig, ReplayOptions, activateReplay, replayApi, withReplay };
66
+ export { ReplayConfig, ReplayOptions, activateReplay, isPathBlocked, replayApi, withReplay };
@@ -34,16 +34,14 @@ type ErrorSignal = {
34
34
  ts: string;
35
35
  url?: string;
36
36
  };
37
- /**
38
- * Internal entry-point invoked by Segi.init() when a ReplayConfig was
39
- * passed. Not part of the public surface.
40
- */
41
- declare function activateReplay(args: {
37
+ declare function isPathBlocked(opts: ReplayOptions, path: string): boolean;
38
+ type Activation = {
42
39
  projectKey: string;
43
- /** Optional — replay transport defaults to https://segiapi.extn.ai. */
44
40
  endpoint?: string;
45
41
  config: ReplayConfig;
46
- }): Promise<void>;
42
+ };
43
+ /** Fetch policy before recording. Failure keeps replay off; retry every 30s until configured. */
44
+ declare function activateReplay(args: Activation): Promise<void>;
47
45
  /** Public Segi.replay namespace, attached by the main index.ts. */
48
46
  declare const replayApi: {
49
47
  /** Internal bridge from the error SDK; no stack/message is copied to trace. */
@@ -65,4 +63,4 @@ declare const replayApi: {
65
63
  _sessionUid(): string | null;
66
64
  };
67
65
 
68
- export { ReplayConfig, ReplayOptions, activateReplay, replayApi, withReplay };
66
+ export { ReplayConfig, ReplayOptions, activateReplay, isPathBlocked, replayApi, withReplay };