@builderai/sentinel-browser 0.1.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.
- package/dist/index.d.mts +57 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.js +227 -0
- package/dist/index.mjs +196 -0
- package/package.json +34 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
interface SentinelConfig {
|
|
2
|
+
dsn: string;
|
|
3
|
+
environment?: string;
|
|
4
|
+
release?: string;
|
|
5
|
+
debug?: boolean;
|
|
6
|
+
sampleRate?: number;
|
|
7
|
+
maxBreadcrumbs?: number;
|
|
8
|
+
}
|
|
9
|
+
interface SentinelEvent {
|
|
10
|
+
dsn: string;
|
|
11
|
+
events: ErrorEventPayload[];
|
|
12
|
+
}
|
|
13
|
+
interface ErrorEventPayload {
|
|
14
|
+
title: string;
|
|
15
|
+
location?: string;
|
|
16
|
+
type?: 'Error' | 'Exception' | 'Performance' | 'Security';
|
|
17
|
+
severity?: 'critical' | 'error' | 'warning' | 'info';
|
|
18
|
+
stackTrace?: string;
|
|
19
|
+
tags?: Record<string, string>;
|
|
20
|
+
breadcrumbs?: Breadcrumb[];
|
|
21
|
+
usersAffected?: number;
|
|
22
|
+
fingerprint?: string;
|
|
23
|
+
}
|
|
24
|
+
interface Breadcrumb {
|
|
25
|
+
timestamp: string;
|
|
26
|
+
category: string;
|
|
27
|
+
message: string;
|
|
28
|
+
level: string;
|
|
29
|
+
data?: any;
|
|
30
|
+
}
|
|
31
|
+
declare class Sentinel {
|
|
32
|
+
private static instance;
|
|
33
|
+
private config;
|
|
34
|
+
private breadcrumbs;
|
|
35
|
+
private user;
|
|
36
|
+
private tags;
|
|
37
|
+
private initialized;
|
|
38
|
+
private constructor();
|
|
39
|
+
static getInstance(): Sentinel;
|
|
40
|
+
init(config: SentinelConfig): void;
|
|
41
|
+
setUser(user: any): void;
|
|
42
|
+
setTag(key: string, value: string): void;
|
|
43
|
+
addBreadcrumb(breadcrumb: Omit<Breadcrumb, 'timestamp'>): void;
|
|
44
|
+
captureException(error: Error | string, context?: any): void;
|
|
45
|
+
captureMessage(message: string, level?: 'info' | 'warning' | 'error' | 'critical'): void;
|
|
46
|
+
private sendEvent;
|
|
47
|
+
private setupGlobalHandlers;
|
|
48
|
+
private setupBreadcrumbs;
|
|
49
|
+
}
|
|
50
|
+
declare const sentinel: Sentinel;
|
|
51
|
+
declare const init: (config: SentinelConfig) => void;
|
|
52
|
+
declare const captureException: (error: Error | string, context?: any) => void;
|
|
53
|
+
declare const captureMessage: (msg: string, level?: any) => void;
|
|
54
|
+
declare const setUser: (user: any) => void;
|
|
55
|
+
declare const setTag: (key: string, value: string) => void;
|
|
56
|
+
|
|
57
|
+
export { type Breadcrumb, type ErrorEventPayload, Sentinel, type SentinelConfig, type SentinelEvent, captureException, captureMessage, init, sentinel, setTag, setUser };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
interface SentinelConfig {
|
|
2
|
+
dsn: string;
|
|
3
|
+
environment?: string;
|
|
4
|
+
release?: string;
|
|
5
|
+
debug?: boolean;
|
|
6
|
+
sampleRate?: number;
|
|
7
|
+
maxBreadcrumbs?: number;
|
|
8
|
+
}
|
|
9
|
+
interface SentinelEvent {
|
|
10
|
+
dsn: string;
|
|
11
|
+
events: ErrorEventPayload[];
|
|
12
|
+
}
|
|
13
|
+
interface ErrorEventPayload {
|
|
14
|
+
title: string;
|
|
15
|
+
location?: string;
|
|
16
|
+
type?: 'Error' | 'Exception' | 'Performance' | 'Security';
|
|
17
|
+
severity?: 'critical' | 'error' | 'warning' | 'info';
|
|
18
|
+
stackTrace?: string;
|
|
19
|
+
tags?: Record<string, string>;
|
|
20
|
+
breadcrumbs?: Breadcrumb[];
|
|
21
|
+
usersAffected?: number;
|
|
22
|
+
fingerprint?: string;
|
|
23
|
+
}
|
|
24
|
+
interface Breadcrumb {
|
|
25
|
+
timestamp: string;
|
|
26
|
+
category: string;
|
|
27
|
+
message: string;
|
|
28
|
+
level: string;
|
|
29
|
+
data?: any;
|
|
30
|
+
}
|
|
31
|
+
declare class Sentinel {
|
|
32
|
+
private static instance;
|
|
33
|
+
private config;
|
|
34
|
+
private breadcrumbs;
|
|
35
|
+
private user;
|
|
36
|
+
private tags;
|
|
37
|
+
private initialized;
|
|
38
|
+
private constructor();
|
|
39
|
+
static getInstance(): Sentinel;
|
|
40
|
+
init(config: SentinelConfig): void;
|
|
41
|
+
setUser(user: any): void;
|
|
42
|
+
setTag(key: string, value: string): void;
|
|
43
|
+
addBreadcrumb(breadcrumb: Omit<Breadcrumb, 'timestamp'>): void;
|
|
44
|
+
captureException(error: Error | string, context?: any): void;
|
|
45
|
+
captureMessage(message: string, level?: 'info' | 'warning' | 'error' | 'critical'): void;
|
|
46
|
+
private sendEvent;
|
|
47
|
+
private setupGlobalHandlers;
|
|
48
|
+
private setupBreadcrumbs;
|
|
49
|
+
}
|
|
50
|
+
declare const sentinel: Sentinel;
|
|
51
|
+
declare const init: (config: SentinelConfig) => void;
|
|
52
|
+
declare const captureException: (error: Error | string, context?: any) => void;
|
|
53
|
+
declare const captureMessage: (msg: string, level?: any) => void;
|
|
54
|
+
declare const setUser: (user: any) => void;
|
|
55
|
+
declare const setTag: (key: string, value: string) => void;
|
|
56
|
+
|
|
57
|
+
export { type Breadcrumb, type ErrorEventPayload, Sentinel, type SentinelConfig, type SentinelEvent, captureException, captureMessage, init, sentinel, setTag, setUser };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
Sentinel: () => Sentinel,
|
|
24
|
+
captureException: () => captureException,
|
|
25
|
+
captureMessage: () => captureMessage,
|
|
26
|
+
init: () => init,
|
|
27
|
+
sentinel: () => sentinel,
|
|
28
|
+
setTag: () => setTag,
|
|
29
|
+
setUser: () => setUser
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(index_exports);
|
|
32
|
+
var Sentinel = class _Sentinel {
|
|
33
|
+
constructor() {
|
|
34
|
+
this.config = {
|
|
35
|
+
dsn: "",
|
|
36
|
+
environment: "production",
|
|
37
|
+
maxBreadcrumbs: 20
|
|
38
|
+
};
|
|
39
|
+
this.breadcrumbs = [];
|
|
40
|
+
this.user = {};
|
|
41
|
+
this.tags = {};
|
|
42
|
+
this.initialized = false;
|
|
43
|
+
}
|
|
44
|
+
static getInstance() {
|
|
45
|
+
if (!_Sentinel.instance) {
|
|
46
|
+
_Sentinel.instance = new _Sentinel();
|
|
47
|
+
}
|
|
48
|
+
return _Sentinel.instance;
|
|
49
|
+
}
|
|
50
|
+
init(config) {
|
|
51
|
+
if (this.initialized) {
|
|
52
|
+
console.warn("[Sentinel] Already initialized");
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
this.config = { ...this.config, ...config };
|
|
56
|
+
this.initialized = true;
|
|
57
|
+
if (this.config.debug) {
|
|
58
|
+
console.log("[Sentinel] Initialized with DSN:", this.config.dsn);
|
|
59
|
+
}
|
|
60
|
+
this.setupGlobalHandlers();
|
|
61
|
+
this.setupBreadcrumbs();
|
|
62
|
+
}
|
|
63
|
+
setUser(user) {
|
|
64
|
+
this.user = user;
|
|
65
|
+
}
|
|
66
|
+
setTag(key, value) {
|
|
67
|
+
this.tags[key] = value;
|
|
68
|
+
}
|
|
69
|
+
addBreadcrumb(breadcrumb) {
|
|
70
|
+
const crumb = {
|
|
71
|
+
...breadcrumb,
|
|
72
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
73
|
+
};
|
|
74
|
+
this.breadcrumbs.push(crumb);
|
|
75
|
+
if (this.breadcrumbs.length > (this.config.maxBreadcrumbs || 20)) {
|
|
76
|
+
this.breadcrumbs.shift();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
captureException(error, context) {
|
|
80
|
+
const stackTrace = error instanceof Error ? error.stack : "No stack trace available";
|
|
81
|
+
const title = error instanceof Error ? error.message : String(error);
|
|
82
|
+
const type = error instanceof Error ? error.name : "Error";
|
|
83
|
+
this.sendEvent({
|
|
84
|
+
title,
|
|
85
|
+
location: window.location.href,
|
|
86
|
+
// Simplified location
|
|
87
|
+
type: "Exception",
|
|
88
|
+
severity: "error",
|
|
89
|
+
stackTrace,
|
|
90
|
+
tags: { ...this.tags, ...context?.tags },
|
|
91
|
+
breadcrumbs: [...this.breadcrumbs],
|
|
92
|
+
usersAffected: 1
|
|
93
|
+
// Client side always 1 user context usually
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
captureMessage(message, level = "info") {
|
|
97
|
+
this.sendEvent({
|
|
98
|
+
title: message,
|
|
99
|
+
location: window.location.href,
|
|
100
|
+
type: "Error",
|
|
101
|
+
severity: level,
|
|
102
|
+
stackTrace: new Error().stack,
|
|
103
|
+
// Capture stack for context
|
|
104
|
+
tags: this.tags,
|
|
105
|
+
breadcrumbs: [...this.breadcrumbs]
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
sendEvent(payload) {
|
|
109
|
+
if (!this.initialized || !this.config.dsn) return;
|
|
110
|
+
if (this.config.environment) payload.tags = { ...payload.tags, environment: this.config.environment };
|
|
111
|
+
if (this.config.release) payload.tags = { ...payload.tags, release: this.config.release };
|
|
112
|
+
if (this.user.id) payload.tags = { ...payload.tags, userId: this.user.id };
|
|
113
|
+
const envelope = {
|
|
114
|
+
dsn: this.config.dsn,
|
|
115
|
+
events: [payload]
|
|
116
|
+
};
|
|
117
|
+
if (this.config.debug) {
|
|
118
|
+
console.log("[Sentinel] Sending event:", envelope);
|
|
119
|
+
}
|
|
120
|
+
let endpoint = "/api/ingest/events";
|
|
121
|
+
const controller = new AbortController();
|
|
122
|
+
const timeoutId = setTimeout(() => controller.abort(), 5e3);
|
|
123
|
+
let ingestUrl = window.SENTINEL_API_URL || "http://localhost:3001/api/ingest/events";
|
|
124
|
+
if (window.location.hostname === "localhost") {
|
|
125
|
+
ingestUrl = "/api/ingest/events";
|
|
126
|
+
}
|
|
127
|
+
fetch(ingestUrl, {
|
|
128
|
+
method: "POST",
|
|
129
|
+
body: JSON.stringify(envelope),
|
|
130
|
+
headers: {
|
|
131
|
+
"Content-Type": "application/json"
|
|
132
|
+
},
|
|
133
|
+
keepalive: true,
|
|
134
|
+
signal: controller.signal
|
|
135
|
+
}).catch((err) => {
|
|
136
|
+
if (this.config.debug) console.error("[Sentinel] Failed to send event:", err);
|
|
137
|
+
}).finally(() => clearTimeout(timeoutId));
|
|
138
|
+
}
|
|
139
|
+
setupGlobalHandlers() {
|
|
140
|
+
const self = this;
|
|
141
|
+
const oldOnError = window.onerror;
|
|
142
|
+
window.onerror = function(msg, url, line, col, error) {
|
|
143
|
+
self.captureException(error || String(msg));
|
|
144
|
+
if (oldOnError) return oldOnError.apply(this, arguments);
|
|
145
|
+
};
|
|
146
|
+
window.onunhandledrejection = function(e) {
|
|
147
|
+
self.captureException(e.reason || "Unhandled Promise Rejection");
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
setupBreadcrumbs() {
|
|
151
|
+
const self = this;
|
|
152
|
+
document.addEventListener("click", (e) => {
|
|
153
|
+
let target = e.target;
|
|
154
|
+
while (target && target !== document.body && !target.id && !target.className) {
|
|
155
|
+
target = target.parentElement;
|
|
156
|
+
}
|
|
157
|
+
if (!target) return;
|
|
158
|
+
const safeSummary = (target.id ? "#" + target.id : "") + (target.className ? "." + target.className.split(" ").join(".") : "") || target.tagName;
|
|
159
|
+
self.addBreadcrumb({
|
|
160
|
+
category: "ui.click",
|
|
161
|
+
message: safeSummary,
|
|
162
|
+
level: "info"
|
|
163
|
+
});
|
|
164
|
+
}, true);
|
|
165
|
+
const levels = ["log", "info", "warn", "error", "debug"];
|
|
166
|
+
levels.forEach((level) => {
|
|
167
|
+
const original = console[level];
|
|
168
|
+
console[level] = function(...args) {
|
|
169
|
+
self.addBreadcrumb({
|
|
170
|
+
category: "console",
|
|
171
|
+
message: args.map((a) => String(a)).join(" "),
|
|
172
|
+
level: level === "error" ? "error" : level === "warn" ? "warning" : "info"
|
|
173
|
+
});
|
|
174
|
+
original.apply(console, args);
|
|
175
|
+
};
|
|
176
|
+
});
|
|
177
|
+
const originalFetch = window.fetch;
|
|
178
|
+
window.fetch = async function(...args) {
|
|
179
|
+
const [resource, config] = args;
|
|
180
|
+
const method = (config?.method || "GET").toUpperCase();
|
|
181
|
+
const url = String(resource);
|
|
182
|
+
self.addBreadcrumb({
|
|
183
|
+
category: "http",
|
|
184
|
+
message: `${method} ${url}`,
|
|
185
|
+
level: "info",
|
|
186
|
+
data: {
|
|
187
|
+
method,
|
|
188
|
+
url
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
try {
|
|
192
|
+
const response = await originalFetch.apply(this, args);
|
|
193
|
+
self.addBreadcrumb({
|
|
194
|
+
category: "http.response",
|
|
195
|
+
message: `${method} ${url} [${response.status}]`,
|
|
196
|
+
level: response.status >= 400 ? "error" : "info",
|
|
197
|
+
data: { status: response.status }
|
|
198
|
+
});
|
|
199
|
+
return response;
|
|
200
|
+
} catch (err) {
|
|
201
|
+
self.addBreadcrumb({
|
|
202
|
+
category: "http.error",
|
|
203
|
+
message: `${method} ${url} FAILED`,
|
|
204
|
+
level: "error",
|
|
205
|
+
data: { error: String(err) }
|
|
206
|
+
});
|
|
207
|
+
throw err;
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
var sentinel = Sentinel.getInstance();
|
|
213
|
+
var init = (config) => sentinel.init(config);
|
|
214
|
+
var captureException = (error, context) => sentinel.captureException(error, context);
|
|
215
|
+
var captureMessage = (msg, level) => sentinel.captureMessage(msg, level);
|
|
216
|
+
var setUser = (user) => sentinel.setUser(user);
|
|
217
|
+
var setTag = (key, value) => sentinel.setTag(key, value);
|
|
218
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
219
|
+
0 && (module.exports = {
|
|
220
|
+
Sentinel,
|
|
221
|
+
captureException,
|
|
222
|
+
captureMessage,
|
|
223
|
+
init,
|
|
224
|
+
sentinel,
|
|
225
|
+
setTag,
|
|
226
|
+
setUser
|
|
227
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var Sentinel = class _Sentinel {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.config = {
|
|
5
|
+
dsn: "",
|
|
6
|
+
environment: "production",
|
|
7
|
+
maxBreadcrumbs: 20
|
|
8
|
+
};
|
|
9
|
+
this.breadcrumbs = [];
|
|
10
|
+
this.user = {};
|
|
11
|
+
this.tags = {};
|
|
12
|
+
this.initialized = false;
|
|
13
|
+
}
|
|
14
|
+
static getInstance() {
|
|
15
|
+
if (!_Sentinel.instance) {
|
|
16
|
+
_Sentinel.instance = new _Sentinel();
|
|
17
|
+
}
|
|
18
|
+
return _Sentinel.instance;
|
|
19
|
+
}
|
|
20
|
+
init(config) {
|
|
21
|
+
if (this.initialized) {
|
|
22
|
+
console.warn("[Sentinel] Already initialized");
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
this.config = { ...this.config, ...config };
|
|
26
|
+
this.initialized = true;
|
|
27
|
+
if (this.config.debug) {
|
|
28
|
+
console.log("[Sentinel] Initialized with DSN:", this.config.dsn);
|
|
29
|
+
}
|
|
30
|
+
this.setupGlobalHandlers();
|
|
31
|
+
this.setupBreadcrumbs();
|
|
32
|
+
}
|
|
33
|
+
setUser(user) {
|
|
34
|
+
this.user = user;
|
|
35
|
+
}
|
|
36
|
+
setTag(key, value) {
|
|
37
|
+
this.tags[key] = value;
|
|
38
|
+
}
|
|
39
|
+
addBreadcrumb(breadcrumb) {
|
|
40
|
+
const crumb = {
|
|
41
|
+
...breadcrumb,
|
|
42
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
43
|
+
};
|
|
44
|
+
this.breadcrumbs.push(crumb);
|
|
45
|
+
if (this.breadcrumbs.length > (this.config.maxBreadcrumbs || 20)) {
|
|
46
|
+
this.breadcrumbs.shift();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
captureException(error, context) {
|
|
50
|
+
const stackTrace = error instanceof Error ? error.stack : "No stack trace available";
|
|
51
|
+
const title = error instanceof Error ? error.message : String(error);
|
|
52
|
+
const type = error instanceof Error ? error.name : "Error";
|
|
53
|
+
this.sendEvent({
|
|
54
|
+
title,
|
|
55
|
+
location: window.location.href,
|
|
56
|
+
// Simplified location
|
|
57
|
+
type: "Exception",
|
|
58
|
+
severity: "error",
|
|
59
|
+
stackTrace,
|
|
60
|
+
tags: { ...this.tags, ...context?.tags },
|
|
61
|
+
breadcrumbs: [...this.breadcrumbs],
|
|
62
|
+
usersAffected: 1
|
|
63
|
+
// Client side always 1 user context usually
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
captureMessage(message, level = "info") {
|
|
67
|
+
this.sendEvent({
|
|
68
|
+
title: message,
|
|
69
|
+
location: window.location.href,
|
|
70
|
+
type: "Error",
|
|
71
|
+
severity: level,
|
|
72
|
+
stackTrace: new Error().stack,
|
|
73
|
+
// Capture stack for context
|
|
74
|
+
tags: this.tags,
|
|
75
|
+
breadcrumbs: [...this.breadcrumbs]
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
sendEvent(payload) {
|
|
79
|
+
if (!this.initialized || !this.config.dsn) return;
|
|
80
|
+
if (this.config.environment) payload.tags = { ...payload.tags, environment: this.config.environment };
|
|
81
|
+
if (this.config.release) payload.tags = { ...payload.tags, release: this.config.release };
|
|
82
|
+
if (this.user.id) payload.tags = { ...payload.tags, userId: this.user.id };
|
|
83
|
+
const envelope = {
|
|
84
|
+
dsn: this.config.dsn,
|
|
85
|
+
events: [payload]
|
|
86
|
+
};
|
|
87
|
+
if (this.config.debug) {
|
|
88
|
+
console.log("[Sentinel] Sending event:", envelope);
|
|
89
|
+
}
|
|
90
|
+
let endpoint = "/api/ingest/events";
|
|
91
|
+
const controller = new AbortController();
|
|
92
|
+
const timeoutId = setTimeout(() => controller.abort(), 5e3);
|
|
93
|
+
let ingestUrl = window.SENTINEL_API_URL || "http://localhost:3001/api/ingest/events";
|
|
94
|
+
if (window.location.hostname === "localhost") {
|
|
95
|
+
ingestUrl = "/api/ingest/events";
|
|
96
|
+
}
|
|
97
|
+
fetch(ingestUrl, {
|
|
98
|
+
method: "POST",
|
|
99
|
+
body: JSON.stringify(envelope),
|
|
100
|
+
headers: {
|
|
101
|
+
"Content-Type": "application/json"
|
|
102
|
+
},
|
|
103
|
+
keepalive: true,
|
|
104
|
+
signal: controller.signal
|
|
105
|
+
}).catch((err) => {
|
|
106
|
+
if (this.config.debug) console.error("[Sentinel] Failed to send event:", err);
|
|
107
|
+
}).finally(() => clearTimeout(timeoutId));
|
|
108
|
+
}
|
|
109
|
+
setupGlobalHandlers() {
|
|
110
|
+
const self = this;
|
|
111
|
+
const oldOnError = window.onerror;
|
|
112
|
+
window.onerror = function(msg, url, line, col, error) {
|
|
113
|
+
self.captureException(error || String(msg));
|
|
114
|
+
if (oldOnError) return oldOnError.apply(this, arguments);
|
|
115
|
+
};
|
|
116
|
+
window.onunhandledrejection = function(e) {
|
|
117
|
+
self.captureException(e.reason || "Unhandled Promise Rejection");
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
setupBreadcrumbs() {
|
|
121
|
+
const self = this;
|
|
122
|
+
document.addEventListener("click", (e) => {
|
|
123
|
+
let target = e.target;
|
|
124
|
+
while (target && target !== document.body && !target.id && !target.className) {
|
|
125
|
+
target = target.parentElement;
|
|
126
|
+
}
|
|
127
|
+
if (!target) return;
|
|
128
|
+
const safeSummary = (target.id ? "#" + target.id : "") + (target.className ? "." + target.className.split(" ").join(".") : "") || target.tagName;
|
|
129
|
+
self.addBreadcrumb({
|
|
130
|
+
category: "ui.click",
|
|
131
|
+
message: safeSummary,
|
|
132
|
+
level: "info"
|
|
133
|
+
});
|
|
134
|
+
}, true);
|
|
135
|
+
const levels = ["log", "info", "warn", "error", "debug"];
|
|
136
|
+
levels.forEach((level) => {
|
|
137
|
+
const original = console[level];
|
|
138
|
+
console[level] = function(...args) {
|
|
139
|
+
self.addBreadcrumb({
|
|
140
|
+
category: "console",
|
|
141
|
+
message: args.map((a) => String(a)).join(" "),
|
|
142
|
+
level: level === "error" ? "error" : level === "warn" ? "warning" : "info"
|
|
143
|
+
});
|
|
144
|
+
original.apply(console, args);
|
|
145
|
+
};
|
|
146
|
+
});
|
|
147
|
+
const originalFetch = window.fetch;
|
|
148
|
+
window.fetch = async function(...args) {
|
|
149
|
+
const [resource, config] = args;
|
|
150
|
+
const method = (config?.method || "GET").toUpperCase();
|
|
151
|
+
const url = String(resource);
|
|
152
|
+
self.addBreadcrumb({
|
|
153
|
+
category: "http",
|
|
154
|
+
message: `${method} ${url}`,
|
|
155
|
+
level: "info",
|
|
156
|
+
data: {
|
|
157
|
+
method,
|
|
158
|
+
url
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
try {
|
|
162
|
+
const response = await originalFetch.apply(this, args);
|
|
163
|
+
self.addBreadcrumb({
|
|
164
|
+
category: "http.response",
|
|
165
|
+
message: `${method} ${url} [${response.status}]`,
|
|
166
|
+
level: response.status >= 400 ? "error" : "info",
|
|
167
|
+
data: { status: response.status }
|
|
168
|
+
});
|
|
169
|
+
return response;
|
|
170
|
+
} catch (err) {
|
|
171
|
+
self.addBreadcrumb({
|
|
172
|
+
category: "http.error",
|
|
173
|
+
message: `${method} ${url} FAILED`,
|
|
174
|
+
level: "error",
|
|
175
|
+
data: { error: String(err) }
|
|
176
|
+
});
|
|
177
|
+
throw err;
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
var sentinel = Sentinel.getInstance();
|
|
183
|
+
var init = (config) => sentinel.init(config);
|
|
184
|
+
var captureException = (error, context) => sentinel.captureException(error, context);
|
|
185
|
+
var captureMessage = (msg, level) => sentinel.captureMessage(msg, level);
|
|
186
|
+
var setUser = (user) => sentinel.setUser(user);
|
|
187
|
+
var setTag = (key, value) => sentinel.setTag(key, value);
|
|
188
|
+
export {
|
|
189
|
+
Sentinel,
|
|
190
|
+
captureException,
|
|
191
|
+
captureMessage,
|
|
192
|
+
init,
|
|
193
|
+
sentinel,
|
|
194
|
+
setTag,
|
|
195
|
+
setUser
|
|
196
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@builderai/sentinel-browser",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Official browser SDK for Sentinel AI error tracking",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
10
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
11
|
+
"lint": "eslint src/**/*.ts",
|
|
12
|
+
"test": "vitest run"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"error-tracking",
|
|
22
|
+
"logging",
|
|
23
|
+
"monitoring",
|
|
24
|
+
"sentinel-ai"
|
|
25
|
+
],
|
|
26
|
+
"author": "Sentinel AI",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"happy-dom": "^20.5.3",
|
|
30
|
+
"tsup": "^8.0.2",
|
|
31
|
+
"typescript": "^5.3.3",
|
|
32
|
+
"vitest": "^1.3.1"
|
|
33
|
+
}
|
|
34
|
+
}
|