mirror of
https://github.com/DeBrosOfficial/network-ts-sdk.git
synced 2025-12-12 18:28:50 +00:00
Enhance HttpClient to log SQL query details for rqlite operations
- Added functionality to extract and log SQL query details when making requests to rqlite endpoints, including direct SQL queries and table-based queries. - Improved error handling for expected 404 responses, logging them as warnings instead of errors for better visibility. - Updated console logging to include query details when available, enhancing debugging capabilities.
This commit is contained in:
parent
c7ef91f8d6
commit
091a6d5751
@ -135,6 +135,51 @@ export class HttpClient {
|
|||||||
fetchOptions.body = JSON.stringify(options.body);
|
fetchOptions.body = JSON.stringify(options.body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Extract and log SQL query details for rqlite operations
|
||||||
|
const isRqliteOperation = path.includes("/v1/rqlite/");
|
||||||
|
let queryDetails: string | null = null;
|
||||||
|
if (isRqliteOperation && options.body) {
|
||||||
|
try {
|
||||||
|
const body =
|
||||||
|
typeof options.body === "string"
|
||||||
|
? JSON.parse(options.body)
|
||||||
|
: options.body;
|
||||||
|
|
||||||
|
if (body.sql) {
|
||||||
|
// Direct SQL query (query/exec endpoints)
|
||||||
|
queryDetails = `SQL: ${body.sql}`;
|
||||||
|
if (body.args && body.args.length > 0) {
|
||||||
|
queryDetails += ` | Args: [${body.args.map((a: any) =>
|
||||||
|
typeof a === 'string' ? `"${a}"` : a
|
||||||
|
).join(', ')}]`;
|
||||||
|
}
|
||||||
|
} else if (body.table) {
|
||||||
|
// Table-based query (find/find-one/select endpoints)
|
||||||
|
queryDetails = `Table: ${body.table}`;
|
||||||
|
if (body.criteria && Object.keys(body.criteria).length > 0) {
|
||||||
|
queryDetails += ` | Criteria: ${JSON.stringify(body.criteria)}`;
|
||||||
|
}
|
||||||
|
if (body.options) {
|
||||||
|
queryDetails += ` | Options: ${JSON.stringify(body.options)}`;
|
||||||
|
}
|
||||||
|
if (body.select) {
|
||||||
|
queryDetails += ` | Select: ${JSON.stringify(body.select)}`;
|
||||||
|
}
|
||||||
|
if (body.where) {
|
||||||
|
queryDetails += ` | Where: ${JSON.stringify(body.where)}`;
|
||||||
|
}
|
||||||
|
if (body.limit) {
|
||||||
|
queryDetails += ` | Limit: ${body.limit}`;
|
||||||
|
}
|
||||||
|
if (body.offset) {
|
||||||
|
queryDetails += ` | Offset: ${body.offset}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// Failed to parse body, ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await this.requestWithRetry(
|
const result = await this.requestWithRetry(
|
||||||
url.toString(),
|
url.toString(),
|
||||||
@ -144,9 +189,13 @@ export class HttpClient {
|
|||||||
);
|
);
|
||||||
const duration = performance.now() - startTime;
|
const duration = performance.now() - startTime;
|
||||||
if (typeof console !== "undefined") {
|
if (typeof console !== "undefined") {
|
||||||
console.log(
|
const logMessage = `[HttpClient] ${method} ${path} completed in ${duration.toFixed(2)}ms`;
|
||||||
`[HttpClient] ${method} ${path} completed in ${duration.toFixed(2)}ms`
|
if (queryDetails) {
|
||||||
);
|
console.log(logMessage);
|
||||||
|
console.log(`[HttpClient] ${queryDetails}`);
|
||||||
|
} else {
|
||||||
|
console.log(logMessage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -206,12 +255,11 @@ export class HttpClient {
|
|||||||
// Log cache miss, non-blocked status, or non-participant status as debug/info, not error
|
// Log cache miss, non-blocked status, or non-participant status as debug/info, not error
|
||||||
// These are expected behaviors
|
// These are expected behaviors
|
||||||
} else {
|
} else {
|
||||||
console.error(
|
const errorMessage = `[HttpClient] ${method} ${path} failed after ${duration.toFixed(2)}ms:`;
|
||||||
`[HttpClient] ${method} ${path} failed after ${duration.toFixed(
|
console.error(errorMessage, error);
|
||||||
2
|
if (queryDetails) {
|
||||||
)}ms:`,
|
console.error(`[HttpClient] ${queryDetails}`);
|
||||||
error
|
}
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user