Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,99 @@
.start()
.completed();
});

describe('Worker thread error handling', () => {
test.each(['mjs', 'js'])('should not interfere with worker thread error handling ".%s"', async extension => {

Check failure on line 106 in dev-packages/node-integration-tests/suites/public-api/OnUncaughtException/test.ts

View workflow job for this annotation

GitHub Actions / Node (18) Integration Tests

suites/public-api/OnUncaughtException/test.ts > OnUncaughtException integration > Worker thread error handling > should not interfere with worker thread error handling ".js"

Error: Test timed out in 15000ms. If this is a long-running test, pass a timeout value as the last argument or configure it globally with "testTimeout". ❯ suites/public-api/OnUncaughtException/test.ts:106:28

Check failure on line 106 in dev-packages/node-integration-tests/suites/public-api/OnUncaughtException/test.ts

View workflow job for this annotation

GitHub Actions / Node (18) Integration Tests

suites/public-api/OnUncaughtException/test.ts > OnUncaughtException integration > Worker thread error handling > should not interfere with worker thread error handling ".mjs"

Error: Test timed out in 15000ms. If this is a long-running test, pass a timeout value as the last argument or configure it globally with "testTimeout". ❯ suites/public-api/OnUncaughtException/test.ts:106:28
const runner = createRunner(__dirname, `worker-thread/caught-worker.${extension}`)
.withFlags(
extension === 'mjs' ? '--import' : '--require',
path.join(__dirname, `worker-thread/instrument.${extension}`),
)
.expect({
event: {
level: 'error',
exception: {
values: [
{
type: 'Error',
value: 'job failed',
mechanism: {
type: 'auto.child_process.worker_thread',
handled: false,
},
stacktrace: {
frames: expect.any(Array),
},
},
],
},
},
})
.start();

await runner.completed();

const logs = runner.getLogs();

expect(logs).toEqual(expect.arrayContaining([expect.stringMatching(/^caught Error: job failed/)]));
});

test('should not interfere with worker thread error handling when required inline', async () => {

Check failure on line 141 in dev-packages/node-integration-tests/suites/public-api/OnUncaughtException/test.ts

View workflow job for this annotation

GitHub Actions / Node (18) Integration Tests

suites/public-api/OnUncaughtException/test.ts > OnUncaughtException integration > Worker thread error handling > should not interfere with worker thread error handling when required inline

Error: Test timed out in 15000ms. If this is a long-running test, pass a timeout value as the last argument or configure it globally with "testTimeout". ❯ suites/public-api/OnUncaughtException/test.ts:141:5
const runner = createRunner(__dirname, 'worker-thread/caught-worker-inline.js')
.expect({
event: {
level: 'error',
exception: {
values: [
{
type: 'Error',
value: 'job failed',
mechanism: {
type: 'auto.child_process.worker_thread',
handled: false,
},
stacktrace: {
frames: expect.any(Array),
},
},
],
},
},
})
.start();

await runner.completed();

const logs = runner.getLogs();

expect(logs).toEqual(expect.arrayContaining([expect.stringMatching(/^caught Error: job failed/)]));
});

test('should capture uncaught worker thread errors', async () => {
await createRunner(__dirname, 'worker-thread/uncaught-worker.mjs')
.withInstrument(path.join(__dirname, 'worker-thread/instrument.mjs'))
.expect({
event: {
level: 'error',
exception: {
values: [
{
type: 'Error',
value: 'job failed',
mechanism: {
type: 'auto.child_process.worker_thread',
handled: false,
},
stacktrace: {
frames: expect.any(Array),
},
},
],
},
},
})
.start()
.completed();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// reuse the same worker script as the other tests
// just now in one file
require('./instrument.js');
require('./caught-worker.js');
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const path = require('path');
const { Worker } = require('worker_threads');

function runJob() {
const worker = new Worker(path.join(__dirname, 'job.js'));
return new Promise((resolve, reject) => {
worker.once('error', reject);
worker.once('exit', code => {
if (code) reject(new Error(`Worker exited with code ${code}`));
else resolve();
});
});
}

runJob()
.then(() => {
// eslint-disable-next-line no-console
console.log('Job completed successfully');
})
.catch(err => {
// eslint-disable-next-line no-console
console.error('caught', err);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import path from 'path';
import { Worker } from 'worker_threads';

const __dirname = new URL('.', import.meta.url).pathname;

function runJob() {
const worker = new Worker(path.join(__dirname, 'job.js'));
return new Promise((resolve, reject) => {
worker.once('error', reject);
worker.once('exit', code => {
if (code) reject(new Error(`Worker exited with code ${code}`));
else resolve();
});
});
}

try {
await runJob();
// eslint-disable-next-line no-console
console.log('Job completed successfully');
} catch (err) {
// eslint-disable-next-line no-console
console.error('caught', err);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const Sentry = require('@sentry/node');
const { loggingTransport } = require('@sentry-internal/node-integration-tests');

Sentry.init({
dsn: 'https://[email protected]/1337',
tracesSampleRate: 0,
debug: false,
transport: loggingTransport,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as Sentry from '@sentry/node';
import { loggingTransport } from '@sentry-internal/node-integration-tests';

Sentry.init({
dsn: 'https://[email protected]/1337',
tracesSampleRate: 0,
debug: false,
transport: loggingTransport,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('job failed');
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import path from 'path';
import { Worker } from 'worker_threads';

const __dirname = new URL('.', import.meta.url).pathname;

function runJob() {
const worker = new Worker(path.join(__dirname, 'job.js'));
return new Promise((resolve, reject) => {
worker.once('error', reject);
worker.once('exit', code => {
if (code) reject(new Error(`Worker exited with code ${code}`));
else resolve();
});
});
}

await runJob();
5 changes: 5 additions & 0 deletions packages/node-core/src/integrations/onuncaughtexception.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { captureException, debug, defineIntegration, getClient } from '@sentry/core';
import { isMainThread } from 'worker_threads';
import { DEBUG_BUILD } from '../debug-build';
import type { NodeClient } from '../sdk/client';
import { logAndExitProcess } from '../utils/errorhandling';
Expand Down Expand Up @@ -44,6 +45,10 @@ export const onUncaughtExceptionIntegration = defineIntegration((options: Partia
return {
name: INTEGRATION_NAME,
setup(client: NodeClient) {
if (!isMainThread) {
return;
}

global.process.on('uncaughtException', makeErrorHandler(client, optionsWithDefaults));
},
};
Expand Down
Loading