Skip to content

Commit d332c32

Browse files
authored
fix: console log uncaught exceptions (#159)
sdk should stdout the exception before exit
1 parent 6b6ddd2 commit d332c32

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

.changeset/khaki-melons-mix.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@hyperdx/instrumentation-exception': patch
3+
'@hyperdx/node-opentelemetry': patch
4+
---
5+
6+
fix: console log uncaught exceptions

packages/instrumentation-exception/src/node/utils/errorhandling.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { consoleSandbox } from '@sentry/utils';
12
import { diag } from '@opentelemetry/api';
23

34
const DEFAULT_SHUTDOWN_TIMEOUT = 2000;
@@ -21,7 +22,10 @@ export async function logAndExitProcess(
2122
error: Error,
2223
forceFlush: () => Promise<void>,
2324
): Promise<void> {
24-
diag.error('Exiting process due to fatal error', error);
25+
consoleSandbox(() => {
26+
// eslint-disable-next-line no-console
27+
console.error(error);
28+
});
2529

2630
try {
2731
await Promise.race([
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const express = require('express');
2+
const HyperDX = require('../build/src');
3+
4+
HyperDX.init({
5+
apiKey: '',
6+
service: 'hard-crash',
7+
});
8+
9+
// Crash immediately
10+
har();
11+
12+
// unhandledRejection
13+
// new Promise((resolve, reject) => {
14+
// reject(new Error('🦄🦄🦄🦄'));
15+
// });
16+
17+
const PORT = parseInt(process.env.PORT || '7788');
18+
const app = express();
19+
20+
app.get('/uncaught', async (req, res) => {
21+
setTimeout(() => {
22+
throw new Error('💥💥💥💥');
23+
}, 2000);
24+
res.send('Uncaught exception in 2 seconds');
25+
});
26+
27+
app.get('/crash', (req, res) => {
28+
throw new Error('🧨🧨🧨🧨');
29+
});
30+
31+
app.get('/', (req, res) => {
32+
res.send('Hello World!');
33+
});
34+
35+
// HyperDX.setupExpressErrorHandler(app);
36+
37+
app.listen(PORT, () => {
38+
console.log(`Listening for requests on http://localhost:${PORT}`);
39+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const express = require('express');
2+
const Redis = require('redis');
3+
const HyperDX = require('../build/src');
4+
5+
HyperDX.init({
6+
apiKey: '',
7+
service: 'dummy_redis4',
8+
});
9+
10+
const PORT = parseInt(process.env.PORT || '7788');
11+
const app = express();
12+
13+
app.use(express.json());
14+
app.get('/', async (req, res) => {
15+
const client = await Redis.createClient({
16+
host: 'localhost',
17+
port: 6379,
18+
}).connect();
19+
// await redis.commandsExecutor('set', 'foo', 'bar');
20+
await client.set('foo1', 'bar1');
21+
await client.get('foo1');
22+
// await redis.sendCommand(['SET', 'foo1', 'bar1']);
23+
// await redis.sendCommand(['GET', 'foo1']);
24+
// const [setKeyReply, otherKeyValue] = await client
25+
// .multi()
26+
// .set('key', 'value')
27+
// .get('another-key')
28+
// .exec(); // ['OK', 'another-value']
29+
await client.disconnect();
30+
res.send('Hello World!!!');
31+
});
32+
33+
app.listen(PORT, () => {
34+
console.log(`Listening for requests on http://localhost:${PORT}`);
35+
});

0 commit comments

Comments
 (0)