Skip to content

Commit

Permalink
error-handling-draft
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed Jan 16, 2025
1 parent 52e1456 commit de6ae91
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 23 deletions.
17 changes: 0 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,3 @@ export * from './core/plugin';
export { CommandType, PluginType, PayloadType, EventType } from './core/structures/enums';
export { Context } from './core/structures/context';
export { type CoreDependencies, makeDependencies, single, transient, Service, Services } from './core/ioc';


import type { Container } from '@sern/ioc';

/**
* @deprecated This old signature will be incompatible with future versions of sern >= 4.0.0. See {@link makeDependencies}
* @example
* ```ts
* To switch your old code:
await makeDependencies(({ add }) => {
add('@sern/client', new Client())
})
* ```
*/
export interface DependencyConfiguration {
build: (root: Container) => Container;
}
23 changes: 17 additions & 6 deletions src/sern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import ready from './handlers/ready';
import { interactionHandler } from './handlers/interaction';
import { messageHandler } from './handlers/message'
import { presenceHandler } from './handlers/presence';
import { UnpackedDependencies, Wrapper } from './types/utility';
import type { Payload, UnpackedDependencies, Wrapper } from './types/utility';
import type { Presence} from './core/presences';
import { registerTasks } from './handlers/tasks';

Expand All @@ -32,7 +32,6 @@ import { registerTasks } from './handlers/tasks';
export function init(maybeWrapper: Wrapper = { commands: "./dist/commands" }) {
const startTime = performance.now();
const deps = useContainerRaw().deps<UnpackedDependencies>();

if (maybeWrapper.events !== undefined) {
eventsHandler(deps, maybeWrapper)
.then(() => {
Expand All @@ -42,6 +41,22 @@ export function init(maybeWrapper: Wrapper = { commands: "./dist/commands" }) {
deps['@sern/logger']?.info({ message: "No events registered" });
}

// autohandle errors that occur in modules.
// convenient for rapid iteration
if(maybeWrapper.autoHandleErrors) {
if(!deps['@sern/logger']) {
throw Error('A logger is required to autoHandleModuleErrors.\n A default logger is already supplied!');
}
deps['@sern/logger']?.info({ 'message': 'autoHandleModuleErrors enabled' })
deps['@sern/emitter'].addListener('error', (payload: Payload) => {
if(payload.type === 'failure') {
deps['@sern/logger']?.error({ message: payload.reason })
} else {
deps['@sern/logger']?.warning({ message: "error event should only have payloads of 'failure'" });
}
})
}

const initCallsite = callsites()[1].getFileName();
const presencePath = Files.shouldHandle(initCallsite!, "presence");
//Ready event: load all modules and when finished, time should be taken and logged
Expand All @@ -60,10 +75,6 @@ export function init(maybeWrapper: Wrapper = { commands: "./dist/commands" }) {
}
})
.catch(err => { throw err });

//const messages$ = messageHandler(deps, maybeWrapper.defaultPrefix);
interactionHandler(deps, maybeWrapper.defaultPrefix);
messageHandler(deps, maybeWrapper.defaultPrefix)
// listening to the message stream and interaction stream
//merge(messages$, interactions$).subscribe();
}
55 changes: 55 additions & 0 deletions src/types/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,64 @@ export type UnpackedDependencies = {
export type ReplyOptions = string | Omit<InteractionReplyOptions, 'fetchReply'> | MessageReplyOptions;


/**
* @interface Wrapper
* @description Configuration interface for the sern framework. This interface defines
* the structure for configuring essential framework features including command handling,
* event management, and task scheduling.
*/
export interface Wrapper {
/**
* @property {string} commands
* @description Specifies the directory path where command modules are located.
* This is a required property that tells Sern where to find and load command files.
* The path should be relative to the project root.
*
* @example
* commands: "./dist/commands"
*/
commands: string;

/**
* @property {boolean} [autoHandleErrors]
* @description Optional flag to enable automatic error handling for modules.
* When enabled, sern will automatically catch and handle errors that occur
* during module execution, preventing crashes and providing error logging.
*
* @default false
*/
autoHandleErrors?: boolean;

/**
* @property {string} [defaultPrefix]
* @description Optional prefix for text commands. This prefix will be used
* to identify text commands in messages. If not specified, text commands {@link CommandType.Text}
* will be disabled.
*
* @example
* defaultPrefix: "?"
*/
defaultPrefix?: string;

/**
* @property {string} [events]
* @description Optional directory path where event modules are located.
* If provided, Sern will automatically register and handle events from
* modules in this directory. The path should be relative to the project root.
*
* @example
* events: "./dist/events"
*/
events?: string;

/**
* @property {string} [tasks]
* @description Optional directory path where scheduled task modules are located.
* If provided, Sern will automatically register and handle scheduled tasks
* from modules in this directory. The path should be relative to the project root.
*
* @example
* tasks: "./dist/tasks"
*/
tasks?: string;
}

0 comments on commit de6ae91

Please sign in to comment.