refactor(websockets): pass error to stream instead of metadata attribute
MODIFY
packages/core/src/+internal/testing/marbles.helper.ts
packages/core/src/+internal/testing/marbles.helper.ts
import { TestScheduler } from 'rxjs/testing';
import { EffectMetadata, EffectLike } from '../../effects/effects.interface';
-type MarbleFlow = [string, { [marble: string]: any } | undefined];
+type MarbleFlow =
+ | [string, { [marble: string]: any } | undefined]
+ | [string, { [marble: string]: any } | undefined, any]
+ ;
type MarbleDependencies = { client?: any; meta?: Partial<EffectMetadata> };
export const Marbles = {
@@ -14,15 +17,15 @@ export const Marbles = {
marbleflow: [MarbleFlow, MarbleFlow],
dependencies: MarbleDependencies = {},
) => {
- const [initStream, initValues] = marbleflow[0];
- const [expectedStream, expectedValues] = marbleflow[1];
+ const [initStream, initValues, initError] = marbleflow[0];
+ const [expectedStream, expectedValues, expectedError] = marbleflow[1];
const scheduler = Marbles.createTestScheduler();
- const stream$ = scheduler.createColdObservable(initStream, initValues);
+ const stream$ = scheduler.createColdObservable(initStream, initValues, initError);
scheduler
.expectObservable(effect(stream$, dependencies.client, dependencies.meta))
- .toBe(expectedStream, expectedValues);
+ .toBe(expectedStream, expectedValues, expectedError);
scheduler.flush();
},

MODIFY
packages/websockets/src/effects/ws-effects.interface.ts
packages/websockets/src/effects/ws-effects.interface.ts
@@ -11,7 +11,7 @@ export interface WsErrorEffect<
T extends Error = Error,
U = Event,
V = Event
-> extends WsEffect<U, V, MarbleWebSocketClient, T> {}
+> extends WsEffect<{ event: U; error: T }, V, MarbleWebSocketClient> {}
export interface WsConnectionEffect<
T extends http.IncomingMessage = http.IncomingMessage
@@ -25,5 +25,4 @@ export interface WsEffect<
T = Event,
U = Event,
V = MarbleWebSocketClient,
- W extends Error = Error,
-> extends Effect<T, U, V, W> {}
+> extends Effect<T, U, V> {}

MODIFY
packages/websockets/src/error/specs/ws-error.effect.spec.ts
packages/websockets/src/error/specs/ws-error.effect.spec.ts
@@ -5,10 +5,11 @@ import { error$ } from '../ws-error.effect';
describe('error$', () => {
test('returns stream of error events for defined error object', () => {
// given
- const incomingEvent = { type: 'TEST_EVENT' };
- const error = new EventError(incomingEvent, 'Test error message', { errorData: 'test_error_data' });
+ const event = { type: 'TEST_EVENT' };
+ const error = new EventError(event, 'Test error message', { errorData: 'test_error_data' });
+ const incomingEvent = { event, error };
const outgoingEvent = {
- type: incomingEvent.type,
+ type: event.type,
error: {
message: error.message,
data: error.data,
@@ -19,32 +20,34 @@ describe('error$', () => {
Marbles.assertEffect(error$, [
['--a--', { a: incomingEvent }],
['--b--', { b: outgoingEvent }],
- ], { meta: { error } });
+ ]);
});
test('returns stream of error events for undefined error object', () => {
// given
- const incomingEvent = { type: 'TEST_EVENT' };
+ const event = { type: 'TEST_EVENT' };
const error = undefined;
- const outgoingEvent = { type: incomingEvent.type, error: {} };
+ const incomingEvent = { event, error };
+ const outgoingEvent = { type: event.type, error: {} };
// then
Marbles.assertEffect(error$, [
['--a--', { a: incomingEvent }],
['--b--', { b: outgoingEvent }],
- ], { meta: { error } });
+ ]);
});
test('returns stream of error events for undefined event object', () => {
// given
- const incomingEvent = undefined;
const error = undefined;
+ const event = undefined;
+ const incomingEvent = { event, error };
const outgoingEvent = { type: 'ERROR', error: {} };
// then
Marbles.assertEffect(error$, [
['--a--', { a: incomingEvent }],
['--b--', { b: outgoingEvent }],
- ], { meta: { error } });
+ ]);
});
});

MODIFY
packages/websockets/src/error/ws-error.effect.ts
packages/websockets/src/error/ws-error.effect.ts
@@ -8,9 +8,9 @@ const errorFactory = (message: string | undefined, data: any | undefined) => ({
message, data,
});
-export const error$: WsErrorEffect<EventError> = (event$, _, { error }) =>
+export const error$: WsErrorEffect<EventError> = event$ =>
event$.pipe(
- map(event => ({
+ map(({ error, event }) => ({
type: event ? event.type : DEFAULT_ERROR_CHANNEL,
error: errorFactory(
error ? error.message : undefined,

MODIFY
packages/websockets/src/error/ws-error.handler.ts
packages/websockets/src/error/ws-error.handler.ts
@@ -9,6 +9,11 @@ export const handleEffectsError = <IncomingError extends EventError>(
error$: WsErrorEffect<IncomingError, any, any> | undefined,
) => (error: IncomingError) => {
if (error$) {
- error$(of(error.event), client, { ...metadata, error }).subscribe(client.sendResponse);
+ const input$ = of({
+ event: error.event,
+ error,
+ });
+
+ error$(input$, client, metadata).subscribe(client.sendResponse);
}
};

refactor(websockets): pass error to stream instead of metadata attribute