Skip to content

Commit

Permalink
Improve network error logging messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
wparad committed Sep 30, 2024
1 parent d609d73 commit 3712280
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/httpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,34 @@ const errorMessages = new Set([
]);

function isNetworkError(error) {
return error && error.message && typeof error.message === 'string' && errorMessages.has(error.message)
|| error && error.data && typeof error.data === 'string' && errorMessages.has(error.data);
return error.message === 'Network Error' || error.code === 'ERR_NETWORK' || !error.status || error.status >= 500
|| typeof error.message === 'string' && errorMessages.has(error.message)
|| typeof error.data === 'string' && errorMessages.has(error.data);
}

async function retryExecutor(func) {
let lastError = null;
let lastNetworkError = null;
for (let iteration = 0; iteration < 5; iteration++) {
try {
const result = await func();
return result;
} catch (error) {
error.retryCount = iteration;
lastError = error;
if (isNetworkError(error) || error.message === 'Network Error' || error.code === 'ERR_NETWORK' || !error.status || error.status >= 500) {
lastError.isNetworkError = true;
await new Promise(resolve => setTimeout(resolve, 10 * 2 ** iteration));
continue;

if (!isNetworkError(error)) {
throw error;
}
throw error;

lastNetworkError = error;
lastNetworkError.isNetworkError = true;
await new Promise(resolve => setTimeout(resolve, 10 * 2 ** iteration));
continue;
}
}
throw lastError;

const customError = new Error('[Authress Login SDK] Http Request failed due to a Network Error even after multiple retries', { cause: lastNetworkError });
customError.code = 'AuthressSdkNetworkError';
throw customError;
}

class HttpClient {
Expand Down Expand Up @@ -90,7 +96,7 @@ class HttpClient {
const method = rawMethod.toUpperCase();
const headers = Object.assign({}, defaultHeaders, requestHeaders);
try {
this.logger && this.logger.debug && this.logger.debug({ title: 'HttpClient Request', method, url });
this.logger && this.logger.debug && this.logger.debug({ title: '[Authress Login SDK] HttpClient Request', method, url });
const request = { method, headers };
if (data) {
request.body = JSON.stringify(data);
Expand Down Expand Up @@ -129,22 +135,22 @@ class HttpClient {

const extensionErrorId = resolvedError.stack && resolvedError.stack.match(/chrome-extension:[/][/](\w+)[/]/);
if (extensionErrorId) {
this.logger && this.logger.debug && this.logger.debug({ title: `Fetch failed due to a browser extension - ${method} - ${url}`, method, url, data, headers, error, resolvedError, extensionErrorId });
this.logger && this.logger.debug && this.logger.debug({ title: `[Authress Login SDK] Fetch failed due to a browser extension - ${method} - ${url}`, method, url, data, headers, error, resolvedError, extensionErrorId });
const newError = new Error(`Extension Error ID: ${extensionErrorId}`);
newError.code = 'BROWSER_EXTENSION_ERROR';
throw newError;
}

const status = error.status;
let level = 'warn';
let message = 'HttpClient Response Error';
let message = '[Authress Login SDK] HttpClient Response Error';
if (!error) {
message = 'HttpClient Response Error - Unknown error occurred';
message = '[Authress Login SDK] HttpClient Response Error - Unknown error occurred';
} else if (status === 401) {
message = 'HttpClient Response Error due to invalid token';
message = '[Authress Login SDK] HttpClient Response Error due to invalid token';
level = 'debug';
} else if (status === 404) {
message = 'HttpClient Response: Not Found';
message = '[Authress Login SDK] HttpClient Response: Not Found';
level = 'debug';
} else if (status < 500 && ignoreExpectedWarnings) {
level = 'debug';
Expand Down

0 comments on commit 3712280

Please sign in to comment.