fix(messaging): encode error events
ADD
packages/messaging/src/middlewares/messaging.eventOutput.middleware.ts
+import { Event, useContext } from '@marblejs/core';
+import { createUuid, isNonNullable } from '@marblejs/core/dist/+internal/utils';
+import { map } from 'rxjs/operators';
+import { MsgOutputEffect } from '../effects/messaging.effects.interface';
+import { TransportLayerToken } from '../server/messaging.server.tokens';
+
+export const encodeError = (error: any) =>
+ ['name', ...Object.getOwnPropertyNames(error)].reduce((acc, key) => {
+ acc[key] = error[key];
+ return acc;
+ }, Object.create(null))
+
+export const outputRouter$: MsgOutputEffect = (event$, ctx) => {
+ const transportLayer = useContext(TransportLayerToken)(ctx.ask);
+ const originChannel = transportLayer.config.channel;
+
+ return event$.pipe(
+ map(event => ({
+ ...event,
+ metadata: {
+ ...event.metadata,
+ correlationId: event.metadata?.correlationId ?? createUuid(),
+ replyTo: event.metadata?.replyTo ?? originChannel
+ },
+ }))
+ );
+};
+
+export const outputErrorEncoder$: MsgOutputEffect<Event<any>> = event$ =>
+ event$.pipe(
+ map(event => {
+ const { payload: { error: payloadError } = { error: undefined }, error } = event;
+
+ if (isNonNullable(error)) {
+ event.error = encodeError(error);
+ }
+
+ if (isNonNullable(payloadError)) {
+ event.payload.error = encodeError(payloadError);
+ }
+
+ return event;
+ }),
+ );

DELETE
packages/messaging/src/middlewares/messaging.eventRouter.middleware.ts
-import { useContext } from '@marblejs/core';
-import { createUuid } from '@marblejs/core/dist/+internal/utils';
-import { map } from 'rxjs/operators';
-import { MsgOutputEffect } from '../effects/messaging.effects.interface';
-import { TransportLayerToken } from '../server/messaging.server.tokens';
-
-export const outputRouter$: MsgOutputEffect = (event$, ctx) => {
- const transportLayer = useContext(TransportLayerToken)(ctx.ask);
- const originChannel = transportLayer.config.channel;
-
- return event$.pipe(
- map(event => ({
- ...event,
- metadata: {
- ...event.metadata,
- correlationId: event.metadata?.correlationId ?? createUuid(),
- replyTo: event.metadata?.replyTo ?? originChannel
- },
- }))
- );
-};

MODIFY
packages/messaging/src/server/messaging.server.listener.ts
packages/messaging/src/server/messaging.server.listener.ts
@@ -16,7 +16,7 @@ import { TransportMessage, TransportMessageTransformer, TransportLayerConnection
import { jsonTransformer } from '../transport/transport.transformer';
import { MsgEffect, MsgMiddlewareEffect, MsgErrorEffect, MsgOutputEffect } from '../effects/messaging.effects.interface';
import { inputLogger$, outputLogger$, errorLogger$ } from '../middlewares/messaging.eventLogger.middleware';
-import { outputRouter$ } from '../middlewares/messaging.eventRouter.middleware';
+import { outputRouter$, outputErrorEncoder$ } from '../middlewares/messaging.eventOutput.middleware';
export interface MessagingListenerConfig {
effects?: MsgEffect<any, any>[];
@@ -88,6 +88,7 @@ export const messagingListener = createListener<MessagingListenerConfig, Messagi
e$ => outputRouter$(e$, ctx),
e$ => output$(e$, ctx),
e$ => outputLogger$(e$, ctx),
+ e$ => outputErrorEncoder$(e$, ctx),
e$ => defer(() => processError(e$)),
);

fix(messaging): encode error events