mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 11:11:12 -05:00
feat: Implement retry logic and tracking
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { serve } from "https://deno.land/std@0.190.0/http/server.ts";
|
||||
import { createClient } from "https://esm.sh/@supabase/supabase-js@2.57.4";
|
||||
import { edgeLogger, startRequest, endRequest } from '../_shared/logger.ts';
|
||||
import { withEdgeRetry } from '../_shared/retryHelper.ts';
|
||||
|
||||
const corsHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
@@ -169,7 +170,7 @@ serve(async (req) => {
|
||||
Please review this submission in the admin panel.
|
||||
`;
|
||||
|
||||
// Send email via ForwardEmail API
|
||||
// Send email via ForwardEmail API with retry
|
||||
const forwardEmailApiKey = Deno.env.get('FORWARDEMAIL_API_KEY');
|
||||
const adminEmail = Deno.env.get('ADMIN_EMAIL_ADDRESS');
|
||||
const fromEmail = Deno.env.get('FROM_EMAIL_ADDRESS');
|
||||
@@ -178,55 +179,43 @@ serve(async (req) => {
|
||||
throw new Error('Email configuration is incomplete. Please check environment variables.');
|
||||
}
|
||||
|
||||
let emailResponse;
|
||||
try {
|
||||
emailResponse = await fetch('https://api.forwardemail.net/v1/emails', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': 'Basic ' + btoa(forwardEmailApiKey + ':'),
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
from: fromEmail,
|
||||
to: adminEmail,
|
||||
subject: emailSubject,
|
||||
html: emailHtml,
|
||||
text: emailText,
|
||||
}),
|
||||
});
|
||||
} catch (fetchError) {
|
||||
edgeLogger.error('Network error sending email', {
|
||||
requestId: tracking.requestId,
|
||||
error: fetchError.message
|
||||
});
|
||||
throw new Error('Network error: Unable to reach email service');
|
||||
}
|
||||
const emailResult = await withEdgeRetry(
|
||||
async () => {
|
||||
const emailResponse = await fetch('https://api.forwardemail.net/v1/emails', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': 'Basic ' + btoa(forwardEmailApiKey + ':'),
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
from: fromEmail,
|
||||
to: adminEmail,
|
||||
subject: emailSubject,
|
||||
html: emailHtml,
|
||||
text: emailText,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!emailResponse.ok) {
|
||||
let errorText;
|
||||
try {
|
||||
errorText = await emailResponse.text();
|
||||
} catch (parseError) {
|
||||
errorText = 'Unable to parse error response';
|
||||
}
|
||||
edgeLogger.error('ForwardEmail API error', {
|
||||
requestId: tracking.requestId,
|
||||
status: emailResponse.status,
|
||||
errorText
|
||||
});
|
||||
throw new Error(`Failed to send email: ${emailResponse.status} - ${errorText}`);
|
||||
}
|
||||
if (!emailResponse.ok) {
|
||||
let errorText;
|
||||
try {
|
||||
errorText = await emailResponse.text();
|
||||
} catch (parseError) {
|
||||
errorText = 'Unable to parse error response';
|
||||
}
|
||||
|
||||
const error = new Error(`Failed to send email: ${emailResponse.status} - ${errorText}`);
|
||||
(error as any).status = emailResponse.status;
|
||||
throw error;
|
||||
}
|
||||
|
||||
let emailResult;
|
||||
try {
|
||||
emailResult = await emailResponse.json();
|
||||
} catch (parseError) {
|
||||
edgeLogger.error('Failed to parse email API response', {
|
||||
requestId: tracking.requestId,
|
||||
error: parseError.message
|
||||
});
|
||||
throw new Error('Invalid response from email service');
|
||||
}
|
||||
const result = await emailResponse.json();
|
||||
return result;
|
||||
},
|
||||
{ maxAttempts: 3, baseDelay: 1500, maxDelay: 10000 },
|
||||
tracking.requestId,
|
||||
'send-escalation-email'
|
||||
);
|
||||
edgeLogger.info('Email sent successfully', {
|
||||
requestId: tracking.requestId,
|
||||
emailId: emailResult.id
|
||||
|
||||
Reference in New Issue
Block a user