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

  1. Inspect responseText in devtools or by logging it before parse to see what the server actually returned.
  2. Check server error logs for PHP warnings/notices emitted before the JSON payload.
  3. Ensure the session is alive and the request is not being redirected to an HTML page (check response.redirected / final URL).
  4. 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

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


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)