-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(node): Add option to OnUncaughtException
integration that allows mimicking native uncaught error exit behaviour
#6137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lforst
merged 4 commits into
master
from
lforst-mimic-native-behaviour-onuncaught-exception
Nov 4, 2022
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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
.../suites/public-api/OnUncaughtException/mimic-behaviour-additional-listener-test-script.js
This file contains hidden or 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({ | ||
mimicNativeBehaviour: true, | ||
}); | ||
} 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
...ites/public-api/OnUncaughtException/mimic-behaviour-no-additional-listener-test-script.js
This file contains hidden or 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({ | ||
mimicNativeBehaviour: true, | ||
}); | ||
} 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 hidden or 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 hidden or 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) => { | ||
Check warningCode scanning / CodeQL Shell command built from environment values
This shell command depends on an uncontrolled [absolute path](1).
|
||
expect(err).not.toBeNull(); | ||
expect(err?.code).toBe(1); | ||
expect(stdout).not.toBe("I'm alive!"); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe('with `mimicNativeBehaviour` set to true', () => { | ||
test('should close process on uncaught error with no additional listeners registered', done => { | ||
expect.assertions(3); | ||
|
||
const testScriptPath = path.resolve(__dirname, 'mimic-behaviour-no-additional-listener-test-script.js'); | ||
|
||
childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (err, stdout) => { | ||
Check warningCode scanning / CodeQL Shell command built from environment values
This shell command depends on an uncontrolled [absolute path](1).
|
||
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-behaviour-additional-listener-test-script.js'); | ||
|
||
childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (err, stdout) => { | ||
Check warningCode scanning / CodeQL Shell command built from environment values
This shell command depends on an uncontrolled [absolute path](1).
|
||
expect(err).toBeNull(); | ||
expect(stdout).toBe("I'm alive!"); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Shell command built from environment values