Implement error handling and adjust import paths

This commit introduces error handling in the 'database-webhook-handler.service.ts' to log and rethrow errors when processing webhooks. It also adjusts the import paths for ContentlayerClient and WordpressClient in 'create-cms-client.ts'. Method names in 'database-webhook-router.service.ts' have been updated for clarity.
This commit is contained in:
giancarlo
2024-04-02 16:03:11 +08:00
parent eb43059fda
commit 2dc2c6461e
3 changed files with 37 additions and 19 deletions

View File

@@ -19,18 +19,18 @@ export async function createCmsClient(
return getWordpressClient(); return getWordpressClient();
default: default:
throw new Error(`Unknown CMS type: ${type}`); throw new Error(`Unknown CMS type`);
} }
} }
async function getContentLayerClient() { async function getContentLayerClient() {
const { ContentlayerClient } = await import('../../contentlayer'); const { ContentlayerClient } = await import('../../contentlayer/src/client');
return new ContentlayerClient(); return new ContentlayerClient();
} }
async function getWordpressClient() { async function getWordpressClient() {
const { WordpressClient } = await import('../../wordpress'); const { WordpressClient } = await import('../../wordpress/src/wp-client');
return new WordpressClient(); return new WordpressClient();
} }

View File

@@ -36,16 +36,30 @@ export class DatabaseWebhookHandlerService {
// handle the webhook // handle the webhook
const service = new DatabaseWebhookRouterService(client); const service = new DatabaseWebhookRouterService(client);
await service.handleWebhook(json); try {
await service.handleWebhook(json);
Logger.info( Logger.info(
{ {
name: this.namespace, name: this.namespace,
table, table,
type, type,
}, },
'Webhook processed successfully', 'Webhook processed successfully',
); );
} catch (error) {
Logger.error(
{
name: this.namespace,
table,
type,
error,
},
'Failed to process webhook',
);
throw error;
}
} }
private assertSignatureIsAuthentic(request: Request, webhooksSecret: string) { private assertSignatureIsAuthentic(request: Request, webhooksSecret: string) {

View File

@@ -12,19 +12,19 @@ export class DatabaseWebhookRouterService {
case 'invitations': { case 'invitations': {
const payload = body as RecordChange<typeof body.table>; const payload = body as RecordChange<typeof body.table>;
return this.handleInvitations(payload); return this.handleInvitationsWebhook(payload);
} }
case 'subscriptions': { case 'subscriptions': {
const payload = body as RecordChange<typeof body.table>; const payload = body as RecordChange<typeof body.table>;
return this.handleSubscriptions(payload); return this.handleSubscriptionsWebhook(payload);
} }
case 'accounts_memberships': { case 'accounts_memberships': {
const payload = body as RecordChange<typeof body.table>; const payload = body as RecordChange<typeof body.table>;
return this.handleAccountsMemberships(payload); return this.handleAccountsMembershipsWebhook(payload);
} }
default: default:
@@ -32,7 +32,7 @@ export class DatabaseWebhookRouterService {
} }
} }
private async handleInvitations(body: RecordChange<'invitations'>) { private async handleInvitationsWebhook(body: RecordChange<'invitations'>) {
const { AccountInvitationsWebhookService } = await import( const { AccountInvitationsWebhookService } = await import(
'@kit/team-accounts/webhooks' '@kit/team-accounts/webhooks'
); );
@@ -42,14 +42,18 @@ export class DatabaseWebhookRouterService {
return service.handleInvitationWebhook(body.record); return service.handleInvitationWebhook(body.record);
} }
private async handleSubscriptions(body: RecordChange<'subscriptions'>) { private async handleSubscriptionsWebhook(
body: RecordChange<'subscriptions'>,
) {
const { BillingWebhooksService } = await import('@kit/billing-gateway'); const { BillingWebhooksService } = await import('@kit/billing-gateway');
const service = new BillingWebhooksService(); const service = new BillingWebhooksService();
return service.handleSubscriptionDeletedWebhook(body.record); if (body.type === 'DELETE' && body.old_record) {
return service.handleSubscriptionDeletedWebhook(body.old_record);
}
} }
private handleAccountsMemberships( private handleAccountsMembershipsWebhook(
payload: RecordChange<'accounts_memberships'>, payload: RecordChange<'accounts_memberships'>,
) { ) {
// no-op // no-op