Codex commited on
Commit
4992155
·
1 Parent(s): 10e9ccd

Surface Brevo API auth errors and avoid SMTP fallback on 401/403

Browse files
Files changed (1) hide show
  1. server.js +21 -13
server.js CHANGED
@@ -64,22 +64,25 @@ async function sendViaBrevoApi(message) {
64
  htmlContent: message.html
65
  };
66
 
67
- await axios.post(
68
- 'https://api.brevo.com/v3/smtp/email',
69
- payload,
70
- {
71
- headers: {
72
- 'api-key': apiKey,
73
- accept: 'application/json',
74
- 'content-type': 'application/json'
75
- },
76
- timeout: BREVO_API_TIMEOUT
77
- }
78
- );
79
 
80
  console.log('Email sent via Brevo API');
81
  }
82
 
 
 
 
 
 
 
 
83
  function getEmailTransport() {
84
  if (emailTransport || emailConfigChecked) return emailTransport;
85
 
@@ -165,7 +168,12 @@ async function sendEmail(message) {
165
  await sendViaBrevoApi(message);
166
  return;
167
  } catch (err) {
168
- console.error('Brevo API send failed, falling back to SMTP if available:', err && err.message ? err.message : err);
 
 
 
 
 
169
  }
170
  }
171
 
 
64
  htmlContent: message.html
65
  };
66
 
67
+ await axios.post('https://api.brevo.com/v3/smtp/email', payload, {
68
+ headers: {
69
+ 'api-key': apiKey,
70
+ accept: 'application/json',
71
+ 'content-type': 'application/json'
72
+ },
73
+ timeout: BREVO_API_TIMEOUT
74
+ });
 
 
 
 
75
 
76
  console.log('Email sent via Brevo API');
77
  }
78
 
79
+ function describeBrevoError(err) {
80
+ const status = err?.response?.status;
81
+ const dataMsg = err?.response?.data?.message || err?.response?.data?.error || '';
82
+ const text = err?.message || String(err);
83
+ return { status, detail: dataMsg || text };
84
+ }
85
+
86
  function getEmailTransport() {
87
  if (emailTransport || emailConfigChecked) return emailTransport;
88
 
 
168
  await sendViaBrevoApi(message);
169
  return;
170
  } catch (err) {
171
+ const info = describeBrevoError(err);
172
+ // If auth error, do not fallback to SMTP (likely wrong key) — surface error early
173
+ if (info.status === 401 || info.status === 403) {
174
+ throw new Error(`Brevo API auth error (${info.status}): ${info.detail}`);
175
+ }
176
+ console.error('Brevo API send failed, falling back to SMTP if available:', info.detail || err);
177
  }
178
  }
179