-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
utils.ts
24 lines (22 loc) 路 1011 Bytes
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Use require instead of import for default exports, to be compatible with variants of esModuleInterop in tsconfig.
import stringifyStream = require('stream-to-string');
import { ActorHttp } from './ActorHttp';
/**
* Check if the http response is valid, and throw an error if not.
* @param url The original URL that was to be updated.
* @param httpResponse The update response.
*/
export async function validateAndCloseHttpResponse(url: string, httpResponse: Response): Promise<void> {
// Check if update was successful
if (httpResponse.status >= 400) {
// Consume the body, to avoid process to hang
let bodyString = 'empty response';
if (httpResponse.body) {
const responseStream = ActorHttp.toNodeReadable(httpResponse.body);
bodyString = await stringifyStream(responseStream);
}
throw new Error(`Could not update ${url} (HTTP status ${httpResponse.status}):\n${bodyString}`);
}
// Close response body, as we don't need it
await httpResponse.body?.cancel();
}