@@ -30,7 +30,7 @@ functions.cloudEvent('StopBillingCloudEvent', async cloudEvent => {
3030 let projectId = projectIdEnv ;
3131
3232 if ( projectId === undefined ) {
33- console . log ( 'Project ID not found in Env variables. Reading metadata...' ) ;
33+ console . log ( 'Project ID not found in env variables. Getting GCP metadata...' ) ;
3434 try {
3535 projectId = await gcpMetadata . project ( 'project-id' ) ;
3636 } catch ( error ) {
@@ -43,12 +43,20 @@ functions.cloudEvent('StopBillingCloudEvent', async cloudEvent => {
4343
4444 // Find more information about the notification format here:
4545 // https://cloud.google.com/billing/docs/how-to/budgets-programmatic-notifications#notification-format
46- const eventData = Buffer . from (
47- cloudEvent . data [ 'message' ] [ 'data' ] ,
48- 'base64'
49- ) . toString ( ) ;
46+ const messageData = cloudEvent . data ?. message ?. data ;
47+ if ( ! messageData ) {
48+ console . error ( 'Invalid CloudEvent: missing data.message.data' ) ;
49+ return ;
50+ }
51+ const eventData = Buffer . from ( messageData , 'base64' ) . toString ( ) ;
5052
51- const eventObject = JSON . parse ( eventData ) ;
53+ let eventObject ;
54+ try {
55+ eventObject = JSON . parse ( eventData ) ;
56+ } catch ( e ) {
57+ console . error ( 'Error parsing event data:' , e ) ;
58+ return ;
59+ }
5260
5361 console . log (
5462 `Project ID: ${ projectId } ` +
@@ -63,7 +71,7 @@ functions.cloudEvent('StopBillingCloudEvent', async cloudEvent => {
6371
6472 console . log ( `Disabling billing for project '${ projectName } '...` ) ;
6573
66- const billingEnabled = await _isBillingEnabled ( projectName ) ;
74+ const billingEnabled = _isBillingEnabled ( projectName ) ;
6775 if ( billingEnabled ) {
6876 _disableBillingForProject ( projectName , simulateDeactivation ) ;
6977 } else {
0 commit comments