Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b452259

Browse files
fix: improve request handling and error reporting in executeEndpoint
1 parent d53fe8a commit b452259

File tree

1 file changed

+48
-37
lines changed

1 file changed

+48
-37
lines changed

‎src/server.js‎

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,47 @@ class CoCreateLazyLoader {
129129
headers = { ...headers, ...override.headers }; // Correct idea for merging
130130
}
131131

132-
let body = formatRequestBody(data[name]);
132+
// let body = formatRequestBody(data[name]);
133133

134-
let options = { method, headers, body, timeout };
134+
let formatType = data.formatType || "json";
135+
const timeout = 10000; // Set default timeout in ms (e.g., 10 seconds)
136+
let options = { method, headers, timeout };
135137

136-
const response = await makeHttpRequest(url, options);
137-
data[name] = parseResponse(response);
138+
// Only add body for methods that support it (not GET or HEAD)
139+
if (!["GET", "HEAD"].includes(method)) {
140+
let { body } = this.formatRequestBody(data[name], formatType);
141+
options.body = body;
142+
}
143+
// For GET/HEAD, do not create or send a body; all params should be in the URL
144+
145+
const response = await this.makeHttpRequest(url, options);
146+
147+
// If the response is not ok, makeHttpRequest will throw and be caught below.
148+
// If you want to include more info in the error, you can log or attach response details here.
149+
150+
data[name] = await response.json();
138151

139152
this.wsManager.send(data);
140153
} catch (error) {
154+
// Add more detail to the error for debugging 404s
141155
data.error = error.message;
156+
if (error.response) {
157+
data.status = error.response.status;
158+
data.statusText = error.response.statusText;
159+
data.responseData = error.response.data;
160+
}
142161
if (data.req) {
143162
data.res.writeHead(400, {
144-
"Content-Type": "text/plain"
163+
"Content-Type": "application/json"
145164
});
146-
data.res.end(`Lazyload Error: ${error.message}`);
165+
data.res.end(
166+
JSON.stringify({
167+
error: data.error,
168+
status: data.status,
169+
statusText: data.statusText,
170+
responseData: data.responseData
171+
})
172+
);
147173
}
148174
if (data.socket) {
149175
this.wsManager.send(data);
@@ -255,13 +281,12 @@ class CoCreateLazyLoader {
255281
* @throws {Error} If the request fails or returns a non-ok status.
256282
*/
257283
async makeHttpRequest(url, options) {
258-
if (!this.server.AbortController) {
259-
console.log("makeHttpRequest test");
260-
return {};
284+
let controller, timeoutId;
285+
if (this.server.AbortController) {
286+
controller = new this.server.AbortController();
287+
timeoutId = setTimeout(() => controller.abort(), options.timeout);
288+
options.signal = controller.signal;
261289
}
262-
const controller = new this.server.AbortController();
263-
const timeoutId = setTimeout(() => controller.abort(), options.timeout);
264-
options.signal = controller.signal;
265290

266291
// Remove Content-Type header if there's no body (relevant for GET, DELETE etc.)
267292
if (
@@ -272,46 +297,32 @@ class CoCreateLazyLoader {
272297
delete options.headers["Content-Type"];
273298
}
274299

300+
const fetchFn = this.server.fetch || global.fetch;
301+
if (typeof fetchFn !== "function") {
302+
throw new Error("No fetch implementation available.");
303+
}
304+
275305
try {
276-
const response = await this.server.fetch(url, options);
277-
clearTimeout(timeoutId);// Request finished, clear timeout
306+
const response = await fetchFn(url, options);
307+
if(timeoutId)clearTimeout(timeoutId);
278308

279309
if (!response.ok) {
280-
// status >= 200 && status < 300
310+
consttext=awaitresponse.text();
281311
const error = new Error(
282312
`HTTP error! Status: ${response.status} ${response.statusText}`
283313
);
284-
// Attach structured response info to the error
285314
error.response = {
286315
status: response.status,
287316
statusText: response.statusText,
288317
headers: Object.fromEntries(response.headers.entries()),
289-
data: parseResponse(response)// Include parsed error body
318+
data: text
290319
};
291320
throw error;
292321
}
293322
return response;
294323
} catch (error) {
295-
clearTimeout(timeoutId);
296-
if (error.name === "AbortError") {
297-
console.error(
298-
`Request timed out after ${options.timeout}ms: ${options.method} ${url}`
299-
);
300-
throw new Error(
301-
`Request Timeout: API call exceeded ${options.timeout}ms`
302-
);
303-
}
304-
305-
// If it already has response info (from !response.ok), rethrow it
306-
if (error.response) {
307-
throw error;
308-
}
309-
// Otherwise, wrap other errors (network, DNS, etc.)
310-
console.error(
311-
`Network/Request Error: ${options.method} ${url}`,
312-
error
313-
);
314-
throw new Error(`Network/Request Error: ${error.message}`);
324+
if (timeoutId) clearTimeout(timeoutId);
325+
throw error;
315326
}
316327
}
317328

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /