feat: change error handling contract

This commit is contained in:
Jacob Nguyen
2023-05-19 20:15:25 -05:00
parent 698d468acb
commit d7195c45b3
8 changed files with 22 additions and 28 deletions

View File

@@ -7,12 +7,6 @@ export interface ErrorHandling {
*/
keepAlive: number;
/**
* Utility function to crash
* @param error
*/
crash(error: Error): never;
/**
* A function that is called on every crash. Updates keepAlive
* @param error

View File

@@ -11,5 +11,4 @@ export interface ModuleManager {
set(id: string, path: string): void;
getPublishableCommands(): Promise<CommandModule[]>;
getByNameCommandType<T extends CommandType>(name: string, commandType: T): Promise<CommandModuleDefs[T]>|undefined;
remove(id: string): boolean;
}

View File

@@ -98,9 +98,6 @@ export function handleError<C>(crashHandler: ErrorHandling, 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 }));
if (crashHandler.keepAlive == 0) {
crashHandler.crash(err);
}
//formatted payload
logging?.error({ message: util.inspect(pload) });
crashHandler.updateAlive(err);

View File

@@ -6,11 +6,13 @@ import { ErrorHandling } from '../../contracts';
* Version 4.0.0 will internalize this api. Please refrain from using ModuleStore!
*/
export class DefaultErrorHandling implements ErrorHandling {
keepAlive = 5;
crash(error: Error): never {
throw error;
}
updateAlive(_: Error) {
updateAlive(err: Error) {
this.keepAlive--;
if(this.keepAlive === 0) {
throw err;
}
}
}

View File

@@ -1,4 +1,4 @@
import { uniqueId } from '../../../handler/id';
import { createId } from '../../../handler/id';
import { CoreModuleStore, ModuleManager } from '../../contracts';
import { importModule } from '../../module-loading';
import { CommandMeta, CommandModule, CommandModuleDefs, Module } from '../../types/modules';
@@ -12,7 +12,7 @@ export class DefaultModuleManager implements ModuleManager {
constructor(private moduleStore: CoreModuleStore) {}
getByNameCommandType<T extends CommandType>(name: string, commandType: T) {
const id = this.get(`${name}_${uniqueId(commandType)}`);
const id = this.get(createId(name, commandType));
if(!id) {
return undefined;
}
@@ -31,9 +31,6 @@ export class DefaultModuleManager implements ModuleManager {
return maybeModule;
}
remove(id: string): boolean {
throw new Error('Method not implemented.');
}
get(id: string) {
return this.moduleStore.commands.get(id);

View File

@@ -27,7 +27,7 @@ import { fmt } from './messages';
import { ControlPlugin, VoidResult } from '../../core/types/plugins';
import { ImportPayload, Processed } from '../types';
import { Awaitable } from '../../shared';
import { createId, uniqueId } from '../id';
import { createId, reconstructId } from '../id';
function createGenericHandler<Source, Narrowed extends Source, Output>(
source: Observable<Source>,
@@ -49,7 +49,7 @@ export function createInteractionHandler<T extends Interaction>(
return createGenericHandler<Interaction, T, ReturnType<typeof createDispatcher>>(
source,
event => {
const fullPath = mg.get(createId(event as unknown as Interaction));
const fullPath = mg.get(reconstructId(event as unknown as Interaction));
if (!fullPath)
return Err(SernError.UndefinedModule + ' No full path found in module store');
return defaultModuleLoader<Processed<CommandModule>>(fullPath).then(res =>
@@ -89,7 +89,7 @@ function assignDefaults<T extends Module>(
moduleManager.setMetadata(module, {
isClass: module.constructor.name === 'Function',
fullPath: absPath,
id: `${module.name}_${uniqueId(module.type)}`,
id: createId(module.name, module.type),
});
});
}

View File

@@ -2,11 +2,11 @@ import { Interaction, InteractionType } from 'discord.js';
import { CommandType, EventType } from '../core';
/**
* Creates a unique ID for a given interaction object.
* Construct unique ID for a given interaction object.
* @param event The interaction object for which to create an ID.
* @returns A unique string ID based on the type and properties of the interaction object.
*/
export function createId<T extends Interaction>(event: T) {
export function reconstructId<T extends Interaction>(event: T) {
switch (event.type) {
case InteractionType.MessageComponent: {
return `${event.customId}_C${event.componentType}`;
@@ -38,7 +38,12 @@ function apiType(t: CommandType | EventType) {
* A is for any ApplicationCommand. C is for any ComponentCommand
* Then, another number generated by apiType function is appended
*/
export function uniqueId(t: CommandType | EventType) {
export function uniqueSuffix(t: CommandType | EventType) {
const am = (appBitField & t) !== 0 ? 'A' : 'C';
return am + apiType(t);
}
export function createId(name: string, type: CommandType | EventType) {
return name+"_"+uniqueSuffix(type)
}

View File

@@ -3,7 +3,7 @@ import { CoreContainer } from '../../src/core/structures/container'
import { DefaultLogging } from "../../src/core";
import { faker } from '@faker-js/faker'
import { commandModule } from "../../src";
import { uniqueId } from '../../src/handler/id'
import { createId } from '../../src/handler/id'
import { CommandMeta } from "../../src/core/types/modules";
describe('services', () => {
@@ -36,14 +36,14 @@ describe('services', () => {
.map((path,i) => `${path}/${modules[i]}.js`);
const metadata: CommandMeta[] = modules.map((cm, i) => ({
id: cm.name+'_'+uniqueId(cm.type),
id: createId(cm.name, cm.type),
isClass: false,
fullPath: `${paths[i]}/${cm.name}.js`
}));
const moduleManager = container.get('@sern/modules');
let i =0;
for(const m of modules) {
moduleManager.set(m.name+'_'+uniqueId(m.type), paths[i]);
moduleManager.set(createId(m.name,m.type), paths[i]);
moduleManager.setMetadata(m, metadata[i]);
i++
}