{"record":{"id":"2a64094c364e8345","repo":"ellite/Wallos","slug":"translate-network-response-error","errorCode":null,"errorMessage":"translate(\"network_response_error\")","messagePattern":"translate\\(\"network_response_error\"\\)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"scripts/settings.js","lineNumber":680,"sourceCode":"\r\nfunction renamePayment(paymentId, newName) {\r\n  const name = newName.trim();\r\n  if (!name) return;\r\n\r\n  const formData = new FormData();\r\n  formData.append(\"paymentId\", paymentId);\r\n  formData.append(\"name\", name);\r\n\r\n  fetch(\"endpoints/payments/rename.php\", {\r\n    method: \"POST\",\r\n    headers: {\r\n      \"X-CSRF-Token\": window.csrfToken,\r\n    },\r\n    body: formData,\r\n  })\r\n    .then(response => {\r\n      if (!response.ok) {\r\n        throw new Error(translate(\"network_response_error\"));\r\n      }\r\n      return response.json();\r\n    })\r\n    .then(data => {\r\n      if (data.success) {\r\n        showSuccessMessage(`${newName} ${data.message}`);\r\n      } else {\r\n        showErrorMessage(data.message || translate(\"failed_save_payment_method\"));\r\n      }\r\n    })\r\n    .catch(error => {\r\n      console.error(error);\r\n      showErrorMessage(translate(\"unknown_error\"));\r\n    });\r\n}\r\n\r\n\r\ndocument.body.addEventListener('keypress', function (e) {\r","sourceCodeStart":662,"sourceCodeEnd":698,"githubUrl":"https://github.com/ellite/Wallos/blob/52820e87ca5a6e105fdbb7f1c0c681bc0cfee2fd/scripts/settings.js#L662-L698","documentation":"renamePayment performs a fetch to a settings/payment endpoint and throws a generic Error with the localized message translate(\"network_response_error\") whenever the HTTP response status is not OK (response.ok is false, i.e. status outside 200-299). It is a deliberately generic network/HTTP failure message shown to end users. The actual cause (404, 403, CSRF rejection, 500) is hidden behind this single message because the code does not inspect response.status or the response body.","triggerScenarios":"Any fetch call in renamePayment that resolves with a non-2xx status: the endpoint URL changed or is wrong, the session expired, the X-CSRF-Token header is stale/missing and the server returns 419/403, or the server errors with 500 while processing the form data POST.","commonSituations":"Users with an expired PHP session or stale CSRF token submitting the rename form; a proxy or server returning 404 after a route rename; a backend exception returning 500 during the payment rename; deployments where the endpoint moved behind a new base path.","solutions":["Open the browser devtools Network tab, retry the rename, and check the actual HTTP status and response body of the failing request.","Regenerate the page so window.csrfToken matches the current session (stale CSRF tokens cause 403/419 responses).","Verify the fetch URL in renamePayment matches the server route for renaming payments.","Improve the throw to include response.status and, when the body is JSON, data.message so the real server error surfaces instead of the generic message."],"exampleFix":"// before\nif (!response.ok) {\n  throw new Error(translate(\"network_response_error\"));\n}\n// after\nif (!response.ok) {\n  const body = await response.text();\n  let serverMessage = \"\";\n  try { serverMessage = JSON.parse(body).message ?? \"\"; } catch (e) {}\n  throw new Error(`${translate(\"network_response_error\")} (HTTP ${response.status})${serverMessage ? \": \" + serverMessage : \"\"}`);\n}","handlingStrategy":"try-catch","validationCode":"function canSubmitPaymentRename() {\n  return typeof window.csrfToken === 'string' && window.csrfToken.length > 0 && document.body.dataset.sessionActive === 'true';\n}","typeGuard":null,"tryCatchPattern":"renamePayment(...).catch(err => {\n  if (/network_response_error/.test(err.message)) {\n    showErrorMessage(`${translate('network_response_error')} — please reload the page and try again (session may have expired)`);\n  } else {\n    showErrorMessage(err.message);\n  }\n});","preventionTips":["Refresh the page after long idle periods so session and CSRF token stay valid before submitting forms.","Check the Network tab for the failing request's status code to diagnose the real cause.","Keep server-side error responses as JSON with a message field so the client can show specific errors.","Include response.status in thrown errors instead of a single generic message."],"tags":["network","fetch","http-error","javascript"],"backgroundTag":"http-error-response","analyzedSha":"52820e87ca5a6e105fdbb7f1c0c681bc0cfee2fd","analyzedAt":"2026-09-13T14:09:30.873Z","contentChangedAt":"2026-09-13T14:09:30.873Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}