imp(messaging): default logger for errored events
MODIFY
packages/@integration/src/messaging/server.ts
packages/@integration/src/messaging/server.ts
import { matchEvent, use } from '@marblejs/core';
import { eventValidator$, t } from '@marblejs/middleware-io';
import { createMicroservice, messagingListener, Transport, MsgEffect } from '@marblejs/messaging';
-import { map } from 'rxjs/operators';
+import { map, tap } from 'rxjs/operators';
const fib = (n: number): number =>
(n === 0 || n === 1) ? n : fib(n - 1) + fib(n - 2);
@@ -10,6 +10,7 @@ const fibonacci$: MsgEffect = event$ =>
event$.pipe(
matchEvent('FIB'),
use(eventValidator$(t.number)),
+ tap(event => { if (event.payload >= 45) throw new Error('Too high number!') }),
map(event => fib(event.payload)),
map(payload => ({ type: 'FIB_RESULT', payload })),
);
@@ -26,10 +27,7 @@ export const microservice = createMicroservice({
}),
});
-const bootstrap = async () => {
- await microservice();
-};
if (process.env.NODE_ENV !== 'test') {
- bootstrap();
+ microservice();
}

MODIFY
packages/messaging/src/middlewares/messaging.eventLogger.middleware.ts
packages/messaging/src/middlewares/messaging.eventLogger.middleware.ts
@@ -2,7 +2,7 @@ import chalk from 'chalk';
import { tap, map } from 'rxjs/operators';
import { useContext } from '@marblejs/core';
import { provideLogger, LoggerLevel } from '../server/messaging.server.logger';
-import { MsgMiddlewareEffect, MsgOutputEffect } from '../effects/messaging.effects.interface';
+import { MsgMiddlewareEffect, MsgOutputEffect, MsgErrorEffect } from '../effects/messaging.effects.interface';
import { TransportLayerToken } from '../server/messaging.server.tokens';
export const inputLogger$: MsgMiddlewareEffect = (event$, ctx) => {
@@ -50,3 +50,19 @@ export const outputLogger$: MsgOutputEffect = (event$, ctx) => {
map(({ event }) => event),
);
};
+
+export const errorLogger$: MsgErrorEffect = (event$, ctx) => {
+ const logger = provideLogger(ctx.ask);
+ const transportLayer = useContext(TransportLayerToken)(ctx.ask);
+
+ return event$.pipe(
+ tap(({ error, event }) => logger({
+ tag: 'ERROR',
+ message: chalk.red(event
+ ? `${error.name}, ${error.message} for event ${event.type}`
+ : `${error.name}, ${error.message}`),
+ level: LoggerLevel.ERROR,
+ channel: transportLayer.config.channel,
+ })),
+ );
+};

MODIFY
packages/messaging/src/server/messaging.server.listener.ts
packages/messaging/src/server/messaging.server.listener.ts
@@ -23,7 +23,7 @@ import { jsonTransformer } from '../transport/transport.transformer';
import { MsgEffect, MsgMiddlewareEffect, MsgErrorEffect, MsgOutputEffect } from '../effects/messaging.effects.interface';
import { TransportLayerToken, ServerEventsToken } from './messaging.server.tokens';
import { AllServerEvents, ServerEvent } from './messaging.server.events';
-import { inputLogger$, outputLogger$ } from '../middlewares/messaging.eventLogger.middleware';
+import { inputLogger$, outputLogger$, errorLogger$ } from '../middlewares/messaging.eventLogger.middleware';
export interface MessagingListenerConfig {
effects?: MsgEffect<any, any>[];
@@ -57,6 +57,7 @@ export const messagingListener = (config: MessagingListenerConfig = {}) => {
const combinedEffects = combineEffects(...effects);
const combinedMiddlewares = combineMiddlewares(inputLogger$, ...middlewares);
const combinedOutput = combineEffects(outputLogger$, output$);
+ const combinedError = combineEffects(errorLogger$, error$);
const ctx = createEffectContext({ ask, client: conn });
const decode = (msg: TransportMessage<Buffer>): TransportMessage<Event> => ({
@@ -81,7 +82,7 @@ export const messagingListener = (config: MessagingListenerConfig = {}) => {
msg$,
)),
map(([data, msg]) => ({ ...msg, data } as TransportMessage<any>)),
- catchError((error: EventError) => error$(of({ event: error.event, error }), ctx).pipe(
+ catchError((error: EventError) => combinedError(of({ event: error.event, error }), ctx).pipe(
map(data => ({ data } as TransportMessage<any>)),
)),
);

MODIFY
packages/messaging/src/server/messaging.server.logger.ts
packages/messaging/src/server/messaging.server.logger.ts
@@ -10,15 +10,15 @@ export type Logger = (opts: LoggerOptions) => any;
export const provideLogger = (ask: ContextProvider): Logger => pipe(
ask(LoggerToken),
- getOrElse(() => log),
+ getOrElse(() => defaultLogger),
);
-const log: Logger = ({ channel, message, tag, level = LoggerLevel.INFO }) => {
+const defaultLogger: Logger = ({ channel, message, tag, level = LoggerLevel.INFO }) => {
const now = new Date().toISOString();
const colorize = {
[LoggerLevel.ERROR]: chalk.red,
[LoggerLevel.INFO]: chalk.green,
[LoggerLevel.WARN]: chalk.yellow,
}[level];
- return console.log(`${now}\t${channel}\t${colorize(tag)}\t${message}`);
+ return console.log(now.padEnd(30) + channel.padEnd(15) + colorize(tag.padEnd(15)) + message);
};

imp(messaging): default logger for errored events