pi-smart-fetch 0.3.4 → 0.3.5

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
@@ -10087,42 +10087,54 @@ function buildUserFacingFetchErrorSummary(error) {
10087
10087
  return "No readable content could be extracted from the page.";
10088
10088
  case "processing_error":
10089
10089
  return "The response could not be processed.";
10090
- case "network_error":
10090
+ case "network_error": {
10091
+ if (/dns error/i.test(error.error)) {
10092
+ return "DNS error \u2014 could not resolve the hostname.";
10093
+ }
10094
+ if (/connection failed|connection refused|unreachable/i.test(error.error)) {
10095
+ return "Connection failed \u2014 the server is unreachable.";
10096
+ }
10097
+ if (/tls|ssl/i.test(error.error)) {
10098
+ return "TLS/SSL error \u2014 certificate may be invalid.";
10099
+ }
10091
10100
  return "The request failed before a usable response was returned.";
10101
+ }
10092
10102
  default:
10093
10103
  return error.error;
10094
10104
  }
10095
10105
  }
10096
10106
  function buildFetchErrorResponseText(error) {
10097
10107
  const lines = [`Error: ${error.error}`];
10098
- const metadata = buildHeader([
10099
- ["URL", error.url],
10100
- ["Final URL", error.finalUrl],
10101
- ["Phase", error.phase ? describeErrorPhase(error.phase) : void 0],
10102
- [
10103
- "Timeout",
10104
- error.timeoutMs ? formatDurationMs(error.timeoutMs) : void 0
10105
- ],
10106
- [
10107
- "HTTP status",
10108
- error.statusCode ? `${error.statusCode}${error.statusText ? ` ${error.statusText}` : ""}` : void 0
10109
- ],
10110
- ["Mime type", error.mimeType],
10111
- [
10112
- "Content-Length",
10113
- error.contentLength !== void 0 ? `${error.contentLength} bytes (${formatByteCount(error.contentLength)})` : void 0
10114
- ],
10115
- [
10116
- "Downloaded before failure",
10117
- error.downloadedBytes !== void 0 ? `${error.downloadedBytes} bytes (${formatByteCount(error.downloadedBytes)})` : void 0
10118
- ],
10119
- [
10120
- "Suggested timeoutMs",
10121
- error.code === "timeout" ? suggestRetryTimeoutMs(error) : void 0
10122
- ]
10123
- ]);
10124
- if (metadata) {
10125
- lines.push("", metadata);
10108
+ if (error.code === "timeout" || error.code === "http_error" || error.code === "download_error") {
10109
+ const metadata = buildHeader([
10110
+ ["URL", error.url],
10111
+ ["Final URL", error.finalUrl],
10112
+ ["Phase", error.phase ? describeErrorPhase(error.phase) : void 0],
10113
+ [
10114
+ "Timeout",
10115
+ error.timeoutMs ? formatDurationMs(error.timeoutMs) : void 0
10116
+ ],
10117
+ [
10118
+ "HTTP status",
10119
+ error.statusCode ? `${error.statusCode}${error.statusText ? ` ${error.statusText}` : ""}` : void 0
10120
+ ],
10121
+ ["Mime type", error.mimeType],
10122
+ [
10123
+ "Content-Length",
10124
+ error.contentLength !== void 0 ? `${error.contentLength} bytes (${formatByteCount(error.contentLength)})` : void 0
10125
+ ],
10126
+ [
10127
+ "Downloaded before failure",
10128
+ error.downloadedBytes !== void 0 ? `${error.downloadedBytes} bytes (${formatByteCount(error.downloadedBytes)})` : void 0
10129
+ ],
10130
+ [
10131
+ "Suggested timeoutMs",
10132
+ error.code === "timeout" ? suggestRetryTimeoutMs(error) : void 0
10133
+ ]
10134
+ ]);
10135
+ if (metadata) {
10136
+ lines.push("", metadata);
10137
+ }
10126
10138
  }
10127
10139
  if (error.code === "timeout") {
10128
10140
  lines.push(
@@ -10656,6 +10668,31 @@ function isTimeoutError(error) {
10656
10668
  const message = error instanceof Error ? error.message : String(error);
10657
10669
  return /timed out|timeout|deadline exceeded|abort(?:ed)?/i.test(message);
10658
10670
  }
10671
+ function isDnsError(error) {
10672
+ const message = error instanceof Error ? error.message : String(error);
10673
+ return /dns error|failed to lookup address|nodename nor servname provided|name resolution failed/i.test(
10674
+ message
10675
+ );
10676
+ }
10677
+ function isConnectError(error) {
10678
+ const message = error instanceof Error ? error.message : String(error);
10679
+ return /client error \(connect\)|connection refused|tcp connect error|connection reset|network unreachable|no route to host/i.test(
10680
+ message
10681
+ );
10682
+ }
10683
+ function isTlsError(error) {
10684
+ const message = error instanceof Error ? error.message : String(error);
10685
+ return /ssl.*error|tls.*error|bad certificate|certificate.*invalid|unknown.*issuer/i.test(
10686
+ message
10687
+ );
10688
+ }
10689
+ function extractHostname(url) {
10690
+ try {
10691
+ return new URL(url).hostname;
10692
+ } catch {
10693
+ return url;
10694
+ }
10695
+ }
10659
10696
  function buildTimeoutError(context) {
10660
10697
  const targetUrl = context.finalUrl ?? context.url;
10661
10698
  const timeoutLabel = `${context.timeoutMs}ms`;
@@ -10733,14 +10770,47 @@ function buildThrownFetchError(error, context) {
10733
10770
  if (isTimeoutError(error)) {
10734
10771
  return buildTimeoutError(context);
10735
10772
  }
10773
+ const hostname = extractHostname(context.url);
10774
+ if (isDnsError(error)) {
10775
+ return {
10776
+ error: `DNS error: failed to lookup address for ${hostname}. Check the URL and try again.`,
10777
+ code: "network_error",
10778
+ phase: "connecting",
10779
+ retryable: false,
10780
+ url: context.url,
10781
+ finalUrl: context.finalUrl
10782
+ };
10783
+ }
10784
+ if (isConnectError(error)) {
10785
+ return {
10786
+ error: `Connection failed to ${hostname}. The server may be unreachable or blocking requests.`,
10787
+ code: "network_error",
10788
+ phase: "connecting",
10789
+ retryable: true,
10790
+ url: context.url,
10791
+ finalUrl: context.finalUrl
10792
+ };
10793
+ }
10794
+ if (isTlsError(error)) {
10795
+ return {
10796
+ error: `TLS/SSL error connecting to ${hostname}. The server's certificate may be invalid.`,
10797
+ code: "network_error",
10798
+ phase: "connecting",
10799
+ retryable: false,
10800
+ url: context.url,
10801
+ finalUrl: context.finalUrl
10802
+ };
10803
+ }
10804
+ const isConnectLevel = isDnsError(error) || isConnectError(error) || isTlsError(error);
10805
+ const effectivePhase = isConnectLevel ? "connecting" : context.phase;
10736
10806
  const message = error instanceof Error ? error.message : String(error);
10737
10807
  const targetUrl = context.finalUrl ?? context.url;
10738
- const phaseDescription = context.phase === "loading" ? "downloading the response" : context.phase === "waiting" ? "waiting for the server response" : context.phase === "connecting" ? "connecting" : "fetching";
10808
+ const phaseDescription = effectivePhase === "loading" ? "downloading the response" : effectivePhase === "waiting" ? "waiting for the server response" : effectivePhase === "connecting" ? "connecting" : "fetching";
10739
10809
  return {
10740
- error: context.phase === "processing" ? `Failed while processing the response from ${targetUrl}: ${message}` : `Request failed while ${phaseDescription} for ${targetUrl}: ${message}`,
10741
- code: context.phase === "processing" ? "processing_error" : context.phase === "loading" && context.mimeType ? "download_error" : "network_error",
10742
- phase: context.phase,
10743
- retryable: context.phase !== "processing",
10810
+ error: effectivePhase === "processing" ? `Failed while processing the response from ${targetUrl}: ${message}` : `Request failed while ${phaseDescription} for ${targetUrl}: ${message}`,
10811
+ code: effectivePhase === "processing" ? "processing_error" : effectivePhase === "loading" && context.mimeType ? "download_error" : "network_error",
10812
+ phase: effectivePhase,
10813
+ retryable: effectivePhase !== "processing",
10744
10814
  timeoutMs: context.timeoutMs,
10745
10815
  url: context.url,
10746
10816
  finalUrl: context.finalUrl,