@devmoods/sentry 0.5.0 → 0.6.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.
@@ -1,210 +1,205 @@
1
1
  /* eslint-disable */
2
- var addSorting = (function() {
3
- 'use strict';
4
- var cols,
5
- currentSort = {
6
- index: 0,
7
- desc: false
8
- };
2
+ var addSorting = (function () {
3
+ "use strict";
4
+ var cols,
5
+ currentSort = {
6
+ index: 0,
7
+ desc: false,
8
+ };
9
9
 
10
- // returns the summary table element
11
- function getTable() {
12
- return document.querySelector('.coverage-summary');
13
- }
14
- // returns the thead element of the summary table
15
- function getTableHeader() {
16
- return getTable().querySelector('thead tr');
17
- }
18
- // returns the tbody element of the summary table
19
- function getTableBody() {
20
- return getTable().querySelector('tbody');
21
- }
22
- // returns the th element for nth column
23
- function getNthColumn(n) {
24
- return getTableHeader().querySelectorAll('th')[n];
10
+ // returns the summary table element
11
+ function getTable() {
12
+ return document.querySelector(".coverage-summary");
13
+ }
14
+ // returns the thead element of the summary table
15
+ function getTableHeader() {
16
+ return getTable().querySelector("thead tr");
17
+ }
18
+ // returns the tbody element of the summary table
19
+ function getTableBody() {
20
+ return getTable().querySelector("tbody");
21
+ }
22
+ // returns the th element for nth column
23
+ function getNthColumn(n) {
24
+ return getTableHeader().querySelectorAll("th")[n];
25
+ }
26
+
27
+ function onFilterInput() {
28
+ const searchValue = document.getElementById("fileSearch").value;
29
+ const rows = document.getElementsByTagName("tbody")[0].children;
30
+
31
+ // Try to create a RegExp from the searchValue. If it fails (invalid regex),
32
+ // it will be treated as a plain text search
33
+ let searchRegex;
34
+ try {
35
+ searchRegex = new RegExp(searchValue, "i"); // 'i' for case-insensitive
36
+ } catch (error) {
37
+ searchRegex = null;
25
38
  }
26
39
 
27
- function onFilterInput() {
28
- const searchValue = document.getElementById('fileSearch').value;
29
- const rows = document.getElementsByTagName('tbody')[0].children;
30
-
31
- // Try to create a RegExp from the searchValue. If it fails (invalid regex),
32
- // it will be treated as a plain text search
33
- let searchRegex;
34
- try {
35
- searchRegex = new RegExp(searchValue, 'i'); // 'i' for case-insensitive
36
- } catch (error) {
37
- searchRegex = null;
38
- }
40
+ for (let i = 0; i < rows.length; i++) {
41
+ const row = rows[i];
42
+ let isMatch = false;
39
43
 
40
- for (let i = 0; i < rows.length; i++) {
41
- const row = rows[i];
42
- let isMatch = false;
43
-
44
- if (searchRegex) {
45
- // If a valid regex was created, use it for matching
46
- isMatch = searchRegex.test(row.textContent);
47
- } else {
48
- // Otherwise, fall back to the original plain text search
49
- isMatch = row.textContent
50
- .toLowerCase()
51
- .includes(searchValue.toLowerCase());
52
- }
53
-
54
- row.style.display = isMatch ? '' : 'none';
55
- }
56
- }
44
+ if (searchRegex) {
45
+ // If a valid regex was created, use it for matching
46
+ isMatch = searchRegex.test(row.textContent);
47
+ } else {
48
+ // Otherwise, fall back to the original plain text search
49
+ isMatch = row.textContent.toLowerCase().includes(searchValue.toLowerCase());
50
+ }
57
51
 
58
- // loads the search box
59
- function addSearchBox() {
60
- var template = document.getElementById('filterTemplate');
61
- var templateClone = template.content.cloneNode(true);
62
- templateClone.getElementById('fileSearch').oninput = onFilterInput;
63
- template.parentElement.appendChild(templateClone);
52
+ row.style.display = isMatch ? "" : "none";
64
53
  }
65
-
66
- // loads all columns
67
- function loadColumns() {
68
- var colNodes = getTableHeader().querySelectorAll('th'),
69
- colNode,
70
- cols = [],
71
- col,
72
- i;
73
-
74
- for (i = 0; i < colNodes.length; i += 1) {
75
- colNode = colNodes[i];
76
- col = {
77
- key: colNode.getAttribute('data-col'),
78
- sortable: !colNode.getAttribute('data-nosort'),
79
- type: colNode.getAttribute('data-type') || 'string'
80
- };
81
- cols.push(col);
82
- if (col.sortable) {
83
- col.defaultDescSort = col.type === 'number';
84
- colNode.innerHTML =
85
- colNode.innerHTML + '<span class="sorter"></span>';
86
- }
87
- }
88
- return cols;
54
+ }
55
+
56
+ // loads the search box
57
+ function addSearchBox() {
58
+ var template = document.getElementById("filterTemplate");
59
+ var templateClone = template.content.cloneNode(true);
60
+ templateClone.getElementById("fileSearch").oninput = onFilterInput;
61
+ template.parentElement.appendChild(templateClone);
62
+ }
63
+
64
+ // loads all columns
65
+ function loadColumns() {
66
+ var colNodes = getTableHeader().querySelectorAll("th"),
67
+ colNode,
68
+ cols = [],
69
+ col,
70
+ i;
71
+
72
+ for (i = 0; i < colNodes.length; i += 1) {
73
+ colNode = colNodes[i];
74
+ col = {
75
+ key: colNode.getAttribute("data-col"),
76
+ sortable: !colNode.getAttribute("data-nosort"),
77
+ type: colNode.getAttribute("data-type") || "string",
78
+ };
79
+ cols.push(col);
80
+ if (col.sortable) {
81
+ col.defaultDescSort = col.type === "number";
82
+ colNode.innerHTML = colNode.innerHTML + '<span class="sorter"></span>';
83
+ }
89
84
  }
90
- // attaches a data attribute to every tr element with an object
91
- // of data values keyed by column name
92
- function loadRowData(tableRow) {
93
- var tableCols = tableRow.querySelectorAll('td'),
94
- colNode,
95
- col,
96
- data = {},
97
- i,
98
- val;
99
- for (i = 0; i < tableCols.length; i += 1) {
100
- colNode = tableCols[i];
101
- col = cols[i];
102
- val = colNode.getAttribute('data-value');
103
- if (col.type === 'number') {
104
- val = Number(val);
105
- }
106
- data[col.key] = val;
107
- }
108
- return data;
85
+ return cols;
86
+ }
87
+ // attaches a data attribute to every tr element with an object
88
+ // of data values keyed by column name
89
+ function loadRowData(tableRow) {
90
+ var tableCols = tableRow.querySelectorAll("td"),
91
+ colNode,
92
+ col,
93
+ data = {},
94
+ i,
95
+ val;
96
+ for (i = 0; i < tableCols.length; i += 1) {
97
+ colNode = tableCols[i];
98
+ col = cols[i];
99
+ val = colNode.getAttribute("data-value");
100
+ if (col.type === "number") {
101
+ val = Number(val);
102
+ }
103
+ data[col.key] = val;
109
104
  }
110
- // loads all row data
111
- function loadData() {
112
- var rows = getTableBody().querySelectorAll('tr'),
113
- i;
114
-
115
- for (i = 0; i < rows.length; i += 1) {
116
- rows[i].data = loadRowData(rows[i]);
117
- }
105
+ return data;
106
+ }
107
+ // loads all row data
108
+ function loadData() {
109
+ var rows = getTableBody().querySelectorAll("tr"),
110
+ i;
111
+
112
+ for (i = 0; i < rows.length; i += 1) {
113
+ rows[i].data = loadRowData(rows[i]);
118
114
  }
119
- // sorts the table using the data for the ith column
120
- function sortByIndex(index, desc) {
121
- var key = cols[index].key,
122
- sorter = function(a, b) {
123
- a = a.data[key];
124
- b = b.data[key];
125
- return a < b ? -1 : a > b ? 1 : 0;
126
- },
127
- finalSorter = sorter,
128
- tableBody = document.querySelector('.coverage-summary tbody'),
129
- rowNodes = tableBody.querySelectorAll('tr'),
130
- rows = [],
131
- i;
132
-
133
- if (desc) {
134
- finalSorter = function(a, b) {
135
- return -1 * sorter(a, b);
136
- };
137
- }
138
-
139
- for (i = 0; i < rowNodes.length; i += 1) {
140
- rows.push(rowNodes[i]);
141
- tableBody.removeChild(rowNodes[i]);
142
- }
143
-
144
- rows.sort(finalSorter);
145
-
146
- for (i = 0; i < rows.length; i += 1) {
147
- tableBody.appendChild(rows[i]);
148
- }
115
+ }
116
+ // sorts the table using the data for the ith column
117
+ function sortByIndex(index, desc) {
118
+ var key = cols[index].key,
119
+ sorter = function (a, b) {
120
+ a = a.data[key];
121
+ b = b.data[key];
122
+ return a < b ? -1 : a > b ? 1 : 0;
123
+ },
124
+ finalSorter = sorter,
125
+ tableBody = document.querySelector(".coverage-summary tbody"),
126
+ rowNodes = tableBody.querySelectorAll("tr"),
127
+ rows = [],
128
+ i;
129
+
130
+ if (desc) {
131
+ finalSorter = function (a, b) {
132
+ return -1 * sorter(a, b);
133
+ };
149
134
  }
150
- // removes sort indicators for current column being sorted
151
- function removeSortIndicators() {
152
- var col = getNthColumn(currentSort.index),
153
- cls = col.className;
154
135
 
155
- cls = cls.replace(/ sorted$/, '').replace(/ sorted-desc$/, '');
156
- col.className = cls;
136
+ for (i = 0; i < rowNodes.length; i += 1) {
137
+ rows.push(rowNodes[i]);
138
+ tableBody.removeChild(rowNodes[i]);
157
139
  }
158
- // adds sort indicators for current column being sorted
159
- function addSortIndicators() {
160
- getNthColumn(currentSort.index).className += currentSort.desc
161
- ? ' sorted-desc'
162
- : ' sorted';
140
+
141
+ rows.sort(finalSorter);
142
+
143
+ for (i = 0; i < rows.length; i += 1) {
144
+ tableBody.appendChild(rows[i]);
163
145
  }
164
- // adds event listeners for all sorter widgets
165
- function enableUI() {
166
- var i,
167
- el,
168
- ithSorter = function ithSorter(i) {
169
- var col = cols[i];
170
-
171
- return function() {
172
- var desc = col.defaultDescSort;
173
-
174
- if (currentSort.index === i) {
175
- desc = !currentSort.desc;
176
- }
177
- sortByIndex(i, desc);
178
- removeSortIndicators();
179
- currentSort.index = i;
180
- currentSort.desc = desc;
181
- addSortIndicators();
182
- };
183
- };
184
- for (i = 0; i < cols.length; i += 1) {
185
- if (cols[i].sortable) {
186
- // add the click event handler on the th so users
187
- // dont have to click on those tiny arrows
188
- el = getNthColumn(i).querySelector('.sorter').parentElement;
189
- if (el.addEventListener) {
190
- el.addEventListener('click', ithSorter(i));
191
- } else {
192
- el.attachEvent('onclick', ithSorter(i));
193
- }
194
- }
146
+ }
147
+ // removes sort indicators for current column being sorted
148
+ function removeSortIndicators() {
149
+ var col = getNthColumn(currentSort.index),
150
+ cls = col.className;
151
+
152
+ cls = cls.replace(/ sorted$/, "").replace(/ sorted-desc$/, "");
153
+ col.className = cls;
154
+ }
155
+ // adds sort indicators for current column being sorted
156
+ function addSortIndicators() {
157
+ getNthColumn(currentSort.index).className += currentSort.desc ? " sorted-desc" : " sorted";
158
+ }
159
+ // adds event listeners for all sorter widgets
160
+ function enableUI() {
161
+ var i,
162
+ el,
163
+ ithSorter = function ithSorter(i) {
164
+ var col = cols[i];
165
+
166
+ return function () {
167
+ var desc = col.defaultDescSort;
168
+
169
+ if (currentSort.index === i) {
170
+ desc = !currentSort.desc;
171
+ }
172
+ sortByIndex(i, desc);
173
+ removeSortIndicators();
174
+ currentSort.index = i;
175
+ currentSort.desc = desc;
176
+ addSortIndicators();
177
+ };
178
+ };
179
+ for (i = 0; i < cols.length; i += 1) {
180
+ if (cols[i].sortable) {
181
+ // add the click event handler on the th so users
182
+ // dont have to click on those tiny arrows
183
+ el = getNthColumn(i).querySelector(".sorter").parentElement;
184
+ if (el.addEventListener) {
185
+ el.addEventListener("click", ithSorter(i));
186
+ } else {
187
+ el.attachEvent("onclick", ithSorter(i));
195
188
  }
189
+ }
196
190
  }
197
- // adds sorting functionality to the UI
198
- return function() {
199
- if (!getTable()) {
200
- return;
201
- }
202
- cols = loadColumns();
203
- loadData();
204
- addSearchBox();
205
- addSortIndicators();
206
- enableUI();
207
- };
191
+ }
192
+ // adds sorting functionality to the UI
193
+ return function () {
194
+ if (!getTable()) {
195
+ return;
196
+ }
197
+ cols = loadColumns();
198
+ loadData();
199
+ addSearchBox();
200
+ addSortIndicators();
201
+ enableUI();
202
+ };
208
203
  })();
209
204
 
210
- window.addEventListener('load', addSorting);
205
+ window.addEventListener("load", addSorting);
package/dist/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { type LoggerAdapter, type MetricAttributes, type MetricsAdapter } from '@devmoods/observability';
2
2
  import * as Sentry from '@sentry/node';
3
- import { type ErrorRequestHandler } from 'express';
4
3
  declare module '@devmoods/config' {
5
4
  interface Config extends ConfigFromDefs<typeof sentryConfig> {
6
5
  }
@@ -17,7 +16,6 @@ export interface SentryOptions extends Sentry.NodeOptions {
17
16
  export declare function initSentry(options?: SentryOptions): Promise<void>;
18
17
  export declare function closeSentry(): Promise<void>;
19
18
  export declare function sentryOptions(options?: SentryOptions): Promise<SentryOptions>;
20
- export declare function sentryErrorHandler(options?: Sentry.NodeOptions): ErrorRequestHandler;
21
19
  /**
22
20
  * MetricsAdapter backed by `Sentry.metrics` (available in @sentry/node >=10.25.0).
23
21
  * Requires `Sentry.init()` to have been called; calls are silently dropped when
package/dist/index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import { config, define, t } from '@devmoods/config';
2
2
  import {} from '@devmoods/observability';
3
3
  import * as Sentry from '@sentry/node';
4
- import {} from 'express';
5
4
  export const sentryConfig = define({
6
5
  SENTRY_DSN: t.string(),
7
6
  SENTRY_TRACES_SAMPLE_RATE: t.number(0.3),
@@ -48,18 +47,29 @@ export async function sentryOptions(options = {}) {
48
47
  dsn: config.SENTRY_DSN,
49
48
  tracesSampleRate: config.SENTRY_TRACES_SAMPLE_RATE,
50
49
  profileSessionSampleRate: config.SENTRY_PROFILES_SAMPLE_RATE,
51
- integrations: nativeIntegrations,
52
- enableLogs: true,
50
+ integrations: [
51
+ ...nativeIntegrations,
52
+ Sentry.expressIntegration({
53
+ shouldHandleError(error) {
54
+ const status = getHttpStatusCode(error);
55
+ return (process.env.NODE_ENV === 'production' &&
56
+ (status == null || status < 400 || status >= 500));
57
+ },
58
+ }),
59
+ ],
53
60
  ...options,
54
61
  };
55
62
  }
56
- export function sentryErrorHandler(options = {}) {
57
- return Sentry.expressErrorHandler({
58
- shouldHandleError() {
59
- return process.env.NODE_ENV === 'production';
60
- },
61
- ...options,
62
- });
63
+ function getHttpStatusCode(error) {
64
+ if (error == null || typeof error !== 'object') {
65
+ return undefined;
66
+ }
67
+ const { status, statusCode, status_code: statusCodeSnake, output, } = error;
68
+ const value = status ?? statusCode ?? statusCodeSnake ?? output?.statusCode;
69
+ const parsed = typeof value === 'string' ? Number.parseInt(value, 10) : value;
70
+ return typeof parsed === 'number' && Number.isFinite(parsed)
71
+ ? parsed
72
+ : undefined;
63
73
  }
64
74
  /**
65
75
  * MetricsAdapter backed by `Sentry.metrics` (available in @sentry/node >=10.25.0).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devmoods/sentry",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -23,14 +23,14 @@
23
23
  },
24
24
  "dependencies": {
25
25
  "@devmoods/config": "0.3.0",
26
- "@devmoods/observability": "0.4.0",
27
- "@sentry/node": "^10.63.0",
28
- "@sentry/node-native": "^10.63.0",
29
- "@sentry/profiling-node": "^10.63.0",
26
+ "@devmoods/observability": "0.5.0",
27
+ "@sentry/node": "^11.5.0",
28
+ "@sentry/node-native": "^11.5.0",
29
+ "@sentry/profiling-node": "^11.5.0",
30
30
  "express": "^4.21.2"
31
31
  },
32
32
  "peerDependencies": {
33
33
  "express": ">=4.21.2 <5.0.0"
34
34
  },
35
- "gitHead": "52519bed307a1eedf13ab334f11c1ad9c7c8db91"
35
+ "gitHead": "60fe0c3459c81c7d71a4423cdc66d046d166de75"
36
36
  }
@@ -9,7 +9,11 @@ async function loadModuleWithSentryMock(initialized = true) {
9
9
  init: vi.fn(),
10
10
  close: vi.fn(),
11
11
  isInitialized: vi.fn(() => initialized),
12
- expressErrorHandler: vi.fn(() => vi.fn()),
12
+ expressIntegration: vi.fn<
13
+ (options: { shouldHandleError(error: unknown): boolean }) => {
14
+ name: string;
15
+ }
16
+ >(() => ({ name: 'Express' })),
13
17
  captureException: vi.fn(),
14
18
  logger: {
15
19
  trace: vi.fn(),
@@ -49,7 +53,6 @@ test('sentryOptions', async () => {
49
53
  expect(options).toEqual(
50
54
  expect.objectContaining({
51
55
  dsn: 'https://example.com/sentry',
52
- enableLogs: true,
53
56
  integrations: expect.any(Array),
54
57
  profileSessionSampleRate: 0.3,
55
58
  tracesSampleRate: 0.3,
@@ -59,6 +62,61 @@ test('sentryOptions', async () => {
59
62
  reset();
60
63
  });
61
64
 
65
+ describe('Express integration', () => {
66
+ beforeEach(() => {
67
+ vi.unstubAllEnvs();
68
+ });
69
+
70
+ test('ignores HTTP 40x errors in production', async () => {
71
+ vi.stubEnv('NODE_ENV', 'production');
72
+
73
+ const { Sentry, sentryOptions: getSentryOptions } =
74
+ await loadModuleWithSentryMock();
75
+
76
+ const sdkOptions = await getSentryOptions();
77
+
78
+ expect(sdkOptions.integrations).toContain(
79
+ Sentry.expressIntegration.mock.results[0]!.value,
80
+ );
81
+
82
+ const options = Sentry.expressIntegration.mock.calls[0]![0];
83
+ expect(options.shouldHandleError({ status: 400 })).toEqual(false);
84
+ expect(options.shouldHandleError({ statusCode: 401 })).toEqual(false);
85
+ expect(options.shouldHandleError({ status_code: '403' })).toEqual(false);
86
+ expect(options.shouldHandleError({ output: { statusCode: 429 } })).toEqual(
87
+ false,
88
+ );
89
+ });
90
+
91
+ test('handles server and unknown errors in production', async () => {
92
+ vi.stubEnv('NODE_ENV', 'production');
93
+
94
+ const { Sentry, sentryOptions: getSentryOptions } =
95
+ await loadModuleWithSentryMock();
96
+
97
+ await getSentryOptions();
98
+
99
+ const options = Sentry.expressIntegration.mock.calls[0]![0];
100
+ expect(options.shouldHandleError(new Error('boom'))).toEqual(true);
101
+ expect(options.shouldHandleError({ status: 500 })).toEqual(true);
102
+ expect(options.shouldHandleError({ status: 399 })).toEqual(true);
103
+ expect(options.shouldHandleError({ status: 'invalid' })).toEqual(true);
104
+ expect(options.shouldHandleError(null)).toEqual(true);
105
+ });
106
+
107
+ test('does not handle errors outside production', async () => {
108
+ vi.stubEnv('NODE_ENV', 'test');
109
+
110
+ const { Sentry, sentryOptions: getSentryOptions } =
111
+ await loadModuleWithSentryMock();
112
+
113
+ await getSentryOptions();
114
+
115
+ const options = Sentry.expressIntegration.mock.calls[0]![0];
116
+ expect(options.shouldHandleError({ status: 500 })).toEqual(false);
117
+ });
118
+ });
119
+
62
120
  describe('SentryMetricsAdapter', () => {
63
121
  beforeEach(() => {
64
122
  vi.restoreAllMocks();
package/src/index.ts CHANGED
@@ -5,7 +5,6 @@ import {
5
5
  type MetricsAdapter,
6
6
  } from '@devmoods/observability';
7
7
  import * as Sentry from '@sentry/node';
8
- import { type ErrorRequestHandler } from 'express';
9
8
 
10
9
  declare module '@devmoods/config' {
11
10
  interface Config extends ConfigFromDefs<typeof sentryConfig> {}
@@ -71,21 +70,45 @@ export async function sentryOptions(
71
70
  dsn: config.SENTRY_DSN,
72
71
  tracesSampleRate: config.SENTRY_TRACES_SAMPLE_RATE,
73
72
  profileSessionSampleRate: config.SENTRY_PROFILES_SAMPLE_RATE,
74
- integrations: nativeIntegrations,
75
- enableLogs: true,
73
+ integrations: [
74
+ ...nativeIntegrations,
75
+ Sentry.expressIntegration({
76
+ shouldHandleError(error) {
77
+ const status = getHttpStatusCode(error);
78
+
79
+ return (
80
+ process.env.NODE_ENV === 'production' &&
81
+ (status == null || status < 400 || status >= 500)
82
+ );
83
+ },
84
+ }),
85
+ ],
76
86
  ...options,
77
87
  };
78
88
  }
79
89
 
80
- export function sentryErrorHandler(
81
- options: Sentry.NodeOptions = {},
82
- ): ErrorRequestHandler {
83
- return Sentry.expressErrorHandler({
84
- shouldHandleError() {
85
- return process.env.NODE_ENV === 'production';
86
- },
87
- ...options,
88
- }) as unknown as ErrorRequestHandler;
90
+ function getHttpStatusCode(error: unknown) {
91
+ if (error == null || typeof error !== 'object') {
92
+ return undefined;
93
+ }
94
+
95
+ const {
96
+ status,
97
+ statusCode,
98
+ status_code: statusCodeSnake,
99
+ output,
100
+ } = error as {
101
+ status?: number | string;
102
+ statusCode?: number | string;
103
+ status_code?: number | string;
104
+ output?: { statusCode?: number | string };
105
+ };
106
+ const value = status ?? statusCode ?? statusCodeSnake ?? output?.statusCode;
107
+ const parsed = typeof value === 'string' ? Number.parseInt(value, 10) : value;
108
+
109
+ return typeof parsed === 'number' && Number.isFinite(parsed)
110
+ ? parsed
111
+ : undefined;
89
112
  }
90
113
 
91
114
  /**