From 56a74ab32a0a050b7a0c05965b50a1764901c279 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Wed, 4 Oct 2023 00:38:18 -0500 Subject: [PATCH] naive onError handling, not tested --- src/handlers/event-utils.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/handlers/event-utils.ts b/src/handlers/event-utils.ts index 1987aa9..5c6c94a 100644 --- a/src/handlers/event-utils.ts +++ b/src/handlers/event-utils.ts @@ -32,6 +32,7 @@ import type { AnyFunction, Awaitable } from '../types/utility'; import type { ControlPlugin } from '../types/core-plugin'; import type { AnyModule, CommandModule, Module, OnError, Processed } from '../types/core-modules'; import type { ImportPayload } from '../types/core'; +import assert from 'node:assert'; function createGenericHandler( source: Observable, @@ -167,17 +168,27 @@ export function executeModule( return EMPTY; } if(onError) { + //Could be promise const err = onError() as CommandError.Response if(!err) { - return throwError(() => - SernEmitter.failure(module, "Failed to handle onError: returned nothing")); + const failure = SernEmitter.failure(module, "Handling onError: returned undefined"); + return throwError(() => failure); } if(err.log) { const { type, message } = err.log; logger?.[type]({ message }); }; - - return EMPTY + //args[0] will be Repliable ( has reply method ), unless it is autocomplete + const apiObject = args[0]; + assert(apiObject && typeof apiObject === 'object', "Args[0] was falsy while trying to create onError"); + assert(err.body, "Body of error response cannot be empty"); + if('reply' in apiObject && typeof apiObject.reply === 'function') { + return from(apiObject.reply(err.body)) + } + if('respond' in apiObject && typeof apiObject.respond === 'function') { + return from(apiObject.respond(err.body)) + } + return EMPTY; } return throwError(() => SernEmitter.failure(module, result.error));