@saptools/cf-inspector 0.4.11 → 0.4.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -501,16 +501,39 @@ async function fetchJson(url, timeoutMs) {
501
501
  );
502
502
  });
503
503
  req.on("error", (err) => {
504
- reject(
505
- err instanceof CfInspectorError ? err : new CfInspectorError(
506
- "INSPECTOR_DISCOVERY_FAILED",
507
- `Inspector discovery at ${url} failed: ${err.message}`
508
- )
509
- );
504
+ reject(err instanceof CfInspectorError ? err : formatDiscoveryRequestError(url, err));
510
505
  });
511
506
  req.end();
512
507
  });
513
508
  }
509
+ function isNodeSystemError(err) {
510
+ return err instanceof Error;
511
+ }
512
+ function isConnectionRefusedOrUnreachable(code) {
513
+ return code === "ECONNREFUSED" || code === "ECONNRESET" || code === "ETIMEDOUT" || code === "EHOSTUNREACH" || code === "ENETUNREACH";
514
+ }
515
+ function formatEndpoint(url, err) {
516
+ if (typeof err.address === "string" && typeof err.port === "number") {
517
+ return `${err.address}:${err.port.toString()}`;
518
+ }
519
+ const parsed = new URL(url);
520
+ return parsed.host;
521
+ }
522
+ function formatDiscoveryRequestError(url, err) {
523
+ const detail = err instanceof Error ? err.message : String(err);
524
+ if (!isNodeSystemError(err) || !isConnectionRefusedOrUnreachable(err.code)) {
525
+ return new CfInspectorError(
526
+ "INSPECTOR_DISCOVERY_FAILED",
527
+ `Inspector discovery at ${url} failed: ${detail}`
528
+ );
529
+ }
530
+ const endpoint = formatEndpoint(url, err);
531
+ return new CfInspectorError(
532
+ "INSPECTOR_DISCOVERY_FAILED",
533
+ `Cannot reach Node inspector discovery at ${url}. Nothing is listening on ${endpoint}, or the inspector tunnel is stale/closed. Restart the local inspector or tunnel and retry. If this port came from cf-debugger, stop the stale session and start a fresh tunnel, or run cf-inspector with --app/--region/--org/--space so it can open a tunnel.`,
534
+ detail
535
+ );
536
+ }
514
537
  function parseJsonResponse(chunks) {
515
538
  const text = Buffer.concat(chunks).toString("utf8");
516
539
  return JSON.parse(text);