Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
53 changes: 26 additions & 27 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,36 +136,35 @@ export class Log {
requestInit.signal = controller.signal as AbortSignal;
requestInit.method = 'GET';

await fetch(requestURL.toString(), requestInit)
.then((response) => {
const status = response.status;
if (status === 200) {
// TODO: the follow search param still has the stream close prematurely based on my testing
response.body.pipe(stream);
} else if (status === 500) {
const v1status = response.body as V1Status;
const v1code = v1status.code;
const v1message = v1status.message;
if (v1code !== undefined && v1message !== undefined) {
throw new ApiException<undefined | V1Status>(
v1code,
v1message,
v1status,
normalizeResponseHeaders(response),
);
}
} else {
throw new ApiException<undefined>(
status,
'Error occurred in log request',
undefined,
try {
const response = await fetch(requestURL.toString(), requestInit);
const status = response.status;
if (status === 200) {
// TODO: the follow search param still has the stream close prematurely based on my testing
response.body.pipe(stream);
} else if (status === 500) {
const v1status = response.body as V1Status;
const v1code = v1status.code;
const v1message = v1status.message;
if (v1code !== undefined && v1message !== undefined) {
throw new ApiException<undefined | V1Status>(
v1code,
v1message,
v1status,
normalizeResponseHeaders(response),
);
}
})
.catch((err) => {
throw new ApiException<undefined>(err, 'Error occurred in log request', undefined, err);
});
} else {
throw new ApiException<undefined>(
status,
'Error occurred in log request',
undefined,
normalizeResponseHeaders(response),
);
}
} catch (err: any) {
throw new ApiException<undefined>(err, 'Error occurred in log request', undefined, err);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note - this line is ported from the previous catch() call, but I don't think passing err as the first parameter is actually correct. catch() typed err as any. When I changed to a try...catch, that typing was not there and TypeScript complained that the first argument should be a numeric code (probably similar to how src/metrics.ts does it).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm on board with switching this to match metric.ts

}

return controller;
}
Expand Down
77 changes: 39 additions & 38 deletions src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,44 +88,45 @@ export class Metrics {
const requestInit = await this.config.applyToFetchOptions({});
requestInit.method = 'GET';

return fetch(requestURL, requestInit)
.then((response) => {
return Promise.all([response.json(), response.status, response]);
})
.then(([json, status, response]) => {
if (status === 200) {
return json as T;
try {
const response = await fetch(requestURL, requestInit);
const json = await response.json();
const { status } = response;

if (status === 200) {
return json as T;
}

if (status === 500) {
const v1status = json as V1Status;
const v1code = v1status.code;
const v1message = v1status.message;
if (v1code !== undefined && v1message !== undefined) {
throw new ApiException<undefined | V1Status>(
v1code,
v1message,
v1status,
normalizeResponseHeaders(response),
);
}
if (status === 500) {
const v1status = json as V1Status;
const v1code = v1status.code;
const v1message = v1status.message;
if (v1code !== undefined && v1message !== undefined) {
throw new ApiException<undefined | V1Status>(
v1code,
v1message,
v1status,
normalizeResponseHeaders(response),
);
}
}
throw new ApiException<undefined>(
status,
'Error occurred in metrics request',
undefined,
normalizeResponseHeaders(response),
);
})
.catch((e) => {
if (e instanceof ApiException) {
throw e;
}
throw new ApiException<undefined | V1Status>(
500,
`Error occurred in metrics request: ${e.message}`,
{},
{},
);
});
}

throw new ApiException<undefined>(
status,
'Error occurred in metrics request',
undefined,
normalizeResponseHeaders(response),
);
} catch (e: any) {
if (e instanceof ApiException) {
throw e;
}
throw new ApiException<undefined | V1Status>(
500,
`Error occurred in metrics request: ${e.message}`,
{},
{},
);
}
}
}
56 changes: 29 additions & 27 deletions src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,36 @@ export class Watch {
}
};

await fetch(watchURL, requestInit)
.then((response) => {
if (response.status === 200) {
response.body.on('error', doneCallOnce);
response.body.on('close', () => doneCallOnce(null));
response.body.on('finish', () => doneCallOnce(null));
try {
const response = await fetch(watchURL, requestInit);

const lines = createInterface(response.body);
lines.on('error', doneCallOnce);
lines.on('close', () => doneCallOnce(null));
lines.on('finish', () => doneCallOnce(null));
lines.on('line', (line) => {
try {
const data = JSON.parse(line.toString());
callback(data.type, data.object, data);
} catch (ignore) {
// ignore parse errors
}
});
} else {
const error = new Error(response.statusText) as Error & {
statusCode: number | undefined;
};
error.statusCode = response.status;
throw error;
}
})
.catch(doneCallOnce);
if (response.status === 200) {
response.body.on('error', doneCallOnce);
response.body.on('close', () => doneCallOnce(null));
response.body.on('finish', () => doneCallOnce(null));

const lines = createInterface(response.body);
lines.on('error', doneCallOnce);
lines.on('close', () => doneCallOnce(null));
lines.on('finish', () => doneCallOnce(null));
lines.on('line', (line) => {
try {
const data = JSON.parse(line.toString());
callback(data.type, data.object, data);
} catch (ignore) {
// ignore parse errors
}
});
} else {
const error = new Error(response.statusText) as Error & {
statusCode: number | undefined;
};
error.statusCode = response.status;
throw error;
}
} catch (err) {
doneCallOnce(err);
}

return controller;
}
Expand Down
Loading