Skip to content

Commit 0a96e69

Browse files
Fix debugging logs not including headers for CF API requests and responses (#10425)
--------- Co-authored-by: Carmen Popoviciu <[email protected]>
1 parent 34f3241 commit 0a96e69

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

.changeset/real-rabbits-enjoy.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Fix debugging logs not including headers for CF API requests and responses
6+
7+
Fix the fact that `wrangler`, when run with the `WRANGLER_LOG=DEBUG` and `WRANGLER_LOG_SANITIZE=false` environment variables, displays `{}` instead of the actual headers for requests and responses for CF API fetches

packages/wrangler/src/__tests__/helpers/assert-request.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ export function makeApiRequestAsserter(
2929
/HEADERS: (?<headers>(.|\n)*?)\nINIT: (?<init>(.|\n)*?)\n(BODY: (?<bodyMatch>(.|\n)*?)\n)?-- END CF API REQUEST/
3030
);
3131
const {
32-
headers: _headers,
32+
headers: headersStr,
3333
init: _init,
3434
bodyMatch,
3535
} = requestDetails?.groups ?? {};
3636

3737
if (body) {
3838
expect(bodyMatch).toMatch(body);
3939
}
40+
41+
const headers = JSON.parse(headersStr);
42+
expect(headers).toEqual({ "user-agent": "wrangler/x.x.x" });
4043
};
4144
}

packages/wrangler/src/cfetch/internal.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ export async function performApiFetch(
4040
`-- START CF API REQUEST: ${method} ${getCloudflareApiBaseUrl(complianceConfig)}${resource}`
4141
);
4242
logger.debugWithSanitization("QUERY STRING:", queryString);
43-
const logHeaders = cloneHeaders(headers);
44-
logHeaders.delete("Authorization");
45-
logger.debugWithSanitization("HEADERS:", JSON.stringify(logHeaders, null, 2));
43+
logHeaders(headers);
4644

4745
logger.debugWithSanitization("INIT:", JSON.stringify({ ...init }, null, 2));
4846
if (init.body instanceof FormData) {
@@ -65,6 +63,15 @@ export async function performApiFetch(
6563
);
6664
}
6765

66+
function logHeaders(headers: Headers) {
67+
headers = cloneHeaders(headers);
68+
headers.delete("Authorization");
69+
logger.debugWithSanitization(
70+
"HEADERS:",
71+
JSON.stringify(Object.fromEntries(headers), null, 2)
72+
);
73+
}
74+
6875
/**
6976
* Make a fetch request to the Cloudflare API.
7077
*
@@ -97,9 +104,7 @@ export async function fetchInternal<ResponseType>(
97104
response.statusText,
98105
response.status
99106
);
100-
const logHeaders = cloneHeaders(response.headers);
101-
logHeaders.delete("Authorization");
102-
logger.debugWithSanitization("HEADERS:", JSON.stringify(logHeaders, null, 2));
107+
logHeaders(response.headers);
103108
logger.debugWithSanitization("RESPONSE:", jsonText);
104109
logger.debug("-- END CF API RESPONSE");
105110

0 commit comments

Comments
 (0)