Skip to content

Commit 4489036

Browse files
committed
add headers printing
1 parent 4fc08c2 commit 4489036

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/common/errors.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,30 @@ export class ApiError extends Error {
2020
code: string;
2121
raw?: any;
2222
url: string;
23+
headers?: Record<string, string>;
2324

2425
constructor({
2526
message,
2627
status,
2728
code,
2829
raw,
29-
url
30+
url,
31+
headers
3032
}: {
3133
message: string;
3234
status: number;
3335
code: string;
3436
url: string;
3537
raw?: any;
38+
headers?: Record<string, string>;
3639
}) {
3740
const formattedMessage = `
3841
Request failed
3942
URL: ${url}
4043
Reason: ${message}
4144
Response status: ${status}
4245
${code ? `Code: ${code}` : ""}
46+
${headers ? `Headers: ${JSON.stringify(headers, null, 2)}` : ""}
4347
${raw ? `Response: ${JSON.stringify(raw, null, 2)}` : ""}
4448
`;
4549
super(formattedMessage);
@@ -48,6 +52,7 @@ ${raw ? `Response: ${JSON.stringify(raw, null, 2)}` : ""}
4852
this.code = code;
4953
this.raw = raw;
5054
this.url = url;
55+
this.headers = headers;
5156
}
5257
}
5358

src/http/node.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,18 @@ export class NodeHttpClient {
9292
const withProtocol = assignProtocol(url);
9393

9494
const userAgent = headers["user-agent"] || DEFAULT_USER_AGENT;
95+
const finalHeaders = {
96+
"user-agent": userAgent,
97+
"Content-Type": "application/json",
98+
[PROTOCOL_VERSION_HEADER]: PROTOCOL_VERSION,
99+
...headers
100+
};
101+
95102
const response = await fetch(withProtocol, {
96103
agent,
97104
signal: controller.signal as AbortSignal,
98105
method,
99-
headers: {
100-
"user-agent": userAgent,
101-
"Content-Type": "application/json",
102-
[PROTOCOL_VERSION_HEADER]: PROTOCOL_VERSION,
103-
...headers
104-
},
106+
headers: finalHeaders,
105107
body
106108
});
107109

@@ -147,7 +149,8 @@ export class NodeHttpClient {
147149
code,
148150
status: response.status,
149151
raw: json,
150-
url
152+
url,
153+
headers: finalHeaders
151154
});
152155
} else {
153156
const text = await response.text();
@@ -156,7 +159,8 @@ export class NodeHttpClient {
156159
message,
157160
code: "",
158161
status: response.status,
159-
url
162+
url,
163+
headers: finalHeaders
160164
});
161165
}
162166
}

0 commit comments

Comments
 (0)