Skip to content
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
11 changes: 9 additions & 2 deletions clients/algoliasearch-client-javascript/utils/Transporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { RetryError } from './errors';
import * as responseUtils from './Response';
import { Requester } from './requester/Requester';
import { HttpRequester } from './requester/HttpRequester';
import { stackTraceWithoutCredentials, stackFrameWithoutCredentials } from './stackTrace';

export class Transporter {
private hosts: Host[];
Expand Down Expand Up @@ -143,7 +144,7 @@ export class Transporter {
*/
const host = hosts.pop();
if (host === undefined) {
throw new RetryError(stackTrace);
throw new RetryError(stackTraceWithoutCredentials(stackTrace));
}

let responseTimeout = requestOptions.timeout;
Expand Down Expand Up @@ -181,12 +182,18 @@ export class Transporter {
const response = await this.requester.send(payload, request);

if (responseUtils.isRetryable(response)) {
pushToStackTrace(response);
const stackFrame = pushToStackTrace(response);

// If response is a timeout, we increase the number of timeouts so we can increase the timeout later.
if (response.isTimedOut) {
timeoutsCount++;
}
/**
* Failures are individually sent to the logger, allowing
* the end user to debug / store stack frames even
* when a retry error does not happen.
*/
console.log('Retryable failure', stackFrameWithoutCredentials(stackFrame));

/**
* We also store the state of the host in failure cases. If the host, is
Expand Down
22 changes: 22 additions & 0 deletions clients/algoliasearch-client-javascript/utils/stackTrace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { StackFrame } from './types';

export function stackTraceWithoutCredentials(stackTrace: StackFrame[]): StackFrame[] {
return stackTrace.map((stackFrame) => stackFrameWithoutCredentials(stackFrame));
}

export function stackFrameWithoutCredentials(stackFrame: StackFrame): StackFrame {
const modifiedHeaders: Record<string, string> = stackFrame.request.headers['x-algolia-api-key']
? { 'x-algolia-api-key': '*****' }
: {};

return {
...stackFrame,
request: {
...stackFrame.request,
headers: {
...stackFrame.request.headers,
...modifiedHeaders,
},
},
};
}
11 changes: 9 additions & 2 deletions clients/utils/javascript/Transporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { RetryError } from './errors';
import * as responseUtils from './Response';
import { Requester } from './requester/Requester';
import { HttpRequester } from './requester/HttpRequester';
import { stackTraceWithoutCredentials, stackFrameWithoutCredentials } from './stackTrace';

export class Transporter {
private hosts: Host[];
Expand Down Expand Up @@ -143,7 +144,7 @@ export class Transporter {
*/
const host = hosts.pop();
if (host === undefined) {
throw new RetryError(stackTrace);
throw new RetryError(stackTraceWithoutCredentials(stackTrace));
}

let responseTimeout = requestOptions.timeout;
Expand Down Expand Up @@ -181,12 +182,18 @@ export class Transporter {
const response = await this.requester.send(payload, request);

if (responseUtils.isRetryable(response)) {
pushToStackTrace(response);
const stackFrame = pushToStackTrace(response);

// If response is a timeout, we increase the number of timeouts so we can increase the timeout later.
if (response.isTimedOut) {
timeoutsCount++;
}
/**
* Failures are individually send the logger, allowing
* the end user to debug / store stack frames even
* when a retry error does not happen.
*/
console.log('Retryable failure', stackFrameWithoutCredentials(stackFrame));
Copy link
Contributor

Choose a reason for hiding this comment

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

For now this is probably fine, but we might want to extract this into a Logger class that the user can swap for something custom (e.g. writing to a file or server instead of to the console).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good idea, I created a ticket


/**
* We also store the state of the host in failure cases. If the host, is
Expand Down
22 changes: 22 additions & 0 deletions clients/utils/javascript/stackTrace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { StackFrame } from './types';

export function stackTraceWithoutCredentials(stackTrace: StackFrame[]): StackFrame[] {
return stackTrace.map((stackFrame) => stackFrameWithoutCredentials(stackFrame));
}

export function stackFrameWithoutCredentials(stackFrame: StackFrame): StackFrame {
const modifiedHeaders: Record<string, string> = stackFrame.request.headers['x-algolia-api-key']
? { 'x-algolia-api-key': '*****' }
: {};

return {
...stackFrame,
request: {
...stackFrame.request,
headers: {
...stackFrame.request.headers,
...modifiedHeaders,
},
},
};
}