Skip to content

Commit a0e7c42

Browse files
committed
refactor(debug): route API failure logs through the error namespace
1 parent ea881e6 commit a0e7c42

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

packages/cli/src/utils/debug.mts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function buildApiDebugDetails(
9898
requestInfo?: ApiRequestDebugInfo | undefined,
9999
): Record<string, unknown> {
100100
// `__proto__: null` keeps the payload free of prototype-chain keys
101-
// when callers iterate over `debugDir`'s output.
101+
// when callers iterate over the debug output.
102102
const details: Record<string, unknown> = { __proto__: null, ...base } as Record<string, unknown>
103103
if (!requestInfo) {
104104
return details
@@ -143,9 +143,9 @@ function buildApiDebugDetails(
143143
}
144144

145145
/**
146-
* Debug an API response. Only failed requests (error or status >= 400)
147-
* produce structured output; successful responses optionally log a
148-
* one-liner at the `notice` debug namespace.
146+
* Debug an API response. Failed requests (error or status >= 400) log
147+
* under the `error` namespace; successful responses optionally log a
148+
* one-liner under `notice`.
149149
*
150150
* Request and response headers are sanitized via `sanitizeHeaders` so
151151
* Authorization and `*api-key*` values are redacted.
@@ -157,7 +157,8 @@ export function debugApiResponse(
157157
requestInfo?: ApiRequestDebugInfo | undefined,
158158
): void {
159159
if (error) {
160-
debugDir(
160+
debugDirNs(
161+
'error',
161162
buildApiDebugDetails(
162163
{
163164
endpoint,
@@ -168,9 +169,9 @@ export function debugApiResponse(
168169
)
169170
} else if (status && status >= 400) {
170171
if (requestInfo) {
171-
debugDir(buildApiDebugDetails({ endpoint, status }, requestInfo))
172+
debugDirNs('error', buildApiDebugDetails({ endpoint, status }, requestInfo))
172173
} else {
173-
debug(`API ${endpoint}: HTTP ${status}`)
174+
debugNs('error', `API ${endpoint}: HTTP ${status}`)
174175
}
175176
/* c8 ignore next 3 */
176177
} else if (isDebugNs('notice')) {

packages/cli/test/unit/utils/debug.test.mts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,16 @@ describe('debug utilities', () => {
9797

9898
debugApiResponse('/api/test', undefined, error)
9999

100-
expect(debugDir).toHaveBeenCalledWith({
100+
expect(mockDebugDirNs).toHaveBeenCalledWith('error', {
101101
endpoint: '/api/test',
102102
error: 'API failed',
103103
})
104104
})
105105

106-
it('logs warning for HTTP error status codes', () => {
106+
it('logs under error namespace for HTTP error status codes', () => {
107107
debugApiResponse('/api/test', 404)
108108

109-
expect(debug).toHaveBeenCalledWith('API /api/test: HTTP 404')
109+
expect(debugNs).toHaveBeenCalledWith('error', 'API /api/test: HTTP 404')
110110
})
111111

112112
it('logs notice for successful responses when debug is enabled', () => {
@@ -128,7 +128,7 @@ describe('debug utilities', () => {
128128
it('handles non-Error objects in error parameter', () => {
129129
debugApiResponse('/api/test', undefined, 'String error')
130130

131-
expect(debugDir).toHaveBeenCalledWith({
131+
expect(mockDebugDirNs).toHaveBeenCalledWith('error', {
132132
endpoint: '/api/test',
133133
error: 'Unknown error',
134134
})
@@ -148,7 +148,7 @@ describe('debug utilities', () => {
148148

149149
debugApiResponse('/api/test', undefined, error, requestInfo)
150150

151-
const calledWith = mockDebugDir.mock.calls[0]?.[0]
151+
const calledWith = mockDebugDirNs.mock.calls[0]?.[1] as any
152152
expect(calledWith.method).toBe('POST')
153153
expect(calledWith.url).toBe('https://api.socket.dev/test')
154154
expect(calledWith.durationMs).toBe(1500)
@@ -169,7 +169,7 @@ describe('debug utilities', () => {
169169

170170
debugApiResponse('/api/resource', 500, undefined, requestInfo)
171171

172-
const calledWith = mockDebugDir.mock.calls[0]?.[0]
172+
const calledWith = mockDebugDirNs.mock.calls[0]?.[1] as any
173173
expect(calledWith.status).toBe(500)
174174
expect(calledWith.method).toBe('GET')
175175
// API key should be redacted.
@@ -183,7 +183,7 @@ describe('debug utilities', () => {
183183

184184
debugApiResponse('/api/update', 400, undefined, requestInfo)
185185

186-
const calledWith = mockDebugDir.mock.calls[0]?.[0]
186+
const calledWith = mockDebugDirNs.mock.calls[0]?.[1] as any
187187
expect(calledWith.method).toBe('PUT')
188188
expect(calledWith.url).toBeUndefined()
189189
expect(calledWith.headers).toBeUndefined()
@@ -198,7 +198,7 @@ describe('debug utilities', () => {
198198

199199
debugApiResponse('/api/x', 500, undefined, requestInfo)
200200

201-
const calledWith = mockDebugDir.mock.calls[0]?.[0]
201+
const calledWith = mockDebugDirNs.mock.calls[0]?.[1] as any
202202
expect(calledWith.requestedAt).toBe('2026-04-18T00:00:00.000Z')
203203
})
204204

@@ -214,7 +214,7 @@ describe('debug utilities', () => {
214214

215215
debugApiResponse('/api/y', 500, undefined, requestInfo)
216216

217-
const calledWith = mockDebugDir.mock.calls[0]?.[0]
217+
const calledWith = mockDebugDirNs.mock.calls[0]?.[1] as any
218218
expect(calledWith.cfRay).toBe('abc123-IAD')
219219
expect(calledWith.responseHeaders?.['cf-ray']).toBe('abc123-IAD')
220220
})
@@ -230,7 +230,7 @@ describe('debug utilities', () => {
230230

231231
debugApiResponse('/api/z', 500, undefined, requestInfo)
232232

233-
const calledWith = mockDebugDir.mock.calls[0]?.[0]
233+
const calledWith = mockDebugDirNs.mock.calls[0]?.[1] as any
234234
expect(calledWith.cfRay).toBe('xyz789-SJC')
235235
})
236236

@@ -243,7 +243,7 @@ describe('debug utilities', () => {
243243

244244
debugApiResponse('/api/body', 400, undefined, requestInfo)
245245

246-
const calledWith = mockDebugDir.mock.calls[0]?.[0]
246+
const calledWith = mockDebugDirNs.mock.calls[0]?.[1] as any
247247
expect(calledWith.responseBody).toBe('{"error":"bad"}')
248248
})
249249

@@ -257,7 +257,7 @@ describe('debug utilities', () => {
257257

258258
debugApiResponse('/api/big', 500, undefined, requestInfo)
259259

260-
const calledWith = mockDebugDir.mock.calls[0]?.[0]
260+
const calledWith = mockDebugDirNs.mock.calls[0]?.[1] as any
261261
expect(calledWith.responseBody).toMatch(/ \(truncated, 5000 chars\)$/)
262262
expect((calledWith.responseBody as string).length).toBeLessThan(
263263
bigBody.length,

0 commit comments

Comments
 (0)