apinow-sdk 0.12.1 → 0.12.2

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.
Files changed (2) hide show
  1. package/dist/index.js +44 -5
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -45,13 +45,52 @@ async function fetchJson(url, options) {
45
45
  else {
46
46
  console.error(`fetchJson (using node-fetch): Called with no options.`);
47
47
  }
48
- // @ts-ignore options might not perfectly match node-fetch's expected type if RequestInit from lib.dom.d.ts is too different
49
- const response = await fetch(url, options);
48
+ let response;
49
+ try {
50
+ // @ts-ignore options might not perfectly match node-fetch's expected type if RequestInit from lib.dom.d.ts is too different
51
+ response = await fetch(url, options);
52
+ }
53
+ catch (networkError) {
54
+ let requestBodySummary = "No body provided";
55
+ if (options?.body) {
56
+ if (typeof options.body === 'string') {
57
+ requestBodySummary = `String body (len ${options.body.length}): ${options.body.substring(0, 100)}...`;
58
+ }
59
+ else if (options.body instanceof Buffer) {
60
+ requestBodySummary = `Buffer body (len ${options.body.length})`;
61
+ }
62
+ else if (options.body instanceof Uint8Array) { // Should not happen with current txResponse
63
+ requestBodySummary = `Uint8Array body (len ${options.body.length})`;
64
+ }
65
+ else {
66
+ requestBodySummary = `Body of type ${options.body?.constructor?.name || 'unknown'}`;
67
+ }
68
+ }
69
+ const errorMessage = `Network/fetch error for URL: ${url}, Method: ${options?.method || 'GET'}, Request: ${requestBodySummary}. Original error: ${networkError instanceof Error ? networkError.message : String(networkError)}`;
70
+ console.error("fetchJson (using node-fetch): Fetch execution error - ", errorMessage);
71
+ throw new Error(errorMessage);
72
+ }
50
73
  console.error(`fetchJson (using node-fetch): Response status: ${response.status}, ok: ${response.ok}`);
51
74
  if (!response.ok) {
52
- const errorBody = await response.text();
53
- console.error(`fetchJson: Error response body: ${errorBody}`);
54
- throw new Error(`HTTP error ${response.status}: ${errorBody}`);
75
+ const errorBodyText = await response.text();
76
+ console.error(`fetchJson (using node-fetch): Error response body for ${url} (status ${response.status}): ${errorBodyText}`);
77
+ let requestBodySummary = "No body provided";
78
+ if (options?.body) {
79
+ if (typeof options.body === 'string') {
80
+ requestBodySummary = `String body (len ${options.body.length}): ${options.body.substring(0, 100)}...`;
81
+ }
82
+ else if (options.body instanceof Buffer) {
83
+ requestBodySummary = `Buffer body (len ${options.body.length})`;
84
+ }
85
+ else if (options.body instanceof Uint8Array) { // Should not happen with current txResponse
86
+ requestBodySummary = `Uint8Array body (len ${options.body.length})`;
87
+ }
88
+ else {
89
+ requestBodySummary = `Body of type ${options.body?.constructor?.name || 'unknown'}`;
90
+ }
91
+ }
92
+ const detailedErrorMessage = `HTTP error ${response.status} for URL: ${url}, Method: ${options?.method || 'GET'}, Request: ${requestBodySummary}. Response: ${errorBodyText}`;
93
+ throw new Error(detailedErrorMessage);
55
94
  }
56
95
  const responseData = await response.json();
57
96
  console.error(`fetchJson: Successfully fetched and parsed JSON (first 500 chars): ${JSON.stringify(responseData).substring(0, 500)}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apinow-sdk",
3
- "version": "0.12.1",
3
+ "version": "0.12.2",
4
4
  "description": "ApiNow SDK · The endpoint vending machine",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",