-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(node): Use fatal
level for unhandled rejections in strict
mode
#15720
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
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
13 changes: 13 additions & 0 deletions
13
...ges/node-integration-tests/suites/public-api/onUnhandledRejectionIntegration/mode-none.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', | ||
integrations: [Sentry.onUnhandledRejectionIntegration({ mode: 'none' })], | ||
}); | ||
|
||
setTimeout(() => { | ||
process.stdout.write("I'm alive!"); | ||
process.exit(0); | ||
}, 500); | ||
|
||
Promise.reject('test rejection'); |
14 changes: 14 additions & 0 deletions
14
...s/node-integration-tests/suites/public-api/onUnhandledRejectionIntegration/mode-strict.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,14 @@ | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
integrations: [Sentry.onUnhandledRejectionIntegration({ mode: 'strict' })], | ||
}); | ||
|
||
setTimeout(() => { | ||
// should not be called | ||
process.stdout.write("I'm alive!"); | ||
process.exit(0); | ||
}, 500); | ||
|
||
Promise.reject('test rejection'); |
12 changes: 12 additions & 0 deletions
12
...de-integration-tests/suites/public-api/onUnhandledRejectionIntegration/mode-warn-error.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,12 @@ | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
}); | ||
|
||
setTimeout(() => { | ||
process.stdout.write("I'm alive!"); | ||
process.exit(0); | ||
}, 500); | ||
|
||
Promise.reject(new Error('test rejection')); |
12 changes: 12 additions & 0 deletions
12
...e-integration-tests/suites/public-api/onUnhandledRejectionIntegration/mode-warn-string.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,12 @@ | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
}); | ||
|
||
setTimeout(() => { | ||
process.stdout.write("I'm alive!"); | ||
process.exit(0); | ||
}, 500); | ||
|
||
Promise.reject('test rejection'); |
12 changes: 12 additions & 0 deletions
12
...de-integration-tests/suites/public-api/onUnhandledRejectionIntegration/scenario-strict.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,12 @@ | ||
import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
import * as Sentry from '@sentry/node'; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
release: '1.0', | ||
transport: loggingTransport, | ||
integrations: [Sentry.onUnhandledRejectionIntegration({ mode: 'strict' })], | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
Promise.reject('test rejection'); |
11 changes: 11 additions & 0 deletions
11
...node-integration-tests/suites/public-api/onUnhandledRejectionIntegration/scenario-warn.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,11 @@ | ||
import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
import * as Sentry from '@sentry/node'; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
release: '1.0', | ||
transport: loggingTransport, | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
Promise.reject('test rejection'); |
127 changes: 127 additions & 0 deletions
127
...packages/node-integration-tests/suites/public-api/onUnhandledRejectionIntegration/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,127 @@ | ||
import * as childProcess from 'child_process'; | ||
import * as path from 'path'; | ||
import { afterAll, describe, expect, test } from 'vitest'; | ||
import { cleanupChildProcesses } from '../../../utils/runner'; | ||
import { createRunner } from '../../../utils/runner'; | ||
|
||
describe('onUnhandledRejectionIntegration', () => { | ||
afterAll(() => { | ||
cleanupChildProcesses(); | ||
}); | ||
|
||
test('should show string-type promise rejection warnings by default', () => | ||
new Promise<void>(done => { | ||
expect.assertions(3); | ||
|
||
const testScriptPath = path.resolve(__dirname, 'mode-warn-string.js'); | ||
|
||
childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (err, stdout, stderr) => { | ||
expect(err).toBeNull(); | ||
expect(stdout).toBe("I'm alive!"); | ||
expect(stderr.trim()) | ||
.toBe(`This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason: | ||
test rejection`); | ||
done(); | ||
}); | ||
})); | ||
|
||
test('should show error-type promise rejection warnings by default', () => | ||
new Promise<void>(done => { | ||
expect.assertions(3); | ||
|
||
const testScriptPath = path.resolve(__dirname, 'mode-warn-error.js'); | ||
|
||
childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (err, stdout, stderr) => { | ||
|
||
expect(err).toBeNull(); | ||
expect(stdout).toBe("I'm alive!"); | ||
expect(stderr) | ||
.toContain(`This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason: | ||
Error: test rejection | ||
at Object.<anonymous>`); | ||
done(); | ||
}); | ||
})); | ||
|
||
test('should not close process on unhandled rejection in strict mode', () => | ||
new Promise<void>(done => { | ||
expect.assertions(4); | ||
|
||
const testScriptPath = path.resolve(__dirname, 'mode-strict.js'); | ||
|
||
childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (err, stdout, stderr) => { | ||
|
||
expect(err).not.toBeNull(); | ||
expect(err?.code).toBe(1); | ||
expect(stdout).not.toBe("I'm alive!"); | ||
expect(stderr) | ||
.toContain(`This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason: | ||
test rejection`); | ||
done(); | ||
}); | ||
})); | ||
|
||
test('should not close process or warn on unhandled rejection in none mode', () => | ||
new Promise<void>(done => { | ||
expect.assertions(3); | ||
|
||
const testScriptPath = path.resolve(__dirname, 'mode-none.js'); | ||
|
||
childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (err, stdout, stderr) => { | ||
|
||
expect(err).toBeNull(); | ||
expect(stdout).toBe("I'm alive!"); | ||
expect(stderr).toBe(''); | ||
done(); | ||
}); | ||
})); | ||
|
||
test('captures exceptions for unhandled rejections', async () => { | ||
await createRunner(__dirname, 'scenario-warn.ts') | ||
.expect({ | ||
event: { | ||
level: 'error', | ||
exception: { | ||
values: [ | ||
{ | ||
type: 'Error', | ||
value: 'test rejection', | ||
mechanism: { | ||
type: 'onunhandledrejection', | ||
handled: false, | ||
}, | ||
stacktrace: { | ||
frames: expect.any(Array), | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}) | ||
.start() | ||
.completed(); | ||
}); | ||
|
||
test('captures exceptions for unhandled rejections in strict mode', async () => { | ||
await createRunner(__dirname, 'scenario-strict.ts') | ||
.expect({ | ||
event: { | ||
level: 'fatal', | ||
exception: { | ||
values: [ | ||
{ | ||
type: 'Error', | ||
value: 'test rejection', | ||
mechanism: { | ||
type: 'onunhandledrejection', | ||
handled: false, | ||
}, | ||
stacktrace: { | ||
frames: expect.any(Array), | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}) | ||
.start() | ||
.completed(); | ||
}); | ||
}); |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.