{"record":{"id":"9f00ddcc8b561d89","repo":"hcengineering/platform","slug":"network-error-string-err","errorCode":null,"errorMessage":"Network error: ${String(err)}","messagePattern":"Network error: (.+?)","errorType":"exception","errorClass":"NetworkError","httpStatus":null,"severity":"error","filePath":"packages/payment-client/src/client.ts","lineNumber":161,"sourceCode":"    const response = await fetchSafe(url, { headers: { ...this.headers } })\n    return (await response.json()) as CheckoutStatus\n  }\n}\n\n/**\n * Safe fetch wrapper that handles errors consistently\n * @param url - URL to fetch\n * @param init - Fetch options\n * @returns Response\n * @throws NetworkError on network issues\n * @throws PaymentError on non-ok responses\n */\nasync function fetchSafe (url: string | URL, init?: RequestInit): Promise<Response> {\n  let response\n  try {\n    response = await fetch(url, init)\n  } catch (err: any) {\n    throw new NetworkError(`Network error: ${String(err)}`)\n  }\n\n  if (!response.ok) {\n    const text = await response.text()\n    try {\n      const error = JSON.parse(text)\n      throw new PaymentError(error.error ?? text)\n    } catch {\n      throw new PaymentError(`Payment service error: ${response.status} ${text}`)\n    }\n  }\n\n  return response\n}\n","sourceCodeStart":143,"sourceCodeEnd":176,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/packages/payment-client/src/client.ts#L143-L176","documentation":"fetchSafe wraps the underlying fetch call and converts low-level network failures into a typed NetworkError with the message `Network error: ${String(err)}`. This happens when fetch itself rejects — DNS failure, connection refused, TLS errors, aborted requests — before any HTTP response exists. The original error text is embedded in the message for diagnosis.","triggerScenarios":"Any PaymentClient operation (via `response`, which calls fetchSafe) where `fetch` throws: unreachable host, wrong port, DNS resolution failure, self-signed/expired certificate, offline environment, or request aborted.","commonSituations":"Payment service down or redeploying, typo in the payment URL hostname, container networking/DNS issues in Kubernetes, firewall blocking egress, or a browser CORS preflight hard-failing.","solutions":["Read the embedded cause in the message (e.g. 'fetch failed', 'ENOTFOUND', 'ECONNREFUSED') to identify the network problem.","Verify the payment service URL is correct and the service is running/reachable (curl the health endpoint).","Check network connectivity, DNS, and egress/firewall rules from the client environment.","Implement retry with backoff for transient outages; NetworkError is the type to match for retry decisions."],"exampleFix":"// before\nconst res = await paymentClient.response('/charge', init)\n// after\ntry {\n  const res = await paymentClient.response('/charge', init)\n} catch (e) {\n  if (e instanceof NetworkError) {\n    console.error('Payment service unreachable:', e.message)\n    // retry or alert\n  } else throw e\n}","handlingStrategy":"retry","validationCode":"const url = new URL(baseUrl) // throws immediately on malformed URL, before any request\nif (!/^https?:$/.test(url.protocol)) throw new Error('Payment URL must be http(s)')","typeGuard":"function isNetworkError(e: unknown): e is NetworkError {\n  return e instanceof NetworkError\n}","tryCatchPattern":"async function withRetry<T>(fn: () => Promise<T>, attempts = 3): Promise<T> {\n  for (let i = 1; ; i++) {\n    try { return await fn() }\n    catch (e) {\n      if (isNetworkError(e) && i < attempts) {\n        await new Promise(r => setTimeout(r, 2 ** i * 250)); continue\n      }\n      throw e\n    }\n  }\n}","preventionTips":["Curl the payment service health endpoint from the same environment to verify reachability.","Distinguish NetworkError (transport) from PaymentError (application) to decide retry vs. fix-request.","Configure sensible connect timeouts and abort signals so requests fail fast.","Check DNS/egress rules when deploying to new environments."],"tags":["network","fetch","payment","connectivity"],"backgroundTag":"network-fetch-failed","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}