simplify, remove reply option

This commit is contained in:
jacob
2023-11-02 15:16:12 -05:00
parent 17eb701b5f
commit 922567ff7b
5 changed files with 23 additions and 57 deletions

View File

@@ -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;
}

View File

@@ -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<PropertyKey, unknown>
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: <T=string>(type: keyof Logging, message: T) => {
payload.log = { type, message };
return payload;
},
reply: (bodyContent: ReplyOptions) => {
payload.body = bodyContent;
return payload;
}
};
}

View File

@@ -59,7 +59,7 @@ export function eventDispatcher(module: Processed<Module>, 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
}
}
}

View File

@@ -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<Processed<CommandModule>>(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);
}),
);
}

View File

@@ -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<string, AnyFunction>|undefined
export type OnError = AnyFunction|undefined
export interface CommandMeta {