Skip to content

Commit 113807b

Browse files
committed
wip
1 parent 524edc8 commit 113807b

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

packages/clerk-js/src/core/fapiClient.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { retry } from '@clerk/shared/retry';
33
import { camelToSnake } from '@clerk/shared/underscore';
44
import type { ClerkAPIErrorJSON, ClientJSON, InstanceType } from '@clerk/types';
55

6+
import { debugLogger } from '@/utils/debug';
7+
68
import { buildEmailAddress as buildEmailAddressUtil, buildURL as buildUrlUtil, stringifyQueryParams } from '../utils';
79
import { SUPPORTED_FAPI_VERSION } from './constants';
810
import { clerkNetworkError } from './errors';
@@ -250,12 +252,16 @@ export function createFapiClient(options: FapiClientOptions): FapiClient {
250252
response = new Response('{}', requestInit); // Mock an empty json response
251253
}
252254
} catch (e) {
255+
debugLogger.error('network error', { error: e, url: urlStr, method }, 'fapiClient');
253256
clerkNetworkError(urlStr, e);
254257
}
255258

256259
// 204 No Content responses do not have a body so we should not try to parse it
257260
const json: FapiResponseJSON<T> | null = response.status !== 204 ? await response.json() : null;
258261
const fapiResponse: FapiResponse<T> = Object.assign(response, { payload: json });
262+
if (!response.ok) {
263+
debugLogger.error('request failed', { method, path: requestInit.path, status: response.status }, 'fapiClient');
264+
}
259265
await runAfterResponseCallbacks(requestInit, fapiResponse);
260266
return fapiResponse;
261267
}

packages/clerk-js/src/utils/debug.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,57 @@ let isEnabled = false;
2525
let realLogger: DebugLoggerInterface | null = null;
2626
let lastOptions: Omit<InitOptions, 'enabled'> | null = null;
2727

28+
type BufferedLogEntry = {
29+
level: DebugLogLevel;
30+
message: string;
31+
context?: Record<string, unknown>;
32+
source?: string;
33+
ts: number;
34+
};
35+
36+
const MAX_BUFFERED_LOGS = 200;
37+
const preInitBuffer: BufferedLogEntry[] = [];
38+
39+
function pushBuffered(level: DebugLogLevel, message: string, context?: Record<string, unknown>, source?: string): void {
40+
preInitBuffer.push({ level, message, context, source, ts: Date.now() });
41+
if (preInitBuffer.length > MAX_BUFFERED_LOGS) {
42+
preInitBuffer.shift();
43+
}
44+
}
45+
46+
function flushBuffered(): void {
47+
if (!realLogger || preInitBuffer.length === 0) {
48+
return;
49+
}
50+
for (const entry of preInitBuffer) {
51+
const mergedContext = {
52+
...(entry.context || {}),
53+
__preInit: true,
54+
__preInitTs: entry.ts,
55+
} as Record<string, unknown>;
56+
switch (entry.level) {
57+
case 'error':
58+
realLogger.error(entry.message, mergedContext, entry.source);
59+
break;
60+
case 'warn':
61+
realLogger.warn(entry.message, mergedContext, entry.source);
62+
break;
63+
case 'info':
64+
realLogger.info(entry.message, mergedContext, entry.source);
65+
break;
66+
case 'debug':
67+
realLogger.debug(entry.message, mergedContext, entry.source);
68+
break;
69+
case 'trace':
70+
realLogger.trace(entry.message, mergedContext, entry.source);
71+
break;
72+
default:
73+
break;
74+
}
75+
}
76+
preInitBuffer.length = 0;
77+
}
78+
2879
async function ensureInitialized(): Promise<void> {
2980
try {
3081
if (!isEnabled || realLogger) {
@@ -40,6 +91,7 @@ async function ensureInitialized(): Promise<void> {
4091

4192
if (logger) {
4293
realLogger = logger;
94+
flushBuffered();
4395
}
4496
} catch (error) {
4597
const message = 'Debug logger initialization failed';
@@ -113,6 +165,7 @@ export function initDebugLogger(options: InitOptions = {}): void {
113165
});
114166
if (logger) {
115167
realLogger = logger;
168+
flushBuffered();
116169
}
117170
} catch (error) {
118171
try {
@@ -145,30 +198,35 @@ export function initDebugLogger(options: InitOptions = {}): void {
145198
const baseDebugLogger: DebugLoggerInterface = {
146199
debug(message: string, context?: Record<string, unknown>, source?: string): void {
147200
if (!realLogger) {
201+
pushBuffered('debug', message, context, source);
148202
return;
149203
}
150204
realLogger.debug(message, context, source);
151205
},
152206
error(message: string, context?: Record<string, unknown>, source?: string): void {
153207
if (!realLogger) {
208+
pushBuffered('error', message, context, source);
154209
return;
155210
}
156211
realLogger.error(message, context, source);
157212
},
158213
info(message: string, context?: Record<string, unknown>, source?: string): void {
159214
if (!realLogger) {
215+
pushBuffered('info', message, context, source);
160216
return;
161217
}
162218
realLogger.info(message, context, source);
163219
},
164220
trace(message: string, context?: Record<string, unknown>, source?: string): void {
165221
if (!realLogger) {
222+
pushBuffered('trace', message, context, source);
166223
return;
167224
}
168225
realLogger.trace(message, context, source);
169226
},
170227
warn(message: string, context?: Record<string, unknown>, source?: string): void {
171228
if (!realLogger) {
229+
pushBuffered('warn', message, context, source);
172230
return;
173231
}
174232
realLogger.warn(message, context, source);

0 commit comments

Comments
 (0)