aissue 0.0.0-stage → 2.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.
Files changed (82) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +390 -2
  3. package/dist/cjs/browser.d.ts +16 -0
  4. package/dist/cjs/browser.js +80 -0
  5. package/dist/cjs/core/breadcrumbs.d.ts +48 -0
  6. package/dist/cjs/core/breadcrumbs.js +313 -0
  7. package/dist/cjs/core/client.d.ts +54 -0
  8. package/dist/cjs/core/client.js +293 -0
  9. package/dist/cjs/core/env.d.ts +19 -0
  10. package/dist/cjs/core/env.js +71 -0
  11. package/dist/cjs/core/event.d.ts +35 -0
  12. package/dist/cjs/core/event.js +187 -0
  13. package/dist/cjs/core/hub.d.ts +20 -0
  14. package/dist/cjs/core/hub.js +21 -0
  15. package/dist/cjs/core/instrument-browser.d.ts +16 -0
  16. package/dist/cjs/core/instrument-browser.js +208 -0
  17. package/dist/cjs/core/instrument-node.d.ts +5 -0
  18. package/dist/cjs/core/instrument-node.js +250 -0
  19. package/dist/cjs/core/instrument.d.ts +12 -0
  20. package/dist/cjs/core/instrument.js +137 -0
  21. package/dist/cjs/core/privacy.d.ts +25 -0
  22. package/dist/cjs/core/privacy.js +269 -0
  23. package/dist/cjs/core/stack.d.ts +13 -0
  24. package/dist/cjs/core/stack.js +147 -0
  25. package/dist/cjs/core/transport.d.ts +31 -0
  26. package/dist/cjs/core/transport.js +154 -0
  27. package/dist/cjs/core/types.d.ts +149 -0
  28. package/dist/cjs/core/types.js +2 -0
  29. package/dist/cjs/core/version.d.ts +2 -0
  30. package/dist/cjs/core/version.js +6 -0
  31. package/dist/cjs/index.d.ts +27 -0
  32. package/dist/cjs/index.js +83 -0
  33. package/dist/cjs/nextjs/client.d.ts +10 -0
  34. package/dist/cjs/nextjs/client.js +99 -0
  35. package/dist/cjs/nextjs/index.d.ts +31 -0
  36. package/dist/cjs/nextjs/index.js +99 -0
  37. package/dist/cjs/node.d.ts +48 -0
  38. package/dist/cjs/node.js +223 -0
  39. package/dist/cjs/package.json +3 -0
  40. package/dist/cjs/react.d.ts +40 -0
  41. package/dist/cjs/react.js +90 -0
  42. package/dist/esm/browser.d.ts +16 -0
  43. package/dist/esm/browser.js +71 -0
  44. package/dist/esm/cli.d.ts +2 -0
  45. package/dist/esm/cli.js +386 -0
  46. package/dist/esm/core/breadcrumbs.d.ts +48 -0
  47. package/dist/esm/core/breadcrumbs.js +296 -0
  48. package/dist/esm/core/client.d.ts +54 -0
  49. package/dist/esm/core/client.js +285 -0
  50. package/dist/esm/core/env.d.ts +19 -0
  51. package/dist/esm/core/env.js +62 -0
  52. package/dist/esm/core/event.d.ts +35 -0
  53. package/dist/esm/core/event.js +179 -0
  54. package/dist/esm/core/hub.d.ts +20 -0
  55. package/dist/esm/core/hub.js +17 -0
  56. package/dist/esm/core/instrument-browser.d.ts +16 -0
  57. package/dist/esm/core/instrument-browser.js +203 -0
  58. package/dist/esm/core/instrument-node.d.ts +5 -0
  59. package/dist/esm/core/instrument-node.js +214 -0
  60. package/dist/esm/core/instrument.d.ts +12 -0
  61. package/dist/esm/core/instrument.js +132 -0
  62. package/dist/esm/core/privacy.d.ts +25 -0
  63. package/dist/esm/core/privacy.js +257 -0
  64. package/dist/esm/core/stack.d.ts +13 -0
  65. package/dist/esm/core/stack.js +140 -0
  66. package/dist/esm/core/transport.d.ts +31 -0
  67. package/dist/esm/core/transport.js +149 -0
  68. package/dist/esm/core/types.d.ts +149 -0
  69. package/dist/esm/core/types.js +1 -0
  70. package/dist/esm/core/version.d.ts +2 -0
  71. package/dist/esm/core/version.js +3 -0
  72. package/dist/esm/index.d.ts +27 -0
  73. package/dist/esm/index.js +72 -0
  74. package/dist/esm/nextjs/client.d.ts +10 -0
  75. package/dist/esm/nextjs/client.js +90 -0
  76. package/dist/esm/nextjs/index.d.ts +31 -0
  77. package/dist/esm/nextjs/index.js +88 -0
  78. package/dist/esm/node.d.ts +48 -0
  79. package/dist/esm/node.js +210 -0
  80. package/dist/esm/react.d.ts +40 -0
  81. package/dist/esm/react.js +47 -0
  82. package/package.json +141 -4
