Skip to content

Commit

Permalink
common: better handling of error responses in http
Browse files Browse the repository at this point in the history
  • Loading branch information
josephjclark committed Jan 19, 2024
1 parent 4be3594 commit 8f27946
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions packages/common/src/util/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ export const makeBasicAuthHeader = (username, password) => {
export const logResponse = response => {
const { method, url, statusCode, duration } = response;
if (method && url && duration && statusCode) {
console.log(`${method} ${url} - ${statusCode} in ${duration}ms`);
const message = `${method} ${url} - ${statusCode} in ${duration}ms`;
if (response instanceof Error) {
console.error(message);
console.error('response body: ');
console.error(response.body || '[no body]');
} else {
console.log(message);
}
}
};

const getClient = (baseUrl, options) => {
const { tls, timeout } = options;
const { tls } = options;
if (!clients.has(baseUrl)) {
clients.set(
baseUrl,
new Client(baseUrl, { bodyTimeout: timeout, connect: tls })
);
clients.set(baseUrl, new Client(baseUrl, { connect: tls }));
}
return clients.get(baseUrl);
};
Expand All @@ -37,7 +41,7 @@ export const enableMockClient = baseUrl => {
return client;
};

const assertOK = (response, errorMap, fullUrl, method, startTime) => {
const assertOK = async (response, errorMap, fullUrl, method, startTime) => {
const errMapMessage = errorMap[response.statusCode];

const isError =
Expand All @@ -46,6 +50,8 @@ const assertOK = (response, errorMap, fullUrl, method, startTime) => {
: errMapMessage || response.statusCode >= 400;

if (isError) {
const body = await readResponseBody(response);

const statusText = getReasonPhrase(response.statusCode);
const defaultErrorMesssage = `${method} to ${fullUrl} returned ${response.statusCode}: ${statusText}`;

Expand All @@ -62,6 +68,8 @@ const assertOK = (response, errorMap, fullUrl, method, startTime) => {
error.url = fullUrl;
error.duration = duration;
error.method = method;
error.body = body;
error.headers = response.headers;
throw error;
}
};
Expand Down Expand Up @@ -124,7 +132,7 @@ export async function request(method, fullUrlOrPath, options = {}) {
maxRedirections,
} = options;

const client = getClient(baseUrl, { tls, timeout });
const client = getClient(baseUrl, { tls });

const response = await client.request({
path,
Expand All @@ -134,11 +142,13 @@ export async function request(method, fullUrlOrPath, options = {}) {
body: encodeRequestBody(body),
throwOnError: false,
maxRedirections,
bodyTimeout: timeout,
headersTimeout: timeout,
});

const statusText = getReasonPhrase(response.statusCode);

assertOK(response, errors, url, method, startTime);
await assertOK(response, errors, url, method, startTime);

const responseBody = await readResponseBody(response, parseAs);
const endTime = Date.now();
Expand Down

0 comments on commit 8f27946

Please sign in to comment.