@@ -107,17 +107,33 @@ def validate_recaptcha(recaptcha_response):
107107 """
108108 Validates the ReCaptcha response.
109109 """
110- if not recaptcha .verify (response = recaptcha_response ):
111- raise ValueError ('Error: ReCaptcha verification failed!' )
110+ try :
111+ if not recaptcha .verify (response = recaptcha_response ):
112+ logging .error ('ReCaptcha verification failed for response: %s' , recaptcha_response )
113+ raise ValueError ('Error: ReCaptcha verification failed!' )
114+ else :
115+ logging .info ('ReCaptcha verification succeeded' )
116+ except Exception as e :
117+ logging .error ('ReCaptcha validation encountered an error: %s' , str (e ))
118+ raise
112119
113120def send_email (message ):
114121 """
115- Sends the email using SendGrid.
122+ Sends the email using SendGrid and logs detailed information for debugging .
116123 """
117- sg = SendGridAPIClient (SENDGRIDAPIKEY )
118- response = sg .send (message )
119- if response .status_code not in [200 , 201 , 202 ]:
120- raise ValueError (f"Error: Failed to send email. Status code: { response .status_code } " )
124+ try :
125+ sg = SendGridAPIClient (SENDGRIDAPIKEY )
126+ response = sg .send (message )
127+ logging .info ('SendGrid response status code: %s' , response .status_code )
128+ if response .status_code not in [200 , 201 , 202 ]:
129+ logging .error ('SendGrid failed with status code: %s, response body: %s' , response .status_code , response .body )
130+ raise ValueError (f"Error: Failed to send email. Status code: { response .status_code } , body: { response .body } " )
131+ else :
132+ logging .info ('Email sent successfully. Status code: %s, response body: %s' , response .status_code , response .body )
133+ except Exception as e :
134+ logging .error ('Error sending email via SendGrid: %s' , str (e ))
135+ raise
136+
121137
122138# Validate required environment variables
123139required_env_vars = ['RECAPTCHASITEKEY' , 'RECAPTCHASECRETKEY' , 'SENDGRIDAPIKEY' , 'SENDGRIDFROMEMAIL' ]
0 commit comments