ellite/Wallos · error · Error
` (HTTP )
Error message
`${translate('network_response_error')} (HTTP ${response.status})` What it means
This code first reads the response as text and attempts JSON.parse. If parsing fails (the body is not JSON at all), it throws a localized network_response_error annotated with the HTTP status. This is the 'malformed/non-JSON body' branch: the server replied, but not with the JSON the client expects (e.g. an HTML error page or empty body).
Solutions
- Inspect responseText in devtools or by logging it before parse to see what the server actually returned.
- Check server error logs for PHP warnings/notices emitted before the JSON payload.
- Ensure the session is alive and the request is not being redirected to an HTML page (check response.redirected / final URL).
- Handle the case explicitly: treat non-JSON bodies as server-side failures and surface the status plus a snippet of the body to the user.
Example fix
// before
try {
data = JSON.parse(responseText);
} catch (error) {
throw new Error(`${translate('network_response_error')} (HTTP ${response.status})`);
}
// after
try {
data = JSON.parse(responseText);
} catch (error) {
console.error('Non-JSON response:', responseText.slice(0, 200));
throw new Error(`${translate('network_response_error')} (HTTP ${response.status}, non-JSON body)`);
} Defensive patterns
Strategy: try-catch
Validate before calling
function looksLikeJson(text) {
const t = text.trim();
return t.startsWith('{') || t.startsWith('[');
} Try / catch
try {
data = JSON.parse(responseText);
} catch (error) {
if (response.redirected || /<html/i.test(responseText)) {
window.location.reload(); // session likely expired, redirected to HTML page
return;
}
throw new Error(`${translate('network_response_error')} (HTTP ${response.status}, non-JSON body)`);
} Prevention
- Disable PHP display_errors in production so warnings cannot corrupt JSON output.
- Check response.redirected on fetch responses to detect session-expiry redirects to HTML pages.
- Validate server endpoints return Content-Type: application/json.
- Log the first bytes of unexpected bodies to spot proxy/HTML injection early.
When it happens
Trigger: The fetch resolves and response.text() returns a body that JSON.parse cannot parse: an HTML 500 error page from PHP, an empty body, plain-text output before/instead of JSON (e.g. a PHP warning printed before json_encode), or a redirect to an HTML login page.
Common situations: PHP fatal error or warning output corrupting the JSON body; session expiry redirecting the API call to an HTML login page; a reverse proxy serving its own HTML error page; calling the wrong URL that returns HTML.
Understand the failure class
Background: "Invalid JSON response" and "Failed to parse response" errors: when an API answers 200 but the body isn't the JSON your library expected — this error's family across 28 libraries.
Related errors
- translate("network_response_error")
- translate("network_response_error")
- Invalid JSON response
- HTTP
- $this->lang('smtp_connect_failed')
AI-assisted analysis of ellite/Wallos@52820e87ca (2026-09-13).
Data as JSON: /api/errors/9c6b0e04ccdeaef4.
Report an issue: GitHub.
Appendix: source
Thrown at scripts/settings.js:1381
button.classList.add("hidden");
spinner.classList.remove("hidden");
fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': window.csrfToken,
}
})
.then(async response => {
const responseText = await response.text();
let data;
try {
data = JSON.parse(responseText);
} catch (error) {
throw new Error(`${translate('network_response_error')} (HTTP ${response.status})`);
}
if (!response.ok && !data.message) {
throw new Error(`${translate('network_response_error')} (HTTP ${response.status})`);
}
return data;
})
.then(data => {
if (data.success) {
showSuccessMessage(data.message);
} else {
showErrorMessage(data.message);
}
})
.catch(error => {
showErrorMessage(error.message || translate('unknown_error'));
})
View on GitHub (pinned to 52820e87ca)