Skip to content

src: replace Promise chains with async/await #2053

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

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
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
57 changes: 30 additions & 27 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,36 +136,39 @@ 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) {
if (err instanceof ApiException) {
throw err;
}

throw new ApiException<undefined>(500, 'Error occurred in log request', undefined, {});
}

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