fix(web): Preserve API error messages in config save handlers (#151)

- Refactor error handling to use async/await for clearer flow
- Store response status before JSON parsing to preserve context
- Extract specific error messages from API responses (data.message)
- Only use generic HTTP status messages when JSON parsing fails
- Ensure API error messages like validation failures are shown to users
  instead of generic 'HTTP 500: Internal Server Error'

Fixes issue where specific API error messages were being replaced
with generic HTTP status messages in the catch handler.

Co-authored-by: Chuck <chuck@example.com>
This commit is contained in:
Chuck
2025-12-27 18:28:02 -05:00
committed by GitHub
parent b0d65581df
commit 33e4f3680c

View File

@@ -218,19 +218,28 @@ function saveMainConfig() {
}, },
body: JSON.stringify(config) body: JSON.stringify(config)
}) })
.then(response => { .then(async response => {
// Check if response is OK before parsing JSON // Store status and statusText before parsing
if (!response.ok) { const status = response.status;
// Try to parse error response as JSON, fallback to status text const statusText = response.statusText;
return response.json().then(data => {
throw new Error(data.message || response.statusText); // Try to parse JSON response
}).catch(() => { let data;
throw new Error(`HTTP ${response.status}: ${response.statusText}`); try {
}); data = await response.json();
} catch (parseError) {
// If JSON parsing fails, throw generic HTTP error
throw new Error(`HTTP ${status}: ${statusText}`);
} }
return response.json();
}) // Handle non-OK responses
.then(data => { if (!response.ok) {
// Extract specific error message from API response if available
const errorMessage = data.message || data.status || statusText;
throw new Error(errorMessage);
}
// Handle successful responses
if (data.status === 'success') { if (data.status === 'success') {
showNotification('config.json saved successfully!', 'success'); showNotification('config.json saved successfully!', 'success');
} else { } else {
@@ -238,6 +247,7 @@ function saveMainConfig() {
} }
}) })
.catch(error => { .catch(error => {
// Preserve the error message that was intentionally thrown
showNotification('Error saving config.json: ' + (error.message || 'An error occurred'), 'error'); showNotification('Error saving config.json: ' + (error.message || 'An error occurred'), 'error');
}); });
} catch (e) { } catch (e) {
@@ -265,19 +275,28 @@ function saveSecretsConfig() {
}, },
body: JSON.stringify(config) body: JSON.stringify(config)
}) })
.then(response => { .then(async response => {
// Check if response is OK before parsing JSON // Store status and statusText before parsing
if (!response.ok) { const status = response.status;
// Try to parse error response as JSON, fallback to status text const statusText = response.statusText;
return response.json().then(data => {
throw new Error(data.message || response.statusText); // Try to parse JSON response
}).catch(() => { let data;
throw new Error(`HTTP ${response.status}: ${response.statusText}`); try {
}); data = await response.json();
} catch (parseError) {
// If JSON parsing fails, throw generic HTTP error
throw new Error(`HTTP ${status}: ${statusText}`);
} }
return response.json();
}) // Handle non-OK responses
.then(data => { if (!response.ok) {
// Extract specific error message from API response if available
const errorMessage = data.message || data.status || statusText;
throw new Error(errorMessage);
}
// Handle successful responses
if (data.status === 'success') { if (data.status === 'success') {
showNotification('config_secrets.json saved successfully!', 'success'); showNotification('config_secrets.json saved successfully!', 'success');
} else { } else {
@@ -285,6 +304,7 @@ function saveSecretsConfig() {
} }
}) })
.catch(error => { .catch(error => {
// Preserve the error message that was intentionally thrown
showNotification('Error saving config_secrets.json: ' + (error.message || 'An error occurred'), 'error'); showNotification('Error saving config_secrets.json: ' + (error.message || 'An error occurred'), 'error');
}); });
} catch (e) { } catch (e) {