mirror of
https://github.com/DeNNiiInc/Advanced-Smtp-Tester.git
synced 2026-04-17 17:35:59 +00:00
Improve error handling in server and client
This commit is contained in:
@@ -4,7 +4,7 @@ function togglePassword() {
|
||||
passInput.setAttribute('type', type);
|
||||
}
|
||||
|
||||
document.getElementById('smtpForm').addEventListener('submit', async function(e) {
|
||||
document.getElementById('smtpForm').addEventListener('submit', async function (e) {
|
||||
e.preventDefault();
|
||||
|
||||
const btn = document.getElementById('testBtn');
|
||||
@@ -37,6 +37,17 @@ document.getElementById('smtpForm').addEventListener('submit', async function(e)
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
if (!response.ok) {
|
||||
// Handle non-200 responses
|
||||
const contentType = response.headers.get("content-type");
|
||||
if (contentType && contentType.includes("application/json")) {
|
||||
const errorResult = await response.json();
|
||||
throw new Error(errorResult.message || errorResult.error || "Server Error");
|
||||
} else {
|
||||
const text = await response.text();
|
||||
throw new Error(`Server returned ${response.status}: ${text.substring(0, 100)}...`);
|
||||
}
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
@@ -48,6 +59,7 @@ document.getElementById('smtpForm').addEventListener('submit', async function(e)
|
||||
statusDiv.textContent = '✅ Success! Email Sent Successfully.';
|
||||
logOutput.textContent = JSON.stringify(result.details, null, 2);
|
||||
} else {
|
||||
// This block handles cases where response was 200 OK but success is false (business logic error)
|
||||
statusDiv.classList.add('status-error');
|
||||
statusDiv.textContent = '❌ Error: ' + result.message;
|
||||
logOutput.textContent = result.error || 'Unknown error occurred.';
|
||||
@@ -56,8 +68,8 @@ document.getElementById('smtpForm').addEventListener('submit', async function(e)
|
||||
} catch (error) {
|
||||
resultsDiv.classList.remove('hidden');
|
||||
statusDiv.classList.add('status-error');
|
||||
statusDiv.textContent = '❌ Network Error';
|
||||
logOutput.textContent = error.toString();
|
||||
statusDiv.textContent = '❌ Error Caught'; // Changed from "Network Error" to be more accurate
|
||||
logOutput.textContent = error.message; // Use error.message instead of error.toString()
|
||||
} finally {
|
||||
// Reset Button
|
||||
btn.disabled = false;
|
||||
@@ -67,7 +79,7 @@ document.getElementById('smtpForm').addEventListener('submit', async function(e)
|
||||
});
|
||||
|
||||
// Auto Discovery Test Handler
|
||||
document.getElementById('autoTestBtn').addEventListener('click', async function() {
|
||||
document.getElementById('autoTestBtn').addEventListener('click', async function () {
|
||||
const btn = document.getElementById('autoTestBtn');
|
||||
const spinner = btn.querySelector('.loading-spinner');
|
||||
const btnText = btn.querySelector('.btn-text');
|
||||
@@ -108,6 +120,16 @@ document.getElementById('autoTestBtn').addEventListener('click', async function(
|
||||
},
|
||||
body: JSON.stringify(autoTestData),
|
||||
});
|
||||
if (!response.ok) {
|
||||
const contentType = response.headers.get("content-type");
|
||||
if (contentType && contentType.includes("application/json")) {
|
||||
const errorResult = await response.json();
|
||||
throw new Error(errorResult.message || errorResult.error || "Server Error");
|
||||
} else {
|
||||
const text = await response.text();
|
||||
throw new Error(`Server returned ${response.status}: ${text.substring(0, 100)}...`);
|
||||
}
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
@@ -149,8 +171,8 @@ document.getElementById('autoTestBtn').addEventListener('click', async function(
|
||||
} catch (error) {
|
||||
resultsDiv.classList.remove('hidden');
|
||||
statusDiv.classList.add('status-error');
|
||||
statusDiv.textContent = '❌ Network Error';
|
||||
logOutput.textContent = error.toString();
|
||||
statusDiv.textContent = '❌ Error Caught';
|
||||
logOutput.textContent = error.message;
|
||||
} finally {
|
||||
// Reset Button
|
||||
btn.disabled = false;
|
||||
|
||||
17
server.js
17
server.js
@@ -23,8 +23,7 @@ app.post('/api/test-smtp', async (req, res) => {
|
||||
|
||||
// Log intent
|
||||
console.log(
|
||||
`Attempting SMTP connection to ${host}:${port} (${
|
||||
secure ? "Secure" : "Insecure"
|
||||
`Attempting SMTP connection to ${host}:${port} (${secure ? "Secure" : "Insecure"
|
||||
}) for ${user}`
|
||||
);
|
||||
|
||||
@@ -46,9 +45,9 @@ app.post('/api/test-smtp', async (req, res) => {
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const transporter = nodemailer.createTransport(transporterConfig);
|
||||
|
||||
try {
|
||||
// 1. Verify Connection
|
||||
await transporter.verify();
|
||||
console.log("SMTP Connection Verified Successfully");
|
||||
@@ -248,9 +247,9 @@ app.post('/api/auto-test-smtp', async (req, res) => {
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const transporter = nodemailer.createTransport(transporterConfig);
|
||||
|
||||
try {
|
||||
// Verify connection
|
||||
await transporter.verify();
|
||||
console.log(`✅ ${config.name} - Connection successful!`);
|
||||
@@ -420,6 +419,16 @@ app.post('/api/auto-test-smtp', async (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
// Global Error Handler
|
||||
app.use((err, req, res, next) => {
|
||||
console.error("Unhandled Error:", err);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Internal Server Error",
|
||||
error: err.message || "Unknown error occurred"
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server running at http://localhost:${port}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user