Skip to content

Commit

Permalink
Error Messages Made Easier to Read
Browse files Browse the repository at this point in the history
  • Loading branch information
loeiks committed May 15, 2024
1 parent 92fbc8c commit 30b67bf
Show file tree
Hide file tree
Showing 46 changed files with 145 additions and 172 deletions.
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exweiv/weiv-data",
"version": "3.0.4",
"version": "3.1.0.beta-1",
"description": "Custom API Library for Wix sites to connect MongoDB. Designed to easily switch from wix-data APIs.",
"main": "./lib/index.js",
"files": [
Expand Down
26 changes: 13 additions & 13 deletions app/src/Aggregate/data_aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ export class WeivDataAggregate extends AggregateResult {
/** @internal */
constructor(collectionId: string) {
if (!collectionId) {
throw Error(`WeivData - Database and Collection name required`);
throw new Error(`WeivData - Database and Collection name required`);
}

super(collectionId);
}

ascending(propertyName: string): WeivDataAggregate {
if (!propertyName) {
throw Error(`WeivData - Property name required!`);
throw new Error(`WeivData - Property name required!`);
}
this.sorting = {
propertyName,
Expand All @@ -42,7 +42,7 @@ export class WeivDataAggregate extends AggregateResult {

avg(propertyName: string, projectedName = `${propertyName}Avg`): WeivDataAggregate {
if (!propertyName) {
throw Error(`WeivData - Property name is required!`);
throw new Error(`WeivData - Property name is required!`);
}
this.addGroup({
_id: "0",
Expand All @@ -60,7 +60,7 @@ export class WeivDataAggregate extends AggregateResult {

descending(propertyName: string): WeivDataAggregate {
if (!propertyName) {
throw Error(`WeivData - Property name is required!`);
throw new Error(`WeivData - Property name is required!`);
}
this.sorting = {
propertyName,
Expand All @@ -71,7 +71,7 @@ export class WeivDataAggregate extends AggregateResult {

filter(filter: WeivDataFilter): WeivDataAggregate {
if (!filter) {
throw Error(`WeivData - Filter is empty, please add a filter using weivData.filter method!`);
throw new Error(`WeivData - Filter is empty, please add a filter using weivData.filter method!`);
}
this.pipeline = checkPipelineArray(this.pipeline);
this.pipeline.push({
Expand All @@ -84,10 +84,10 @@ export class WeivDataAggregate extends AggregateResult {

group(...propertyName: string[]): WeivDataAggregate {
if (!propertyName) {
throw Error(`WeivData - Property or properties are required!`);
throw new Error(`WeivData - Property or properties are required!`);
}
if (this.groupCreated === true) {
throw Error(`WeivData - Group is already set!`);
throw new Error(`WeivData - Group is already set!`);
}

let propertyNames: { [key: string]: string } = {};
Expand All @@ -112,7 +112,7 @@ export class WeivDataAggregate extends AggregateResult {

having(filter: WeivDataFilter): WeivDataAggregate {
if (!filter) {
throw Error(`WeivData - Filter is empty, please add a filter using weivData.filter method!`);
throw new Error(`WeivData - Filter is empty, please add a filter using weivData.filter method!`);
}
this.havingFilter = {
$match: {
Expand All @@ -124,7 +124,7 @@ export class WeivDataAggregate extends AggregateResult {

limit(limit: number): WeivDataAggregate {
if (!limit && limit != 0) {
throw Error(`WeivData - Limit number is required please specify a limit amount`);
throw new Error(`WeivData - Limit number is required please specify a limit amount`);
}

if (limit != 0) {
Expand All @@ -136,7 +136,7 @@ export class WeivDataAggregate extends AggregateResult {

max(propertyName: string, projectedName = `${propertyName}Max`): WeivDataAggregate {
if (!propertyName) {
throw Error(`WeivData - Property name is required!`);
throw new Error(`WeivData - Property name is required!`);
}
this.addGroup({
_id: "0",
Expand All @@ -149,7 +149,7 @@ export class WeivDataAggregate extends AggregateResult {

min(propertyName: string, projectedName = `${propertyName}Min`): WeivDataAggregate {
if (!propertyName) {
throw Error(`WeivData - Property name is required!`);
throw new Error(`WeivData - Property name is required!`);
}
this.addGroup({
_id: "0",
Expand Down Expand Up @@ -255,7 +255,7 @@ export class WeivDataAggregate extends AggregateResult {

skip(skip: number): WeivDataAggregate {
if (!skip && skip != 0) {
throw Error(`WeivData - Skip number is required please specify a skip number`);
throw new Error(`WeivData - Skip number is required please specify a skip number`);
}

this.skipNumber = skip;
Expand All @@ -264,7 +264,7 @@ export class WeivDataAggregate extends AggregateResult {

sum(propertyName: string, projectedName = `${propertyName}Sum`): WeivDataAggregate {
if (!propertyName) {
throw Error(`WeivData - Property name is required!`)
throw new Error(`WeivData - Property name is required!`)
}

this.addGroup({
Expand Down
2 changes: 1 addition & 1 deletion app/src/Aggregate/data_aggregate_result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class AggregateResult {
const collection = this.db.collection(this.collectionName);
return { collection, memberId };
} catch (err) {
throw Error(`WeivData - Error when connecting to MongoDB Client via aggregate function class: ${err}`);
throw new Error(`WeivData - Error when connecting to MongoDB Client via aggregate function class: ${err}`);
}
}
}
2 changes: 1 addition & 1 deletion app/src/Collections/createCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export async function createCollection(collectionId: CollectionID, options?: Wei
const { collectionName } = splitCollectionId(collectionId);
await database.createCollection(collectionName, safeCollectionOptions);
} catch (err) {
throw Error(`WeivData - Error when creating a new collection in a database, details: ${err}`);
throw new Error(`WeivData - Error when creating a new collection in a database, details: ${err}`);
}
}
2 changes: 1 addition & 1 deletion app/src/Collections/deleteCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export async function deleteCollection(collectionId: CollectionID, options?: Wei
const { collectionName } = splitCollectionId(collectionId);
return await database.dropCollection(collectionName, safeCollectionOptions);
} catch (err) {
throw Error(`WeivData - Error when deleting a collection in a database, details: ${err}`);
throw new Error(`WeivData - Error when deleting a collection in a database, details: ${err}`);
}
}
2 changes: 1 addition & 1 deletion app/src/Collections/listCollections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export async function listCollections(databaseName: string, options?: WeivDataOp
const { database } = await connectionHandler<true>(`${databaseName}/`, suppressAuth, true);
return await database.listCollections(safeCollectionFilter, safeCollectionOptions).toArray();
} catch (err) {
throw Error(`WeivData - Error when listing all collections in a database, details: ${err}`);
throw new Error(`WeivData - Error when listing all collections in a database, details: ${err}`);
}
}
2 changes: 1 addition & 1 deletion app/src/Collections/renameCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export async function renameCollection(collectionId: CollectionID, newCollection
const { collectionName } = splitCollectionId(collectionId);
await database.renameCollection(collectionName, newCollectionName, safeCollectionOptions);
} catch (err) {
throw Error(`WeivData - Error when renaming a collection, details: ${err}`);
throw new Error(`WeivData - Error when renaming a collection, details: ${err}`);
}
}
14 changes: 7 additions & 7 deletions app/src/Connection/automatic_connection_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ async function setupClient(uri: string, role: CustomOptionsRole): Promise<MongoC
if (connection) {
return connection;
} else {
throw Error(`There is a problem with client caching and it's a important problem please report it! This will directly impact to all operations`);
throw new Error(`there is a problem with client caching and it's a important problem please report it! This will directly impact to all operations`);
}
} else {
// If there are no clients in cache create new one and return
return createNewClient(uri, role);
}
} catch (err) {
throw Error(`WeivData - Error when connecting to MongoDB Client via setupClient: ${err}`);
throw new Error(`Error when connecting to MongoDB Client via setupClient: ${err}`);
}
}

Expand Down Expand Up @@ -74,10 +74,10 @@ const createNewClient = async (uri: string, role: CustomOptionsRole): Promise<Mo
if (connection) {
return connection;
} else {
throw Error(`WeivData - Failed to connect to a MongoClient: connection: ${newMongoClient}`);
throw new Error(`Failed to connect to a MongoClient: connection: ${newMongoClient}`);
}
} catch (err) {
throw Error(`WeivData - Error when creating a new MongoDB client: ${err}`);
throw new Error(`Error when creating a new MongoDB client: ${err}`);
}
}

Expand Down Expand Up @@ -106,7 +106,7 @@ const connectClient = async (client: MongoClient, uri: string): Promise<MongoCli
const handleError = async () => {
clientCache.del(uri.slice(0, 20));
statusCache.set<boolean>(uri.slice(0, 20), false);
throw Error(`WeivData - Error when trying to connect client (connection error): ${uri}`); // Rethrow with URI for context
throw new Error(`when trying to connect client (connection error): ${uri}`); // Rethrow with URI for context
};

client.on('close', handleClose);
Expand All @@ -121,7 +121,7 @@ const connectClient = async (client: MongoClient, uri: string): Promise<MongoCli
statusCache.set<boolean>(uri.slice(0, 20), true);
return connectedClient;
} catch (err) {
throw Error(`WeivData - Unexpected error: ${err}`); // Handle unexpected errors gracefully
throw new Error(`Unexpected error: ${err}`); // Handle unexpected errors gracefully
}
};

Expand All @@ -131,7 +131,7 @@ export async function useClient(suppressAuth: boolean = false): Promise<{ pool:
const pool = await setupClient(uri, role);
return { pool, memberId };
} catch (err) {
throw Error(`WeivData - Error when connecting to cached MongoClient via useClient: ${err}`);
throw new Error(`when connecting to cached MongoClient via useClient: ${err}`);
}
}

Expand Down
14 changes: 7 additions & 7 deletions app/src/Connection/permission_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function getMongoURI(suppressAuth: boolean = false): Promise<GetMon
return getAdminURI();
}
} catch (err) {
throw Error(`Error on getting URI for MongoDB based on permission of current user: ${err}`);
throw new Error(`Error on getting URI for MongoDB based on permission of current user: ${err}`);
}
}

Expand All @@ -63,10 +63,10 @@ const getVisitorURI = async (): Promise<GetMongoURIResult> => {
cache.set<CryptoJS.lib.CipherParams>("VisitorMongoDB_URI", encryptedURI, 60 * 5);
return { uri: secret, role: "visitorClientOptions" }
} else {
throw Error(`WeivData - WeivDataURIs Secret Not Found or Not Configured Correctly`);
throw new Error(`WeivDataURIs Secret Not Found or Not Configured Correctly`);
}
} catch (err) {
throw Error(`Error when getting VisitorURI: ${err}`);
throw new Error(`Error when getting VisitorURI: ${err}`);
}
}

Expand Down Expand Up @@ -99,10 +99,10 @@ const getAdminURI = async (): Promise<GetMongoURIResult> => {
role: "adminClientOptions"
}
} else {
throw Error(`WeivData - WeivDataURIs Secret Not Found or Not Configured Correctly`);
throw new Error(`WeivDataURIs Secret Not Found or Not Configured Correctly`);
}
} catch (err) {
throw Error(`WeivData - Error when getting AdminURI: ${err}`);
throw new Error(`Error when getting AdminURI: ${err}`);
}
}

Expand Down Expand Up @@ -153,10 +153,10 @@ const getMemberURI = async (): Promise<GetMongoURIResult> => {
role: "memberClientOptions"
}
} else {
throw Error(`WeivDataURIs Secret Not Found or Not Configured Correctly`);
throw new Error(`WeivDataURIs Secret Not Found or Not Configured Correctly`);
}
} catch (err) {
throw Error(`Error when getting MemberURI: ${err}`);
throw new Error(`Error when getting MemberURI: ${err}`);
}
}

Expand Down
6 changes: 3 additions & 3 deletions app/src/Functions/Helpers/findOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function findOne(collectionId: CollectionID, propertyName: string,
let editedFilter = { propertyName, value: safeValue };
if (suppressHooks != true) {
const modifiedFilter = await runDataHook<'beforeFindOne'>(collectionId, "beforeFindOne", [{ propertyName, value: safeValue }, context]).catch((err) => {
throw Error(`WeivData - beforeFindOne Hook Failure ${err}`);
throw new Error(`beforeFindOne Hook Failure ${err}`);
});

if (modifiedFilter) {
Expand All @@ -50,7 +50,7 @@ export async function findOne(collectionId: CollectionID, propertyName: string,
if (item) {
if (suppressHooks != true) {
const modifiedResult = await runDataHook<'afterFindOne'>(collectionId, "afterFindOne", [item, context]).catch((err) => {
throw Error(`WeivData - afterFindOne Hook Failure ${err}`);
throw new Error(`afterFindOne Hook Failure ${err}`);
});

if (modifiedResult) {
Expand All @@ -67,6 +67,6 @@ export async function findOne(collectionId: CollectionID, propertyName: string,
return undefined;
}
} catch (err) {
throw Error(`WeivData - Error when finding an item from collection (findOne): ${err}`);
throw new Error(`WeivData - Error when finding an item from collection (findOne): ${err}`);
}
}
6 changes: 3 additions & 3 deletions app/src/Functions/Helpers/getAndRemove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function getAndRemove(collectionId: CollectionID, itemId: ItemID, o
let editedItemId = safeItemId;
if (suppressHooks != true) {
const modifiedItemId = await runDataHook<'beforeGetAndRemove'>(collectionId, "beforeGetAndRemove", [safeItemId, context]).catch((err) => {
throw Error(`WeivData - beforeGetAndRemove Hook Failure ${err}`);
throw new Error(`beforeGetAndRemove Hook Failure ${err}`);
});

if (modifiedItemId) {
Expand All @@ -36,7 +36,7 @@ export async function getAndRemove(collectionId: CollectionID, itemId: ItemID, o
if (item) {
if (suppressHooks != true) {
const modifiedResult = await runDataHook<'afterGetAndRemove'>(collectionId, "afterGetAndRemove", [item, context]).catch((err) => {
throw Error(`WeivData - afterGetAndRemove Hook Failure ${err}`);
throw new Error(`afterGetAndRemove Hook Failure ${err}`);
});

if (modifiedResult) {
Expand All @@ -49,6 +49,6 @@ export async function getAndRemove(collectionId: CollectionID, itemId: ItemID, o
return undefined;
}
} catch (err) {
throw Error(`WeivData - Error when removing an item from collection (getAndRemove): ${err}`);
throw new Error(`WeivData - Error when removing an item from collection (getAndRemove): ${err}`);
}
}
6 changes: 3 additions & 3 deletions app/src/Functions/Helpers/getAndReplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function getAndReplace(collectionId: CollectionID, itemId: ItemID,
let editedItem = safeValue;
if (suppressHooks != true) {
const modifiedItem = await runDataHook<'beforeGetAndReplace'>(collectionId, "beforeGetAndReplace", [safeValue, context]).catch((err) => {
throw Error(`WeivData - beforeGetAndReplace Hook Failure ${err}`);
throw new Error(`beforeGetAndReplace Hook Failure ${err}`);
});

if (modifiedItem) {
Expand All @@ -38,7 +38,7 @@ export async function getAndReplace(collectionId: CollectionID, itemId: ItemID,
if (item) {
if (suppressHooks != true) {
const modifiedResult = await runDataHook<'afterGetAndReplace'>(collectionId, "afterGetAndReplace", [item, context]).catch((err) => {
throw Error(`WeivData - afterGetAndReplace Hook Failure ${err}`);
throw new Error(`afterGetAndReplace Hook Failure ${err}`);
});

if (modifiedResult) {
Expand All @@ -51,6 +51,6 @@ export async function getAndReplace(collectionId: CollectionID, itemId: ItemID,
return undefined;
}
} catch (err) {
throw Error(`WeivData - Error when replacing an item from collection (getAndReplace): ${err}`);
throw new Error(`WeivData - Error when replacing an item from collection (getAndReplace): ${err}`);
}
}
6 changes: 3 additions & 3 deletions app/src/Functions/Helpers/getAndUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function getAndUpdate(collectionId: CollectionID, itemId: ItemID, v
let editedItem = safeValue;
if (suppressHooks != true) {
const modifiedItem = await runDataHook<'beforeGetAndUpdate'>(collectionId, "beforeGetAndUpdate", [safeValue, context]).catch((err) => {
throw Error(`WeivData - beforeGetAndUpdate Hook Failure ${err}`);
throw new Error(`beforeGetAndUpdate Hook Failure ${err}`);
});

if (modifiedItem) {
Expand All @@ -38,7 +38,7 @@ export async function getAndUpdate(collectionId: CollectionID, itemId: ItemID, v
if (item) {
if (suppressHooks != true) {
const modifiedResult = await runDataHook<'afterGetAndUpdate'>(collectionId, "afterGetAndUpdate", [item, context]).catch((err) => {
throw Error(`WeivData - afterGetAndUpdate Hook Failure ${err}`);
throw new Error(`afterGetAndUpdate Hook Failure ${err}`);
});

if (modifiedResult) {
Expand All @@ -51,6 +51,6 @@ export async function getAndUpdate(collectionId: CollectionID, itemId: ItemID, v
return undefined;
}
} catch (err) {
throw Error(`WeivData - Error when updating an item from collection (getAndUpdate): ${err}`);
throw new Error(`WeivData - Error when updating an item from collection (getAndUpdate): ${err}`);
}
}
2 changes: 1 addition & 1 deletion app/src/Functions/QueryReferenced/queryReferenced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export async function queryReferenced(collectionId: CollectionID, targetCollecti
const result = await referencedClass.getResult();
return result;
} catch (err) {
throw Error(`WeivData - Error when querying referenced items: ${err}`);
throw new Error(`WeivData - Error when querying referenced items: ${err}`);
}
}
Loading

0 comments on commit 30b67bf

Please sign in to comment.