@@ -0,0 +1,90 @@
1
+ /**
2
+ * aissue/nextjs/client: browser side of Next.js. In instrumentation-client.ts (Next 15.3+):
3
+ *
4
+ * import { initClient } from 'aissue/nextjs/client';
5
+ * initClient();
6
+ *
7
+ * Reads NEXT_PUBLIC_AISSUE_API_KEY and NEXT_PUBLIC_AISSUE_BASE_URL (or NEXT_PUBLIC_AISSUE_DSN).
8
+ */
9
+ import { init } from '../browser.js';
10
+ export { captureException, setUser, flush, addBreadcrumb, clearBreadcrumbs } from '../index.js';
11
+ // Each variable is referenced literally: Next.js only inlines `process.env.NEXT_PUBLIC_*`
12
+ // when the full expression appears in the source.
13
+ function publicApiKey() {
14
+ try {
15
+ return process.env.NEXT_PUBLIC_AISSUE_API_KEY || undefined;
16
+ }
17
+ catch {
18
+ return undefined;
19
+ }
20
+ }
21
+ function publicBaseUrl() {
22
+ try {
23
+ return process.env.NEXT_PUBLIC_AISSUE_BASE_URL || undefined;
24
+ }
25
+ catch {
26
+ return undefined;
27
+ }
28
+ }
29
+ function publicDsn() {
30
+ try {
31
+ return process.env.NEXT_PUBLIC_AISSUE_DSN || undefined;
32
+ }
33
+ catch {
34
+ return undefined;
35
+ }
36
+ }
37
+ function publicRelease() {
38
+ try {
39
+ const explicit = process.env.NEXT_PUBLIC_AISSUE_RELEASE;
40
+ if (explicit)
41
+ return explicit;
42
+ }
43
+ catch {
44
+ // ignore
45
+ }
46
+ try {
47
+ return process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA || undefined;
48
+ }
49
+ catch {
50
+ return undefined;
51
+ }
52
+ }
53
+ function publicEnvironment() {
54
+ try {
55
+ const explicit = process.env.NEXT_PUBLIC_AISSUE_ENVIRONMENT;
56
+ if (explicit)
57
+ return explicit;
58
+ }
59
+ catch {
60
+ // ignore
61
+ }
62
+ try {
63
+ return process.env.NEXT_PUBLIC_VERCEL_ENV || undefined;
64
+ }
65
+ catch {
66
+ return undefined;
67
+ }
68
+ }
69
+ /**
70
+ * Configure the browser SDK for a Next.js app and capture uncaught errors / unhandled rejections.
71
+ * Omitted options come from NEXT_PUBLIC_AISSUE_API_KEY, NEXT_PUBLIC_AISSUE_BASE_URL (or
72
+ * NEXT_PUBLIC_AISSUE_DSN), NEXT_PUBLIC_AISSUE_RELEASE / NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA and
73
+ * NEXT_PUBLIC_AISSUE_ENVIRONMENT / NEXT_PUBLIC_VERCEL_ENV. Does nothing on the server.
74
+ */
75
+ export function initClient(options = {}) {
76
+ try {
77
+ const dsn = options.dsn || publicDsn();
78
+ init({
79
+ ...options,
80
+ apiKey: options.apiKey || publicApiKey() || '',
81
+ baseUrl: options.baseUrl || publicBaseUrl() || '',
82
+ dsn,
83
+ release: options.release || publicRelease(),
84
+ environment: options.environment || publicEnvironment(),
85
+ });
86
+ }
87
+ catch {
88
+ // never throw into the host app
89
+ }
90
+ }
@@ -0,0 +1,31 @@
1
+ import type { AIssueOptions } from '../core/types.js';
2
+ export { runWithBreadcrumbs } from '../node.js';
3
+ export { captureException, setUser, flush, addBreadcrumb, clearBreadcrumbs } from '../index.js';
4
+ export type { AIssueEvent, AIssueOptions, AutoBreadcrumbsOptions, Breadcrumb, BreadcrumbInput, CaptureContext, StackFrame, User, } from '../index.js';
5
+ /** `request` argument of Next's onRequestError hook. */
6
+ export interface NextRequestInfo {
7
+ path: string;
8
+ method: string;
9
+ headers: Record<string, string | string[] | undefined>;
10
+ }
11
+ /** `context` argument of Next's onRequestError hook. */
12
+ export interface NextErrorContext {
13
+ routerKind: string;
14
+ routePath: string;
15
+ routeType: string;
16
+ renderSource?: string;
17
+ revalidateReason?: string;
18
+ renderType?: string;
19
+ }
20
+ /**
21
+ * Next.js instrumentation `register` hook. In the Node.js runtime it also captures uncaught
22
+ * exceptions and unhandled rejections; in the Edge runtime it only configures the SDK.
23
+ * Called by Next with no arguments; call it yourself to pass options.
24
+ */
25
+ export declare function register(options?: AIssueOptions): void;
26
+ /**
27
+ * Next.js 15+ instrumentation `onRequestError` hook. Sends the error with the request path
28
+ * (query stripped), method, allowlisted headers, routePath / routeType and the error digest
29
+ * (the id Next shows in production error pages). 4xx errors are skipped.
30
+ */
31
+ export declare function onRequestError(error: unknown, request: Readonly<NextRequestInfo>, context: Readonly<NextErrorContext>): Promise<void>;
@@ -0,0 +1,88 @@
1
+ /**
2
+ * aissue/nextjs: server side of Next.js (App and Pages Router), for instrumentation.ts:
3
+ *
4
+ * export { register, onRequestError } from 'aissue/nextjs';
5
+ *
6
+ * register() reads AISSUE_API_KEY / AISSUE_BASE_URL (or AISSUE_DSN). onRequestError is the
7
+ * Next.js 15+ hook that receives every server error (Server Components, route handlers,
8
+ * server actions, middleware).
9
+ */
10
+ import { setupClient } from '../core/client.js';
11
+ import { statusFromError } from '../core/event.js';
12
+ import { captureException, flush } from '../index.js';
13
+ import { installNodeBreadcrumbs, installNodeHandlers } from '../node.js';
14
+ export { runWithBreadcrumbs } from '../node.js';
15
+ export { captureException, setUser, flush, addBreadcrumb, clearBreadcrumbs } from '../index.js';
16
+ function nextRuntime() {
17
+ try {
18
+ // Referenced literally so Next.js can inline it per runtime.
19
+ return process.env.NEXT_RUNTIME;
20
+ }
21
+ catch {
22
+ return undefined;
23
+ }
24
+ }
25
+ /**
26
+ * Next.js instrumentation `register` hook. In the Node.js runtime it also captures uncaught
27
+ * exceptions and unhandled rejections; in the Edge runtime it only configures the SDK.
28
+ * Called by Next with no arguments; call it yourself to pass options.
29
+ */
30
+ export function register(options) {
31
+ try {
32
+ if (nextRuntime() === 'nodejs') {
33
+ const client = setupClient(options, { runtime: 'server', platform: 'node' });
34
+ if (client?.enabled) {
35
+ installNodeHandlers();
36
+ installNodeBreadcrumbs();
37
+ }
38
+ }
39
+ else {
40
+ setupClient(options, { runtime: 'server', platform: 'javascript' });
41
+ }
42
+ }
43
+ catch {
44
+ // never throw into the host app
45
+ }
46
+ }
47
+ /** notFound(), redirect(), dynamic-rendering bailouts: Next.js control flow, not bugs. */
48
+ function isNextControlFlow(error) {
49
+ const digest = error && typeof error === 'object' ? error.digest : undefined;
50
+ return (typeof digest === 'string' &&
51
+ /^(NEXT_REDIRECT|NEXT_NOT_FOUND|NEXT_HTTP_ERROR_FALLBACK|DYNAMIC_SERVER_USAGE|BAILOUT_TO_CLIENT_SIDE_RENDERING|NEXT_DYNAMIC_NO_SSR_CODE|NEXT_PRERENDER_INTERRUPTED)/.test(digest));
52
+ }
53
+ /**
54
+ * Next.js 15+ instrumentation `onRequestError` hook. Sends the error with the request path
55
+ * (query stripped), method, allowlisted headers, routePath / routeType and the error digest
56
+ * (the id Next shows in production error pages). 4xx errors are skipped.
57
+ */
58
+ export async function onRequestError(error, request, context) {
59
+ try {
60
+ if (isNextControlFlow(error))
61
+ return;
62
+ const status = statusFromError(error, 500);
63
+ if (status >= 400 && status <= 499)
64
+ return;
65
+ const digest = error && typeof error === 'object' ? error.digest : undefined;
66
+ const extra = {};
67
+ if (context) {
68
+ for (const key of ['routerKind', 'routePath', 'routeType', 'renderSource', 'revalidateReason', 'renderType']) {
69
+ if (context[key] !== undefined)
70
+ extra[key] = context[key];
71
+ }
72
+ }
73
+ if (typeof digest === 'string')
74
+ extra.digest = digest;
75
+ captureException(error, {
76
+ errorCode: status,
77
+ path: request?.path,
78
+ method: request?.method,
79
+ headers: request?.headers,
80
+ extra,
81
+ });
82
+ // Serverless functions may be frozen right after the response; give the send a chance.
83
+ await flush(2000);
84
+ }
85
+ catch {
86
+ // never throw into the host app
87
+ }
88
+ }
@@ -0,0 +1,48 @@
1
+ import type { AIssueOptions, HeadersInput } from './core/types.js';
2
+ export { captureException, setUser, flush, addBreadcrumb, clearBreadcrumbs } from './index.js';
3
+ export type { AIssueEvent, AIssueOptions, AutoBreadcrumbsOptions, Breadcrumb, BreadcrumbInput, CaptureContext, StackFrame, User, } from './index.js';
4
+ export interface NodeOptions extends AIssueOptions {
5
+ /** Capture uncaught exceptions (default true). The process still exits as it would without AIssue. */
6
+ captureUncaughtExceptions?: boolean;
7
+ /** Capture unhandled promise rejections (default true). Node's --unhandled-rejections mode is respected. */
8
+ captureUnhandledRejections?: boolean;
9
+ }
10
+ /** Install process-level handlers (idempotent). Exported for aissue/nextjs. */
11
+ export declare function installNodeHandlers(options?: Pick<NodeOptions, 'captureUncaughtExceptions' | 'captureUnhandledRejections'>): void;
12
+ /**
13
+ * Automatic Node breadcrumbs (console, fetch, http/https requests) and a separate breadcrumb buffer
14
+ * per incoming http(s) request. Idempotent; does nothing when the SDK is disabled. Exported for aissue/nextjs.
15
+ */
16
+ export declare function installNodeBreadcrumbs(): void;
17
+ /**
18
+ * Run `fn` with its own, empty breadcrumb buffer, for work that is not an http request
19
+ * (queue jobs, cron tasks, custom servers). Incoming http(s) server requests get one automatically.
20
+ * Returns what `fn` returns.
21
+ */
22
+ export declare function runWithBreadcrumbs<T>(fn: () => T): T;
23
+ /**
24
+ * Configure the SDK for Node.js and capture uncaught exceptions / unhandled rejections.
25
+ * Omitted options are read from AISSUE_API_KEY, AISSUE_BASE_URL (or AISSUE_DSN), AISSUE_ENVIRONMENT
26
+ * and the release env vars. Disabled by default when NODE_ENV is development or test.
27
+ */
28
+ export declare function init(options?: NodeOptions): void;
29
+ /** The parts of an Express (or plain Node http) request that are used. */
30
+ export interface ExpressLikeRequest {
31
+ method?: string;
32
+ originalUrl?: string;
33
+ url?: string;
34
+ path?: string;
35
+ headers?: HeadersInput;
36
+ }
37
+ export interface ExpressErrorHandlerOptions<Req = any> {
38
+ /** Decide which errors are sent. Default: everything except 4xx (status 400-499). */
39
+ shouldHandleError?: (error: unknown, status: number) => boolean;
40
+ /** Opaque user id for the request (e.g. req => req.user?.id). Never return an email. */
41
+ getUserId?: (req: Req) => string | number | null | undefined;
42
+ }
43
+ /**
44
+ * Express 4/5 error middleware. Add it after your routes and before other error handlers:
45
+ * app.use(expressErrorHandler());
46
+ * Captures the error with path, method, filtered query and allowlisted headers, then calls next(err).
47
+ */
48
+ export declare function expressErrorHandler<Req extends ExpressLikeRequest = ExpressLikeRequest>(options?: ExpressErrorHandlerOptions<Req>): (err: unknown, req: Req, res: unknown, next: (err?: unknown) => void) => void;
@@ -0,0 +1,210 @@
1
+ /**
2
+ * aissue/node: Node.js servers and scripts, including Express 4/5.
3
+ *
4
+ * import { init, expressErrorHandler } from 'aissue/node';
5
+ * init(); // reads AISSUE_API_KEY / AISSUE_BASE_URL
6
+ * ...
7
+ * app.use(expressErrorHandler()); // after your routes
8
+ *
9
+ * Captures uncaught exceptions (then exits like Node would) and unhandled rejections, and records
10
+ * breadcrumbs (console, fetch, http/https requests), kept per incoming request.
11
+ */
12
+ import { runWithBreadcrumbScope } from './core/breadcrumbs.js';
13
+ import { setupClient } from './core/client.js';
14
+ import { instrumentNode } from './core/instrument-node.js';
15
+ import { getHub } from './core/hub.js';
16
+ import { statusFromError } from './core/event.js';
17
+ import { captureException, flush } from './index.js';
18
+ export { captureException, setUser, flush, addBreadcrumb, clearBreadcrumbs } from './index.js';
19
+ /** The global process, looked up dynamically so edge bundlers do not flag Node APIs. */
20
+ function nodeProcess() {
21
+ const p = globalThis.process;
22
+ if (p && typeof p.on === 'function' && typeof p.listeners === 'function' && typeof p.removeListener === 'function') {
23
+ return p;
24
+ }
25
+ return undefined;
26
+ }
27
+ const FLUSH_BEFORE_EXIT_MS = 2000;
28
+ /** Node's --unhandled-rejections mode ("throw" is the default since Node 15). */
29
+ function unhandledRejectionMode(p) {
30
+ let mode = 'throw';
31
+ const args = [...(p.execArgv || []), ...String(p.env?.NODE_OPTIONS || '').split(/\s+/)];
32
+ for (const arg of args) {
33
+ const m = /^--unhandled-rejections=(\S+)$/.exec(arg);
34
+ if (m)
35
+ mode = m[1];
36
+ }
37
+ return mode;
38
+ }
39
+ let exiting = false;
40
+ function onUncaughtException(error) {
41
+ const p = nodeProcess();
42
+ try {
43
+ captureException(error);
44
+ }
45
+ catch {
46
+ // ignore
47
+ }
48
+ if (!p)
49
+ return;
50
+ // Someone else handles uncaught exceptions: their handler decides whether to exit.
51
+ const others = p.listeners('uncaughtException').filter((l) => l !== onUncaughtException);
52
+ if (others.length > 0 || exiting)
53
+ return;
54
+ exiting = true;
55
+ // Send the event, then crash exactly like Node would without AIssue (message on stderr, exit code 1).
56
+ flush(FLUSH_BEFORE_EXIT_MS).then(() => {
57
+ removeHandlers(p);
58
+ setTimeout(() => {
59
+ throw error;
60
+ }, 0);
61
+ });
62
+ }
63
+ function onUnhandledRejection(reason) {
64
+ const p = nodeProcess();
65
+ try {
66
+ captureException(reason);
67
+ }
68
+ catch {
69
+ // ignore
70
+ }
71
+ if (!p)
72
+ return;
73
+ const others = p.listeners('unhandledRejection').filter((l) => l !== onUnhandledRejection);
74
+ if (others.length > 0 || exiting)
75
+ return;
76
+ // Listening for 'unhandledRejection' disables Node's default crash, so restore it.
77
+ const mode = unhandledRejectionMode(p);
78
+ if (mode === 'warn-with-error-code') {
79
+ p.exitCode = 1;
80
+ return;
81
+ }
82
+ if (mode !== 'throw')
83
+ return; // "strict" crashes via uncaughtException; "warn"/"none" never crash
84
+ exiting = true;
85
+ const error = reason instanceof Error
86
+ ? reason
87
+ : Object.assign(new Error('This error originated either by throwing inside of an async function without a catch block, ' +
88
+ 'or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "' +
89
+ safeString(reason) +
90
+ '".'), { code: 'ERR_UNHANDLED_REJECTION' });
91
+ flush(FLUSH_BEFORE_EXIT_MS).then(() => {
92
+ removeHandlers(p);
93
+ setTimeout(() => {
94
+ throw error;
95
+ }, 0);
96
+ });
97
+ }
98
+ function safeString(value) {
99
+ try {
100
+ return String(value);
101
+ }
102
+ catch {
103
+ return '[unprintable]';
104
+ }
105
+ }
106
+ function removeHandlers(p) {
107
+ p.removeListener('uncaughtException', onUncaughtException);
108
+ p.removeListener('unhandledRejection', onUnhandledRejection);
109
+ const installed = getHub().installed;
110
+ installed.uncaughtException = false;
111
+ installed.unhandledRejection = false;
112
+ }
113
+ /** Install process-level handlers (idempotent). Exported for aissue/nextjs. */
114
+ export function installNodeHandlers(options = {}) {
115
+ try {
116
+ const p = nodeProcess();
117
+ if (!p)
118
+ return;
119
+ const installed = getHub().installed;
120
+ if (options.captureUncaughtExceptions !== false && !installed.uncaughtException) {
121
+ installed.uncaughtException = true;
122
+ p.on('uncaughtException', onUncaughtException);
123
+ }
124
+ if (options.captureUnhandledRejections !== false && !installed.unhandledRejection) {
125
+ installed.unhandledRejection = true;
126
+ p.on('unhandledRejection', onUnhandledRejection);
127
+ }
128
+ }
129
+ catch {
130
+ // never throw into the host app
131
+ }
132
+ }
133
+ /**
134
+ * Automatic Node breadcrumbs (console, fetch, http/https requests) and a separate breadcrumb buffer
135
+ * per incoming http(s) request. Idempotent; does nothing when the SDK is disabled. Exported for aissue/nextjs.
136
+ */
137
+ export function installNodeBreadcrumbs() {
138
+ try {
139
+ const client = getHub().client;
140
+ if (!client?.enabled || client.config.maxBreadcrumbs <= 0 || client.config.autoBreadcrumbs === false)
141
+ return;
142
+ instrumentNode();
143
+ }
144
+ catch {
145
+ // never throw into the host app
146
+ }
147
+ }
148
+ /**
149
+ * Run `fn` with its own, empty breadcrumb buffer, for work that is not an http request
150
+ * (queue jobs, cron tasks, custom servers). Incoming http(s) server requests get one automatically.
151
+ * Returns what `fn` returns.
152
+ */
153
+ export function runWithBreadcrumbs(fn) {
154
+ return runWithBreadcrumbScope(fn);
155
+ }
156
+ /**
157
+ * Configure the SDK for Node.js and capture uncaught exceptions / unhandled rejections.
158
+ * Omitted options are read from AISSUE_API_KEY, AISSUE_BASE_URL (or AISSUE_DSN), AISSUE_ENVIRONMENT
159
+ * and the release env vars. Disabled by default when NODE_ENV is development or test.
160
+ */
161
+ export function init(options = {}) {
162
+ try {
163
+ const client = setupClient(options, { runtime: 'server', platform: 'node' });
164
+ if (client?.enabled) {
165
+ installNodeHandlers(options);
166
+ installNodeBreadcrumbs();
167
+ }
168
+ }
169
+ catch {
170
+ // never throw into the host app
171
+ }
172
+ }
173
+ /**
174
+ * Express 4/5 error middleware. Add it after your routes and before other error handlers:
175
+ * app.use(expressErrorHandler());
176
+ * Captures the error with path, method, filtered query and allowlisted headers, then calls next(err).
177
+ */
178
+ export function expressErrorHandler(options = {}) {
179
+ // Four declared parameters: that is how Express recognises error middleware.
180
+ return function aissueErrorHandler(err, req, _res, next) {
181
+ try {
182
+ const status = statusFromError(err, 500);
183
+ const should = options.shouldHandleError
184
+ ? options.shouldHandleError(err, status)
185
+ : !(status >= 400 && status <= 499);
186
+ if (should) {
187
+ const r = (req || {});
188
+ const url = typeof r.originalUrl === 'string' ? r.originalUrl : typeof r.url === 'string' ? r.url : r.path;
189
+ let userId;
190
+ try {
191
+ userId = options.getUserId ? options.getUserId(req) : undefined;
192
+ }
193
+ catch {
194
+ userId = undefined;
195
+ }
196
+ captureException(err, {
197
+ errorCode: status,
198
+ path: url,
199
+ method: r.method,
200
+ headers: r.headers,
201
+ userId: userId ?? undefined,
202
+ });
203
+ }
204
+ }
205
+ catch {
206
+ // never throw into the host app
207
+ }
208
+ next(err);
209
+ };
210
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * aissue/react: error boundary for React 17-19 (and a re-export of the browser init).
3
+ *
4
+ * import { init, AIssueErrorBoundary } from 'aissue/react';
5
+ * init({ apiKey: '...', baseUrl: 'https://aissue.example.com' });
6
+ * <AIssueErrorBoundary fallback={<p>Something went wrong.</p>}><App /></AIssueErrorBoundary>
7
+ */
8
+ import * as React from 'react';
9
+ export { init } from './browser.js';
10
+ export type { BrowserOptions } from './browser.js';
11
+ export { captureException, setUser, flush, addBreadcrumb, clearBreadcrumbs } from './index.js';
12
+ export type { AIssueEvent, AIssueOptions, AutoBreadcrumbsOptions, Breadcrumb, BreadcrumbInput, CaptureContext, StackFrame, User, } from './index.js';
13
+ export interface FallbackProps {
14
+ error: unknown;
15
+ /** Clear the error and render the children again. */
16
+ reset: () => void;
17
+ }
18
+ export interface AIssueErrorBoundaryProps {
19
+ children?: React.ReactNode;
20
+ /** Rendered instead of the children after an error. A function receives { error, reset }. */
21
+ fallback?: React.ReactNode | ((props: FallbackProps) => React.ReactNode);
22
+ /** Called after the error was captured. */
23
+ onError?: (error: unknown, info: {
24
+ componentStack: string;
25
+ }) => void;
26
+ }
27
+ interface AIssueErrorBoundaryState {
28
+ hasError: boolean;
29
+ error: unknown;
30
+ }
31
+ /** Catches render errors in its subtree, sends them to AIssue and renders `fallback`. */
32
+ export declare class AIssueErrorBoundary extends React.Component<AIssueErrorBoundaryProps, AIssueErrorBoundaryState> {
33
+ state: AIssueErrorBoundaryState;
34
+ static getDerivedStateFromError(error: unknown): AIssueErrorBoundaryState;
35
+ componentDidCatch(error: unknown, info: {
36
+ componentStack?: string | null;
37
+ }): void;
38
+ reset: () => void;
39
+ render(): React.ReactNode;
40
+ }
@@ -0,0 +1,47 @@
1
+ "use client";
2
+ /**
3
+ * aissue/react: error boundary for React 17-19 (and a re-export of the browser init).
4
+ *
5
+ * import { init, AIssueErrorBoundary } from 'aissue/react';
6
+ * init({ apiKey: '...', baseUrl: 'https://aissue.example.com' });
7
+ * <AIssueErrorBoundary fallback={<p>Something went wrong.</p>}><App /></AIssueErrorBoundary>
8
+ */
9
+ import * as React from 'react';
10
+ import { captureWithExtras } from './core/client.js';
11
+ export { init } from './browser.js';
12
+ export { captureException, setUser, flush, addBreadcrumb, clearBreadcrumbs } from './index.js';
13
+ const MAX_COMPONENT_STACK = 8000;
14
+ /** Catches render errors in its subtree, sends them to AIssue and renders `fallback`. */
15
+ export class AIssueErrorBoundary extends React.Component {
16
+ constructor() {
17
+ super(...arguments);
18
+ this.state = { hasError: false, error: null };
19
+ this.reset = () => {
20
+ this.setState({ hasError: false, error: null });
21
+ };
22
+ }
23
+ static getDerivedStateFromError(error) {
24
+ return { hasError: true, error };
25
+ }
26
+ componentDidCatch(error, info) {
27
+ const componentStack = (info && typeof info.componentStack === 'string' ? info.componentStack : '').slice(0, MAX_COMPONENT_STACK);
28
+ // The component stack is context, not frames: it goes in request_data.context and the traceback text.
29
+ captureWithExtras(error, componentStack ? { extra: { componentStack } } : undefined, componentStack ? { tracebackSuffix: `\n\nReact component stack:${componentStack}` } : undefined);
30
+ try {
31
+ this.props.onError?.(error, { componentStack });
32
+ }
33
+ catch {
34
+ // a broken onError must not break the boundary
35
+ }
36
+ }
37
+ render() {
38
+ if (this.state.hasError) {
39
+ const { fallback } = this.props;
40
+ if (typeof fallback === 'function') {
41
+ return fallback({ error: this.state.error, reset: this.reset }) ?? null;
42
+ }
43
+ return fallback ?? null;
44
+ }
45
+ return this.props.children ?? null;
46
+ }
47
+ }