From abfeea65f7ab60fb6bff3e67235983a7afe75e5b Mon Sep 17 00:00:00 2001 From: Josh Date: Tue, 16 Jan 2024 09:08:50 +0000 Subject: [PATCH] fix: add instancenotfound error kind and handle it in getsingleton method --- src/error.ts | 1 + src/index.ts | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/error.ts b/src/error.ts index a02d81c6..33541a8e 100644 --- a/src/error.ts +++ b/src/error.ts @@ -4,6 +4,7 @@ export enum ErrorKind { MissingDriver = "MISSING_DRIVER", ParseException = "PARSE_EXCEPTION", InvalidType = "INVALID_TYPE", + InstanceNotFound = "INSTANCE_NOT_FOUND", } export class CustomError extends Error { diff --git a/src/index.ts b/src/index.ts index cf594b84..4ea96531 100644 --- a/src/index.ts +++ b/src/index.ts @@ -175,19 +175,27 @@ export class QuickDB { * ```ts * // If you have set a singleton instance * // Otherwise it will return undefined - * const db = QuickDB.getSingletion("quickdb"); + * const db = QuickDB.getSingleton("quickdb"); * await db.init(); * ``` **/ - static getSingletion(name: string): QuickDB | undefined { + static getSingleton(name: string): QuickDB { if (typeof name != "string") { throw new QuickError( `First argument (name) needs to be a string received "${typeof name}"`, ErrorKind.InvalidType ); } - - return this.instances.get(name); + + const instance = this.instances.get(name); + if (!instance) { + throw new QuickError( + `No singleton instance registered with the name "${name}"`, + ErrorKind.InstanceNotFound + ); + } + + return instance; } /**