mirror of
https://github.com/DeBrosOfficial/network-ts-sdk.git
synced 2025-12-12 18:28:50 +00:00
Refactor HttpClient error logging and SQL query handling
- Improved logging for expected 404 responses in find-one calls, now logged as warnings instead of errors for better clarity. - Enhanced SQL query detail logging by refining the formatting of arguments in the console output. - Cleaned up code by removing unnecessary checks for expected 404 errors related to blocked users and conversation participants.
This commit is contained in:
parent
091a6d5751
commit
ca81e60bcb
@ -144,14 +144,14 @@ export class HttpClient {
|
|||||||
typeof options.body === "string"
|
typeof options.body === "string"
|
||||||
? JSON.parse(options.body)
|
? JSON.parse(options.body)
|
||||||
: options.body;
|
: options.body;
|
||||||
|
|
||||||
if (body.sql) {
|
if (body.sql) {
|
||||||
// Direct SQL query (query/exec endpoints)
|
// Direct SQL query (query/exec endpoints)
|
||||||
queryDetails = `SQL: ${body.sql}`;
|
queryDetails = `SQL: ${body.sql}`;
|
||||||
if (body.args && body.args.length > 0) {
|
if (body.args && body.args.length > 0) {
|
||||||
queryDetails += ` | Args: [${body.args.map((a: any) =>
|
queryDetails += ` | Args: [${body.args
|
||||||
typeof a === 'string' ? `"${a}"` : a
|
.map((a: any) => (typeof a === "string" ? `"${a}"` : a))
|
||||||
).join(', ')}]`;
|
.join(", ")}]`;
|
||||||
}
|
}
|
||||||
} else if (body.table) {
|
} else if (body.table) {
|
||||||
// Table-based query (find/find-one/select endpoints)
|
// Table-based query (find/find-one/select endpoints)
|
||||||
@ -189,7 +189,9 @@ export class HttpClient {
|
|||||||
);
|
);
|
||||||
const duration = performance.now() - startTime;
|
const duration = performance.now() - startTime;
|
||||||
if (typeof console !== "undefined") {
|
if (typeof console !== "undefined") {
|
||||||
const logMessage = `[HttpClient] ${method} ${path} completed in ${duration.toFixed(2)}ms`;
|
const logMessage = `[HttpClient] ${method} ${path} completed in ${duration.toFixed(
|
||||||
|
2
|
||||||
|
)}ms`;
|
||||||
if (queryDetails) {
|
if (queryDetails) {
|
||||||
console.log(logMessage);
|
console.log(logMessage);
|
||||||
console.log(`[HttpClient] ${queryDetails}`);
|
console.log(`[HttpClient] ${queryDetails}`);
|
||||||
@ -201,61 +203,24 @@ export class HttpClient {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
const duration = performance.now() - startTime;
|
const duration = performance.now() - startTime;
|
||||||
if (typeof console !== "undefined") {
|
if (typeof console !== "undefined") {
|
||||||
// Cache "key not found" (404 or error message) is expected behavior - don't log as error
|
// For 404 errors on find-one calls, log at warn level (not error) since "not found" is expected
|
||||||
const isCacheGetNotFound =
|
// Application layer handles these cases in try-catch blocks
|
||||||
path === "/v1/cache/get" &&
|
const is404FindOne =
|
||||||
error instanceof SDKError &&
|
|
||||||
(error.httpStatus === 404 ||
|
|
||||||
(error.httpStatus === 500 &&
|
|
||||||
error.message?.toLowerCase().includes("key not found")));
|
|
||||||
|
|
||||||
// "Not found" (404) for blocked_users is expected behavior - don't log as error
|
|
||||||
// This happens when checking if users are blocked (most users aren't blocked)
|
|
||||||
const isBlockedUsersNotFound =
|
|
||||||
path === "/v1/rqlite/find-one" &&
|
path === "/v1/rqlite/find-one" &&
|
||||||
error instanceof SDKError &&
|
error instanceof SDKError &&
|
||||||
error.httpStatus === 404 &&
|
error.httpStatus === 404;
|
||||||
options.body &&
|
|
||||||
(() => {
|
|
||||||
try {
|
|
||||||
const body =
|
|
||||||
typeof options.body === "string"
|
|
||||||
? JSON.parse(options.body)
|
|
||||||
: options.body;
|
|
||||||
return body.table === "blocked_users";
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
// "Not found" (404) for conversation_participants is expected behavior - don't log as error
|
if (is404FindOne) {
|
||||||
// This happens when checking if a user is a participant (e.g., on first group join)
|
// Log as warning for visibility, but not as error since it's expected behavior
|
||||||
const isConversationParticipantNotFound =
|
console.warn(
|
||||||
path === "/v1/rqlite/find-one" &&
|
`[HttpClient] ${method} ${path} returned 404 after ${duration.toFixed(
|
||||||
error instanceof SDKError &&
|
2
|
||||||
error.httpStatus === 404 &&
|
)}ms (expected for optional lookups)`
|
||||||
options.body &&
|
);
|
||||||
(() => {
|
|
||||||
try {
|
|
||||||
const body =
|
|
||||||
typeof options.body === "string"
|
|
||||||
? JSON.parse(options.body)
|
|
||||||
: options.body;
|
|
||||||
return body.table === "conversation_participants";
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
if (
|
|
||||||
isCacheGetNotFound ||
|
|
||||||
isBlockedUsersNotFound ||
|
|
||||||
isConversationParticipantNotFound
|
|
||||||
) {
|
|
||||||
// Log cache miss, non-blocked status, or non-participant status as debug/info, not error
|
|
||||||
// These are expected behaviors
|
|
||||||
} else {
|
} else {
|
||||||
const errorMessage = `[HttpClient] ${method} ${path} failed after ${duration.toFixed(2)}ms:`;
|
const errorMessage = `[HttpClient] ${method} ${path} failed after ${duration.toFixed(
|
||||||
|
2
|
||||||
|
)}ms:`;
|
||||||
console.error(errorMessage, error);
|
console.error(errorMessage, error);
|
||||||
if (queryDetails) {
|
if (queryDetails) {
|
||||||
console.error(`[HttpClient] ${queryDetails}`);
|
console.error(`[HttpClient] ${queryDetails}`);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user