Skip to content

Commit

Permalink
Merge d03c580 into e927bb3
Browse files Browse the repository at this point in the history
  • Loading branch information
mnasyrov committed Aug 28, 2022
2 parents e927bb3 + d03c580 commit 97271f6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
48 changes: 47 additions & 1 deletion packages/rx-effects/src/effect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from 'rxjs';
import { switchMap, take, toArray } from 'rxjs/operators';
import { createAction } from './action';
import { createEffect } from './effect';
import { createEffect, GLOBAL_EFFECT_UNHANDLED_ERROR$ } from './effect';

describe('Effect', () => {
describe('result$', () => {
Expand Down Expand Up @@ -53,6 +53,20 @@ describe('Effect', () => {
{ event: 2, error: new Error('test error') },
]);
});

it('should emit GLOBAL_EFFECT_UNHANDLED_ERROR$ in case error$ is not observed', async () => {
const effect = createEffect<number>(() => {
throw new Error('test error');
});

const promise = firstValueFrom(GLOBAL_EFFECT_UNHANDLED_ERROR$);
effect.handle(of(1));

expect(await promise).toEqual({
event: 1,
error: new Error('test error'),
});
});
});

describe('final$', () => {
Expand Down Expand Up @@ -344,6 +358,38 @@ describe('Effect', () => {
});
});

describe('GLOBAL_EFFECT_UNHANDLED_ERROR$', () => {
it('should emit event and error in case it is observed', async () => {
const effect = createEffect<number>(() => {
throw 'test error';
});

const promise = firstValueFrom(GLOBAL_EFFECT_UNHANDLED_ERROR$);
effect.handle(of(1));

expect(await promise).toEqual({ event: 1, error: 'test error' });
});

it('should print an error to the console in case it is not observed', async () => {
const effect = createEffect<number>(() => {
throw 'test error';
});

const consoleError = jest
.spyOn(console, 'error')
.mockImplementation(() => undefined);

effect.handle(of(1));

expect(consoleError).toBeCalledWith('Uncaught error in Effect', {
event: 1,
error: 'test error',
});

consoleError.mockReset();
});
});

async function getFirstValues<T>(
source: Observable<T>,
count: number,
Expand Down
23 changes: 22 additions & 1 deletion packages/rx-effects/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ import { Action } from './action';
import { declareState } from './stateDeclaration';
import { Query } from './queries';

const GLOBAL_EFFECT_UNHANDLED_ERROR_SUBJECT = new Subject<{
event: unknown;
error: unknown;
}>();

export const GLOBAL_EFFECT_UNHANDLED_ERROR$ =
GLOBAL_EFFECT_UNHANDLED_ERROR_SUBJECT.asObservable();

function emitGlobalUnhandledError(event: unknown, error: unknown): void {
if (GLOBAL_EFFECT_UNHANDLED_ERROR_SUBJECT.observed) {
GLOBAL_EFFECT_UNHANDLED_ERROR_SUBJECT.next({ event, error });
} else {
console.error('Uncaught error in Effect', { event, error });
}
}

/**
* Handler for an event. It can be asynchronous.
*
Expand Down Expand Up @@ -152,7 +168,12 @@ export function createEffect<Event = void, Result = void, ErrorType = Error>(
},
error: (error) => {
pendingCount.update(decreaseCount);
error$.next({ event, error });

if (error$.observed) {
error$.next({ event, error });
} else {
emitGlobalUnhandledError(event, error);
}
},
}),
);
Expand Down

0 comments on commit 97271f6

Please sign in to comment.