Skip to content

Commit

Permalink
Refactor handleError function to use handleSDKError utility
Browse files Browse the repository at this point in the history
  • Loading branch information
Adammatthiesen committed Dec 18, 2024
1 parent e59e56e commit 13c1c50
Show file tree
Hide file tree
Showing 15 changed files with 174 additions and 445 deletions.
35 changes: 4 additions & 31 deletions packages/studiocms_core/src/sdk-utils/auth/oAuth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { and, db, eq } from 'astro:db';
import { tsOAuthAccounts } from '../tables';
import type { STUDIOCMS_SDK_AUTH } from '../types';
import { StudioCMS_SDK_Error } from '../utils';
import { handleSDKError } from '../utils';

/**
* The `StudioCMS_SDK_authOAuth` object provides methods to handle OAuth authentication
Expand All @@ -25,16 +25,7 @@ export const authOAuth: STUDIOCMS_SDK_AUTH['oAuth'] = {
try {
return await db.insert(tsOAuthAccounts).values(data).returning().get();
} catch (error) {
if (error instanceof Error) {
throw new StudioCMS_SDK_Error(
`Error creating OAuth account: ${error.message}`,
error.stack
);
}
throw new StudioCMS_SDK_Error(
'Error creating OAuth account: An unknown error occurred.',
`${error}`
);
handleSDKError(error, 'Error creating OAuth account: An unknown error occurred.');
}
},
delete: async (userId, provider) => {
Expand All @@ -49,16 +40,7 @@ export const authOAuth: STUDIOCMS_SDK_AUTH['oAuth'] = {
};
});
} catch (error) {
if (error instanceof Error) {
throw new StudioCMS_SDK_Error(
`Error deleting OAuth account: ${error.message}`,
error.stack
);
}
throw new StudioCMS_SDK_Error(
'Error deleting OAuth account: An unknown error occurred.',
`${error}`
);
handleSDKError(error, 'Error deleting OAuth account: An unknown error occurred.');
}
},
searchProvidersForId: async (providerId, userId) => {
Expand All @@ -71,16 +53,7 @@ export const authOAuth: STUDIOCMS_SDK_AUTH['oAuth'] = {
)
.get();
} catch (error) {
if (error instanceof Error) {
throw new StudioCMS_SDK_Error(
`Error searching for OAuth account: ${error.message}`,
error.stack
);
}
throw new StudioCMS_SDK_Error(
'Error searching for OAuth account: An unknown error occurred.',
`${error}`
);
handleSDKError(error, 'Error searching for OAuth account: An unknown error occurred.');
}
},
};
Expand Down
13 changes: 2 additions & 11 deletions packages/studiocms_core/src/sdk-utils/auth/permission.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { db, eq } from 'astro:db';
import { tsPermissions } from '../tables';
import type { STUDIOCMS_SDK_AUTH } from '../types';
import { StudioCMS_SDK_Error } from '../utils';
import { handleSDKError } from '../utils';

/**
* An object representing the authentication permissions for the StudioCMS SDK.
Expand All @@ -16,16 +16,7 @@ export const authPermission: STUDIOCMS_SDK_AUTH['permission'] = {
try {
return await db.select().from(tsPermissions).where(eq(tsPermissions.user, userId)).get();
} catch (error) {
if (error instanceof Error) {
throw new StudioCMS_SDK_Error(
`Error getting user permissions: ${error.message}`,
error.stack
);
}
throw new StudioCMS_SDK_Error(
'Error getting user permissions: An unknown error occurred.',
`${error}`
);
handleSDKError(error, 'Error getting user permissions: An unknown error occurred.');
}
},
};
Expand Down
37 changes: 5 additions & 32 deletions packages/studiocms_core/src/sdk-utils/auth/session.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { db, eq } from 'astro:db';
import { tsSessionTable, tsUsers } from '../tables';
import type { STUDIOCMS_SDK_AUTH } from '../types';
import { StudioCMS_SDK_Error } from '../utils';
import { handleSDKError } from '../utils';

/**
* StudioCMS_SDK_authSession provides methods to manage authentication sessions.
Expand Down Expand Up @@ -40,13 +40,7 @@ export const authSession: STUDIOCMS_SDK_AUTH['session'] = {
})
.get();
} catch (error) {
if (error instanceof Error) {
throw new StudioCMS_SDK_Error(`Error creating session: ${error.message}`, error.stack);
}
throw new StudioCMS_SDK_Error(
'Error creating session: An unknown error occurred.',
`${error}`
);
handleSDKError(error, 'Error creating session: An unknown error occurred.');
}
},
sessionWithUser: async (sessionId) => {
Expand All @@ -57,16 +51,7 @@ export const authSession: STUDIOCMS_SDK_AUTH['session'] = {
.innerJoin(tsUsers, eq(tsSessionTable.userId, tsUsers.id))
.where(eq(tsSessionTable.id, sessionId));
} catch (error) {
if (error instanceof Error) {
throw new StudioCMS_SDK_Error(
`Error getting session with user: ${error.message}`,
error.stack
);
}
throw new StudioCMS_SDK_Error(
'Error getting session with user: An unknown error occurred.',
`${error}`
);
handleSDKError(error, 'Error getting session with user: An unknown error occurred.');
}
},
delete: async (sessionId) => {
Expand All @@ -77,13 +62,7 @@ export const authSession: STUDIOCMS_SDK_AUTH['session'] = {
message: 'Session deleted',
};
} catch (error) {
if (error instanceof Error) {
throw new StudioCMS_SDK_Error(`Error deleting session: ${error.message}`, error.stack);
}
throw new StudioCMS_SDK_Error(
'Error deleting session: An unknown error occurred.',
`${error}`
);
handleSDKError(error, 'Error deleting session: An unknown error occurred.');
}
},
update: async (sessionId, newDate) => {
Expand All @@ -94,13 +73,7 @@ export const authSession: STUDIOCMS_SDK_AUTH['session'] = {
.where(eq(tsSessionTable.id, sessionId))
.returning();
} catch (error) {
if (error instanceof Error) {
throw new StudioCMS_SDK_Error(`Error updating session: ${error.message}`, error.stack);
}
throw new StudioCMS_SDK_Error(
'Error updating session: An unknown error occurred.',
`${error}`
);
handleSDKError(error, 'Error updating session: An unknown error occurred.');
}
},
};
Expand Down
60 changes: 30 additions & 30 deletions packages/studiocms_core/src/sdk-utils/auth/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { db, eq } from 'astro:db';
import { GhostUserDefaults } from '../../consts';
import { tsPermissions, tsUsers } from '../tables';
import type { STUDIOCMS_SDK_AUTH } from '../types';
import { StudioCMS_SDK_Error } from '../utils';
import { handleSDKError } from '../utils';

/**
* The `StudioCMS_SDK_authUser` object provides methods for creating and updating user records
Expand All @@ -29,20 +29,14 @@ export const authUser: STUDIOCMS_SDK_AUTH['user'] = {
await db.insert(tsPermissions).values({ user: newUser.id, rank: 'visitor' });
return newUser;
} catch (error) {
if (error instanceof Error) {
throw new StudioCMS_SDK_Error(`Error creating user: ${error.message}`, error.stack);
}
throw new StudioCMS_SDK_Error('Error creating user: An unknown error occurred.', `${error}`);
handleSDKError(error, 'Error creating user: An unknown error occurred.');
}
},
update: async (userId, userData) => {
try {
return await db.update(tsUsers).set(userData).where(eq(tsUsers.id, userId)).returning().get();
} catch (error) {
if (error instanceof Error) {
throw new StudioCMS_SDK_Error(`Error updating user: ${error.message}`, error.stack);
}
throw new StudioCMS_SDK_Error('Error updating user: An unknown error occurred.', `${error}`);
handleSDKError(error, 'Error updating user: An unknown error occurred.');
}
},
searchUsersForUsernameOrEmail: async (username, email) => {
Expand All @@ -54,33 +48,39 @@ export const authUser: STUDIOCMS_SDK_AUTH['user'] = {

return { usernameSearch, emailSearch };
} catch (error) {
if (error instanceof Error) {
throw new StudioCMS_SDK_Error(
`Error searching for username or email: ${error.message}`,
error.stack
);
}
throw new StudioCMS_SDK_Error(
'Error searching for username or email: An unknown error occurred.',
`${error}`
);
handleSDKError(error, 'Error searching for username or email: An unknown error occurred.');
}
},
ghost: {
verifyExists: async () => {
const ghostUser = await db
.select()
.from(tsUsers)
.where(eq(tsUsers.id, GhostUserDefaults.id))
.get();
if (!ghostUser) {
return false;
try {
const ghostUser = await db
.select()
.from(tsUsers)
.where(eq(tsUsers.id, GhostUserDefaults.id))
.get();
if (!ghostUser) {
return false;
}
return true;
} catch (error) {
handleSDKError(error, 'Error verifying ghost user exists: An unknown error occurred.');
}
},
create: async () => {
try {
return await db.insert(tsUsers).values(GhostUserDefaults).returning().get();
} catch (error) {
handleSDKError(error, 'Error creating ghost user: An unknown error occurred.');
}
},
get: async () => {
try {
return await db.select().from(tsUsers).where(eq(tsUsers.id, GhostUserDefaults.id)).get();
} catch (error) {
handleSDKError(error, 'Error getting ghost user: An unknown error occurred.');
}
return true;
},
create: async () => await db.insert(tsUsers).values(GhostUserDefaults).returning().get(),
get: async () =>
await db.select().from(tsUsers).where(eq(tsUsers.id, GhostUserDefaults.id)).get(),
},
// TODO: Implement delete function that wont error since
// there could be references to the user in other tables
Expand Down
24 changes: 12 additions & 12 deletions packages/studiocms_core/src/sdk-utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
Expire,
StudioCMS_SDK_Error,
cacheMapSet,
handleError,
handleSDKError,
transformNewDataReturn,
transformSiteConfigReturn,
} from './utils';
Expand Down Expand Up @@ -106,7 +106,7 @@ export const studioCMS_SDK_Cache: STUDIOCMS_SDK_CACHE = {
// Return the cached data
return isCached;
} catch (error) {
handleError(error, 'Could not retrieve data from the database.');
handleSDKError(error, 'Could not retrieve data from the database.');
}
},
bySlug: async (slug, pkg) => {
Expand Down Expand Up @@ -173,7 +173,7 @@ export const studioCMS_SDK_Cache: STUDIOCMS_SDK_CACHE = {
// Return the cached data
return isCached;
} catch (error) {
handleError(error, 'Could not retrieve data from the database.');
handleSDKError(error, 'Could not retrieve data from the database.');
}
},
},
Expand Down Expand Up @@ -241,7 +241,7 @@ export const studioCMS_SDK_Cache: STUDIOCMS_SDK_CACHE = {
// Transform and return the data
return recentCacheArray;
} catch (error) {
handleError(error, 'Could not retrieve data from the database.');
handleSDKError(error, 'Could not retrieve data from the database.');
}
},
siteConfig: async () => {
Expand Down Expand Up @@ -298,7 +298,7 @@ export const studioCMS_SDK_Cache: STUDIOCMS_SDK_CACHE = {
// Return the cached data
return cache.siteConfig;
} catch (error) {
handleError(error, 'Could not retrieve data from the database.');
handleSDKError(error, 'Could not retrieve data from the database.');
}
},
},
Expand All @@ -314,7 +314,7 @@ export const studioCMS_SDK_Cache: STUDIOCMS_SDK_CACHE = {
// if caching is enabled, delete the cache entry
cache.pages.delete(id);
} catch (error) {
handleError(error, 'Error clearing cache: An unknown error occurred.');
handleSDKError(error, 'Error clearing cache: An unknown error occurred.');
}
},
bySlug: (slug, pkg) => {
Expand Down Expand Up @@ -343,7 +343,7 @@ export const studioCMS_SDK_Cache: STUDIOCMS_SDK_CACHE = {
cache.pages.delete(key);
}
} catch (error) {
handleError(error, 'Error clearing cache: An unknown error occurred.');
handleSDKError(error, 'Error clearing cache: An unknown error occurred.');
}
},
},
Expand All @@ -357,7 +357,7 @@ export const studioCMS_SDK_Cache: STUDIOCMS_SDK_CACHE = {
// if caching is enabled, clear the cache
cache.pages.clear();
} catch (error) {
handleError(error, 'Error clearing cache: An unknown error occurred.');
handleSDKError(error, 'Error clearing cache: An unknown error occurred.');
}
},
},
Expand Down Expand Up @@ -409,7 +409,7 @@ export const studioCMS_SDK_Cache: STUDIOCMS_SDK_CACHE = {
// Return the transformed data
return transformNewDataReturn(updatedData);
} catch (error) {
handleError(error, 'Could not update page data in the database.');
handleSDKError(error, 'Could not update page data in the database.');
}
},
bySlug: async (slug, pkg, { pageData, pageContent }) => {
Expand Down Expand Up @@ -453,7 +453,7 @@ export const studioCMS_SDK_Cache: STUDIOCMS_SDK_CACHE = {
await studioCMS_SDK_UPDATE.page(pageData);
await studioCMS_SDK_UPDATE.pageContent(pageContent);
} catch (error) {
handleError(error, 'Could not update page data in the database.');
handleSDKError(error, 'Could not update page data in the database.');
}

// Retrieve the updated data from the database
Expand All @@ -470,7 +470,7 @@ export const studioCMS_SDK_Cache: STUDIOCMS_SDK_CACHE = {
// Return the transformed data
return transformNewDataReturn(updatedData);
} catch (error) {
handleError(error, 'Could not update page data in the database.');
handleSDKError(error, 'Could not update page data in the database.');
}
},
},
Expand All @@ -496,7 +496,7 @@ export const studioCMS_SDK_Cache: STUDIOCMS_SDK_CACHE = {
// Return the updated data
return cache.siteConfig;
} catch (error) {
handleError(error, 'Could not update site config in the database.');
handleSDKError(error, 'Could not update site config in the database.');
}
},
},
Expand Down
Loading

0 comments on commit 13c1c50

Please sign in to comment.