Skip to content

Commit

Permalink
fix: sern emitter err (#358)
Browse files Browse the repository at this point in the history
* prep for fix

* fix ? (not tested

* fix error event not emitting payload
  • Loading branch information
jacoobes authored Mar 18, 2024
1 parent 2106522 commit 90e55df
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 12 deletions.
11 changes: 6 additions & 5 deletions src/core/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ export const sharedEventStream = <T>(e: Emitter, eventName: string) => {
return (fromEvent(e, eventName) as Observable<T>).pipe(share());
};

export function handleError<C>(crashHandler: ErrorHandling, logging?: Logging) {
export function handleError<C>(crashHandler: ErrorHandling, emitter: Emitter, logging?: Logging) {
return (pload: unknown, caught: Observable<C>) => {
// This is done to fit the ErrorHandling contract
const err = pload instanceof Error ? pload : Error(util.inspect(pload, { colors: true }));
//formatted payload
logging?.error({ message: util.inspect(pload) });
crashHandler.updateAlive(err);
if(!emitter.emit('error', pload)) {
const err = pload instanceof Error ? pload : Error(util.inspect(pload, { colors: true }));
logging?.error({ message: util.inspect(pload) });
crashHandler.updateAlive(err);
}
return caught;
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/structures/services/error-handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class DefaultErrorHandling implements ErrorHandling {
throw err;
}

#keepAlive = 5;
#keepAlive = 1;

updateAlive(err: Error) {
this.#keepAlive--;
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/event-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@ export function makeModuleExecutor<
);
}

export const handleCrash = (err: ErrorHandling, log?: Logging) =>
export const handleCrash = (err: ErrorHandling,sernemitter: Emitter, log?: Logging) =>
pipe(
catchError(handleError(err, log)),
catchError(handleError(err, sernemitter, log)),
finalize(() => {
log?.info({
message: 'A stream closed or reached end of lifetime',
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/user-defined-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ export function eventsHandler(
* Where all events are turned on
*/
mergeAll(),
handleCrash(err, log))
handleCrash(err, emitter, log))
.subscribe();
}
2 changes: 1 addition & 1 deletion src/sern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ export function init(maybeWrapper: Wrapper | 'file') {
const messages$ = messageHandler(dependencies, wrapper.defaultPrefix);
const interactions$ = interactionHandler(dependencies);
// listening to the message stream and interaction stream
merge(messages$, interactions$).pipe(handleCrash(errorHandler, logger)).subscribe();
merge(messages$, interactions$).pipe(handleCrash(errorHandler, dependencies[0], logger)).subscribe();
}
4 changes: 2 additions & 2 deletions src/types/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ export type Args = ParseType<{ text: string[]; slash: SlashOptions }>;
export interface SernEventsMapping {
'module.register': [Payload];
'module.activate': [Payload];
error: [Payload];
error: [{ type: PayloadType.Failure; module?: AnyModule; reason: string | Error }];
warning: [Payload];
modulesLoaded: [never?];
}

export type Payload =
| { type: PayloadType.Success; module: AnyModule }
| { type: PayloadType.Failure; module?: AnyModule; reason: string | Error }
| { type: PayloadType.Warning; reason: string };
| { type: PayloadType.Warning; module: undefined; reason: string };


export type ReplyOptions = string | Omit<InteractionReplyOptions, 'fetchReply'> | MessageReplyOptions;

0 comments on commit 90e55df

Please sign in to comment.