@bigbertha/sdk 0.1.6 → 0.1.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.
@@ -1 +1 @@
1
- export declare const VERSION = "0.1.6";
1
+ export declare const VERSION = "0.1.8";
@@ -1 +1 @@
1
- export const VERSION = "0.1.6";
1
+ export const VERSION = "0.1.8";
@@ -31,8 +31,8 @@ export function init(options = {}) {
31
31
  setClient(client);
32
32
  if (options.handlers !== false && !handlersInstalled) {
33
33
  handlersInstalled = true;
34
- process.on("uncaughtException", (error) => void crash("uncaughtException", error));
35
- process.on("unhandledRejection", (reason) => void crash("unhandledRejection", reason));
34
+ process.on("uncaughtException", sdkHandler((error) => void crash("uncaughtException", error)));
35
+ process.on("unhandledRejection", sdkHandler((reason) => void crash("unhandledRejection", reason)));
36
36
  process.on("beforeExit", () => void getClient()?.flush());
37
37
  }
38
38
  if (options.heartbeat !== false) {
@@ -44,12 +44,28 @@ export function init(options = {}) {
44
44
  }
45
45
  return client;
46
46
  }
47
- /** Reports, flushes and then, if nothing else is listening, exits as Node would have. */
47
+ /**
48
+ * Error SDKs mark their process handlers with `_errorHandler` (Sentry's convention). Each
49
+ * leaves the exit to the app's own handlers, so two SDKs that counted each other would
50
+ * both wait and the process would carry on after an uncaught exception.
51
+ */
52
+ function sdkHandler(fn) {
53
+ return Object.assign(fn, { _errorHandler: true });
54
+ }
55
+ /** Whether the app itself handles the event, not counting error SDKs on uncaughtException. */
56
+ function appHandles(event) {
57
+ // A rejection handler from another SDK may deliberately keep the process alive (Sentry
58
+ // warns by default), so on that event every other listener counts.
59
+ if (event === "unhandledRejection")
60
+ return process.listenerCount(event) > 1;
61
+ return process.listeners(event).some((l) => !l._errorHandler);
62
+ }
63
+ /** Reports, flushes and then, if the app isn't handling it, exits as Node would have. */
48
64
  async function crash(event, error) {
49
65
  const client = getClient();
50
66
  client?.captureException(error, { level: "fatal" });
51
67
  await Promise.race([client?.flush(), new Promise((r) => setTimeout(r, EXIT_FLUSH_MS).unref())]);
52
- if (process.listenerCount(event) > 1)
68
+ if (appHandles(event))
53
69
  return;
54
70
  console.error(event === "unhandledRejection" && !(error instanceof Error) ? `Unhandled rejection: ${String(error)}` : error);
55
71
  process.exit(1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bigbertha/sdk",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "Error reporting for bertha: browser, Node, edge and Next.js, with source maps by debug id.",
5
5
  "license": "MIT",
6
6
  "repository": {