Improve email sending by adding error handling and fixing security

Add try-catch blocks to Supabase functions for robust error handling during email sending and response processing.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: a46bc7a0-bbf8-43ab-97c0-a58c66c2e365
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
This commit is contained in:
pac7
2025-10-08 12:07:42 +00:00
parent 0b57cba16f
commit 163b721271

View File

@@ -162,7 +162,9 @@ serve(async (req) => {
throw new Error('Email configuration is incomplete. Please check environment variables.');
}
const emailResponse = await fetch('https://api.forwardemail.net/v1/emails', {
let emailResponse;
try {
emailResponse = await fetch('https://api.forwardemail.net/v1/emails', {
method: 'POST',
headers: {
'Authorization': 'Basic ' + btoa(forwardEmailApiKey + ':'),
@@ -176,14 +178,29 @@ serve(async (req) => {
text: emailText,
}),
});
} catch (fetchError) {
console.error('Network error sending email:', fetchError);
throw new Error('Network error: Unable to reach email service');
}
if (!emailResponse.ok) {
const errorText = await emailResponse.text();
let errorText;
try {
errorText = await emailResponse.text();
} catch (parseError) {
errorText = 'Unable to parse error response';
}
console.error('ForwardEmail API error:', errorText);
throw new Error(`Failed to send email: ${emailResponse.status} - ${errorText}`);
}
const emailResult = await emailResponse.json();
let emailResult;
try {
emailResult = await emailResponse.json();
} catch (parseError) {
console.error('Failed to parse email API response:', parseError);
throw new Error('Invalid response from email service');
}
console.log('Email sent successfully:', emailResult);
// Update submission with notification status