Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log request id on timeout #90

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Substrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ export class Substrate {
const { signal } = abortController;
const timeout = setTimeout(() => abortController.abort(), this.timeout);

const request = new Request(url, this.requestOptions(req, signal));
const requestId = request.headers.get("x-substrate-request-id");
try {
const request = new Request(url, this.requestOptions(req, signal));
const apiResponse = await fetch(request);

if (apiResponse.ok) {
Expand All @@ -120,7 +121,6 @@ export class Substrate {

return res;
} else {
const requestId = request.headers.get("x-substrate-request-id");
throw new SubstrateError(
`[Request failed] status=${apiResponse.status} statusText=${apiResponse.statusText} requestId=${requestId}`,
);
Expand All @@ -129,7 +129,7 @@ export class Substrate {
if (err instanceof Error) {
if (err.name === "AbortError") {
throw new RequestTimeoutError(
`Request timed out after ${this.timeout}ms`,
`Request timed out after ${this.timeout}ms requestId=${requestId}`,
);
// TODO: We could propagate timeout errors to nodes too, but I'm
// not sure yet what might be easier for the user to manage.
Expand All @@ -156,8 +156,9 @@ export class Substrate {
requestOptions.headers.set("Accept", "text/event-stream");
requestOptions.headers.set("X-Substrate-Streaming", "1");

const request = new Request(url, requestOptions);
const requestId = request.headers.get("x-substrate-request-id");
try {
const request = new Request(url, requestOptions);
const response = await fetch(request);
return await SubstrateStreamingResponse.fromRequestReponse(
request,
Expand All @@ -167,7 +168,7 @@ export class Substrate {
if (err instanceof Error) {
if (err.name === "AbortError") {
throw new RequestTimeoutError(
`Request timed out after ${this.timeout}ms`,
`Request timed out after ${this.timeout}ms requestId=${requestId}`,
);
// TODO: We could propagate timeout errors to nodes too, but I'm
// not sure yet what might be easier for the user to manage.
Expand Down
Loading