diff --git a/src/core/contracts/error-handling.ts b/src/core/contracts/error-handling.ts index 7d17a56..f26ae56 100644 --- a/src/core/contracts/error-handling.ts +++ b/src/core/contracts/error-handling.ts @@ -10,9 +10,17 @@ export interface ErrorHandling { */ crash(err: Error): never; /** - * A function that is called on every throw. + * A function that is called on every throw, + * If and only if the command is not handled properly * @param error */ updateAlive(error: Error): void; + /** + * This callback is called if a module + * handles onError with type 'fail' + * + */ + handleError(error: Error): void; + } diff --git a/src/core/structures/command-error.ts b/src/core/structures/command-error.ts index 81bf92e..6131c06 100644 --- a/src/core/structures/command-error.ts +++ b/src/core/structures/command-error.ts @@ -1,41 +1,6 @@ -import type { ReplyOptions } from "../../types/utility"; import type { Logging } from "../contracts"; export interface Response { - type: 'fail' | 'continue'; - body?: ReplyOptions; + type: 'fail' | 'handled'; log?: { type: keyof Logging; message: unknown } } - -export const of = () => { - const payload = { - type: 'fail', - body: undefined, - log : undefined - } as Record - - return { - /** - * @param {'fail' | 'continue'} p a status to determine if the error will - * terminate your application or continue. Warning and - */ - status: (p: 'fail' | 'continue') => { - payload.type = p; - return payload; - }, - /** - * @param {keyof Logging} type Determine to log to logger[type]. - * @param {T} message the message to log - * - * Log this error with the logger. - */ - log: (type: keyof Logging, message: T) => { - payload.log = { type, message }; - return payload; - }, - reply: (bodyContent: ReplyOptions) => { - payload.body = bodyContent; - return payload; - } - }; -} diff --git a/src/handlers/dispatchers.ts b/src/handlers/dispatchers.ts index 4f30f04..153f8ac 100644 --- a/src/handlers/dispatchers.ts +++ b/src/handlers/dispatchers.ts @@ -59,7 +59,7 @@ export function eventDispatcher(module: Processed, onError: OnError, sou module.execute(...args), ); return fromEvent(source, module.name).pipe( - intoPayload(module, onError?.default), + intoPayload(module, onError), concatMap(createResult), execute, ); @@ -94,14 +94,14 @@ export function createDispatcher(payload: { return { args: contextArgs(payload.event), ...payload, - onError: payload.onError?.default + onError: payload.onError }; } default: return { args: interactionArg(payload.event), ...payload, - onError: payload.onError?.default + onError: payload.onError } } } diff --git a/src/handlers/event-utils.ts b/src/handlers/event-utils.ts index 7403c5c..dd7fd52 100644 --- a/src/handlers/event-utils.ts +++ b/src/handlers/event-utils.ts @@ -27,7 +27,7 @@ import { CommandError, Emitter, ErrorHandling, Logging, ModuleManager } from '.. import { contextArgs, createDispatcher } from './dispatchers'; import { ObservableInput, pipe } from 'rxjs'; import { SernEmitter } from '../core'; -import { Err, ErrImpl, Ok, Result } from 'ts-results-es'; +import { Err, Ok, Result } from 'ts-results-es'; 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'; @@ -104,7 +104,7 @@ export function createMessageHandler( .defaultModuleLoader>(fullPath) .then(payload => { const args = contextArgs(event, rest); - return Ok({ args, ...payload, onError: payload.onError?.default }); + return Ok({ args, ...payload }); }); }); } @@ -164,27 +164,21 @@ export function executeModule( return throwError(() => SernEmitter.failure(module, err)); } //Could be promise - const err = onError() as CommandError.Response + const err = onError(err_msg) as CommandError.Response if(!err) { - const failure = SernEmitter.failure(module, "Handling onError: returned undefined"); + const failure = SernEmitter.failure(module, "onError: returned undefined/null"); return throwError(() => failure); } if(err.log) { const { type, message } = err.log; logger?.[type]({ message }); }; - //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('respond' in apiObject && typeof apiObject.respond === 'function') { - return throwError(() => SernEmitter.failure(module, "Cannot handle autocomplete errors")); - } - if('reply' in apiObject && typeof apiObject.reply === 'function') { - return from(apiObject.reply(err.body)) - } - return EMPTY; + if(err.type === 'fail') { + } else { + + } + return EMPTY; } return of(module).pipe( //converting the task into a promise so rxjs can resolve the Awaitable properly @@ -195,7 +189,6 @@ export function executeModule( return EMPTY; } return onError$(result.error); - }), ); } diff --git a/src/types/core-modules.ts b/src/types/core-modules.ts index b7055d4..1202bcb 100644 --- a/src/types/core-modules.ts +++ b/src/types/core-modules.ts @@ -19,7 +19,7 @@ import { CommandType, Context, EventType } from '../../src/core'; import { AnyCommandPlugin, AnyEventPlugin, ControlPlugin, InitPlugin } from './core-plugin'; import { Awaitable, Args, SlashOptions, SernEventsMapping, AnyFunction } from './utility'; -export type OnError = Record|undefined +export type OnError = AnyFunction|undefined export interface CommandMeta {