Skip to content

Commit

Permalink
feat: Disable client once flushed (#1904)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek authored and HazAT committed Mar 19, 2019
1 parent 292bc80 commit 42b6cd6
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ since we removed some methods from the public API and removed some classes from
- **breaking** [core] ref: Use `SyncPromise` internally, this reduces memory pressure by a lot.
- **breaking** [browser] ref: Removed `BrowserBackend` from default export.
- **breaking** [node] ref: Removed `BrowserBackend` from default export.
- **breaking** [core] feat: Disable client once flushed using `close` method
- ref: Move internal `ExtendedError` to a types package
- **breaking** [core] ref: Pass `Event` to `sendEvent` instead of already stringified data
- [utils] feat: Introduce `isSyntheticEvent` util
Expand Down
3 changes: 2 additions & 1 deletion packages/browser/src/integrations/globalhandlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getCurrentHub } from '@sentry/core';
import { Event, Integration } from '@sentry/types';
import { logger } from '@sentry/utils/logger';
import { normalize } from '@sentry/utils/object';
import { truncate } from '@sentry/utils/string';
import { addExceptionTypeValue, eventFromStacktrace } from '../parsers';
import {
Expand Down Expand Up @@ -113,7 +114,7 @@ export class GlobalHandlers implements Integration {
},
};

const fallbackValue = stacktrace.original ? truncate(JSON.stringify(stacktrace.original), 300) : '';
const fallbackValue = stacktrace.original ? truncate(JSON.stringify(normalize(stacktrace.original)), 300) : '';
const fallbackType = stacktrace.mechanism === 'onunhandledrejection' ? 'UnhandledRejection' : 'Error';

// This makes sure we have type/value in every exception
Expand Down
11 changes: 6 additions & 5 deletions packages/browser/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
captureMessage,
configureScope,
Event,
flush,
getCurrentHub,
init,
Integrations,
Expand Down Expand Up @@ -74,7 +75,7 @@ describe('SentryBrowser', () => {
addBreadcrumb({ message: 'test2' });

captureMessage('event');
await (getCurrentHub().getClient() as BrowserClient).close(2000);
await flush(2000);
expect(beforeSend.args[0][0].breadcrumbs).to.have.lengthOf(2);
});
});
Expand All @@ -87,7 +88,7 @@ describe('SentryBrowser', () => {
captureException(e);
}

await (getCurrentHub().getClient() as BrowserClient).close(2000);
await flush(2000);

const event = beforeSend.args[0][0];
expect(event.exception).to.not.be.undefined;
Expand Down Expand Up @@ -131,7 +132,7 @@ describe('SentryBrowser', () => {
captureMessage('event222');
captureMessage('event222');

await (getCurrentHub().getClient() as BrowserClient).close(2000);
await flush(2000);

expect(beforeSend.calledOnce).to.be.true;
});
Expand All @@ -149,7 +150,7 @@ describe('SentryBrowser', () => {
captureMessage('event222');
captureMessage('event222');

await (getCurrentHub().getClient() as BrowserClient).close(2000);
await flush(2000);

expect(localBeforeSend.calledTwice).to.be.true;
});
Expand All @@ -166,7 +167,7 @@ describe('SentryBrowser', () => {

captureMessage('capture');

await (getCurrentHub().getClient() as BrowserClient).close(2000);
await flush(2000);

expect(localBeforeSend.called).to.be.false;
});
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,9 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
* @inheritDoc
*/
public async close(timeout?: number): Promise<boolean> {
return this.flush(timeout);
return this.flush(timeout).finally(() => {
this.getOptions().enabled = false;
});
}

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/node/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('SentryNode', () => {
getCurrentHub().popScope();
});

test('close() with to short timeout', done => {
test('flush() with to short timeout', done => {
expect.assertions(1);
jest.useFakeTimers();
const client = new NodeClient({
Expand All @@ -44,7 +44,7 @@ describe('SentryNode', () => {
captureMessage('test');
captureMessage('test');
client
.close(50)
.flush(50)
.then(result => {
expect(result).toBeFalsy();
done();
Expand All @@ -55,7 +55,7 @@ describe('SentryNode', () => {
jest.runAllTimers();
});

test('close() with timeout', done => {
test('flush() with timeout', done => {
expect.assertions(1);
jest.useFakeTimers();
const client = new NodeClient({
Expand All @@ -68,7 +68,7 @@ describe('SentryNode', () => {
captureMessage('test');
jest.runAllTimers();
client
.close(50)
.flush(50)
.then(result => {
expect(result).toBeFalsy();
done();
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/test/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ describe('normalize()', () => {
/*no-empty*/
},
};
const result = safeNormalize(obj);
const result = normalize(obj);
expect(result).toEqual({
foo: '[SyntheticEvent]',
baz: '[NaN]',
Expand Down

0 comments on commit 42b6cd6

Please sign in to comment.