11import { db } from '@sim/db'
22import { account , webhook } from '@sim/db/schema'
33import { createLogger } from '@sim/logger'
4+ import { toStringOrNull } from '@sim/utils/coerce'
45import { toRecord } from '@sim/utils/object'
56import { eq } from 'drizzle-orm'
67import { validateAirtableId } from '@/lib/core/security/input-validation'
@@ -14,6 +15,7 @@ import {
1415 getNotificationUrl ,
1516 getProviderConfig ,
1617} from '@/lib/webhooks/provider-subscription-utils'
18+ import { WebhookDeploymentConfigurationError } from '@/lib/webhooks/providers/errors'
1719import type {
1820 DeleteSubscriptionContext ,
1921 FormatInputContext ,
@@ -435,30 +437,30 @@ export const airtableHandler: WebhookProviderHandler = {
435437 } : SubscriptionContext ) : Promise < SubscriptionResult | undefined > {
436438 try {
437439 const config = getProviderConfig ( webhookRecord )
438- const { baseId, tableId, includeCellValuesInFieldIds, credentialId } = config as {
439- baseId ?: string
440- tableId ?: string
440+ const { includeCellValuesInFieldIds, credentialId } = config as {
441441 includeCellValuesInFieldIds ?: string
442442 credentialId ?: string
443443 }
444+ const baseId = toStringOrNull ( config . baseId )
445+ const tableId = toStringOrNull ( config . tableId )
444446
445447 if ( ! baseId || ! tableId ) {
446448 logger . warn ( `[${ requestId } ] Missing baseId or tableId for Airtable webhook creation.` , {
447449 webhookId : webhookRecord . id ,
448450 } )
449- throw new Error (
451+ throw new WebhookDeploymentConfigurationError (
450452 'Base ID and Table ID are required to create Airtable webhook. Please provide valid Airtable base and table IDs.'
451453 )
452454 }
453455
454456 const baseIdValidation = validateAirtableId ( baseId , 'app' , 'baseId' )
455457 if ( ! baseIdValidation . isValid ) {
456- throw new Error ( baseIdValidation . error )
458+ throw new WebhookDeploymentConfigurationError ( baseIdValidation . error )
457459 }
458460
459461 const tableIdValidation = validateAirtableId ( tableId , 'tbl' , 'tableId' )
460462 if ( ! tableIdValidation . isValid ) {
461- throw new Error ( tableIdValidation . error )
463+ throw new WebhookDeploymentConfigurationError ( tableIdValidation . error )
462464 }
463465
464466 const accessToken = credentialId
@@ -535,6 +537,7 @@ export const airtableHandler: WebhookProviderHandler = {
535537 )
536538 return { providerConfigUpdates : { externalId : responseBody . id } }
537539 } catch ( error : unknown ) {
540+ if ( error instanceof WebhookDeploymentConfigurationError ) throw error
538541 const err = error as Error
539542 logger . error (
540543 `[${ requestId } ] Exception during Airtable webhook creation for webhook ${ webhookRecord . id } .` ,
0 commit comments