-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node): Add option to
OnUncaughtException
integration that allo…
…ws mimicking native uncaught error exit behaviour (#6137)
- Loading branch information
Showing
6 changed files
with
229 additions
and
42 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...ntegration-tests/suites/public-api/OnUncaughtException/additional-listener-test-script.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
}); | ||
|
||
process.on('uncaughtException', () => { | ||
// do nothing - this will prevent the Error below from closing this process before the timeout resolves | ||
}); | ||
|
||
setTimeout(() => { | ||
process.stdout.write("I'm alive!"); | ||
process.exit(0); | ||
}, 500); | ||
|
||
throw new Error(); |
27 changes: 27 additions & 0 deletions
27
.../public-api/OnUncaughtException/mimic-native-behaviour-additional-listener-test-script.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
integrations: integrations => { | ||
return integrations.map(integration => { | ||
if (integration.name === 'OnUncaughtException') { | ||
return new Sentry.Integrations.OnUncaughtException({ | ||
exitEvenIfOtherHandlersAreRegistered: false, | ||
}); | ||
} else { | ||
return integration; | ||
} | ||
}); | ||
}, | ||
}); | ||
|
||
process.on('uncaughtException', () => { | ||
// do nothing - this will prevent the Error below from closing this process before the timeout resolves | ||
}); | ||
|
||
setTimeout(() => { | ||
process.stdout.write("I'm alive!"); | ||
process.exit(0); | ||
}, 500); | ||
|
||
throw new Error(); |
24 changes: 24 additions & 0 deletions
24
...blic-api/OnUncaughtException/mimic-native-behaviour-no-additional-listener-test-script.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
integrations: integrations => { | ||
return integrations.map(integration => { | ||
if (integration.name === 'OnUncaughtException') { | ||
return new Sentry.Integrations.OnUncaughtException({ | ||
exitEvenIfOtherHandlersAreRegistered: false, | ||
}); | ||
} else { | ||
return integration; | ||
} | ||
}); | ||
}, | ||
}); | ||
|
||
setTimeout(() => { | ||
// This should not be called because the script throws before this resolves | ||
process.stdout.write("I'm alive!"); | ||
process.exit(0); | ||
}, 500); | ||
|
||
throw new Error(); |
13 changes: 13 additions & 0 deletions
13
...gration-tests/suites/public-api/OnUncaughtException/no-additional-listener-test-script.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
}); | ||
|
||
setTimeout(() => { | ||
// This should not be called because the script throws before this resolves | ||
process.stdout.write("I'm alive!"); | ||
process.exit(0); | ||
}, 500); | ||
|
||
throw new Error(); |
57 changes: 57 additions & 0 deletions
57
packages/node-integration-tests/suites/public-api/OnUncaughtException/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import * as childProcess from 'child_process'; | ||
import * as path from 'path'; | ||
|
||
describe('OnUncaughtException integration', () => { | ||
test('should close process on uncaught error with no additional listeners registered', done => { | ||
expect.assertions(3); | ||
|
||
const testScriptPath = path.resolve(__dirname, 'no-additional-listener-test-script.js'); | ||
|
||
childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (err, stdout) => { | ||
expect(err).not.toBeNull(); | ||
expect(err?.code).toBe(1); | ||
expect(stdout).not.toBe("I'm alive!"); | ||
done(); | ||
}); | ||
}); | ||
|
||
test('should close process on uncaught error when additional listeners are registered', done => { | ||
expect.assertions(3); | ||
|
||
const testScriptPath = path.resolve(__dirname, 'additional-listener-test-script.js'); | ||
|
||
childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (err, stdout) => { | ||
expect(err).not.toBeNull(); | ||
expect(err?.code).toBe(1); | ||
expect(stdout).not.toBe("I'm alive!"); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe('with `exitEvenIfOtherHandlersAreRegistered` set to false', () => { | ||
test('should close process on uncaught error with no additional listeners registered', done => { | ||
expect.assertions(3); | ||
|
||
const testScriptPath = path.resolve(__dirname, 'mimic-native-behaviour-no-additional-listener-test-script.js'); | ||
|
||
childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (err, stdout) => { | ||
expect(err).not.toBeNull(); | ||
expect(err?.code).toBe(1); | ||
expect(stdout).not.toBe("I'm alive!"); | ||
done(); | ||
}); | ||
}); | ||
|
||
test('should not close process on uncaught error when additional listeners are registered', done => { | ||
expect.assertions(2); | ||
|
||
const testScriptPath = path.resolve(__dirname, 'mimic-native-behaviour-additional-listener-test-script.js'); | ||
|
||
childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (err, stdout) => { | ||
expect(err).toBeNull(); | ||
expect(stdout).toBe("I'm alive!"); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters