Skip to content

Commit f9914f7

Browse files
authored
feat: Redact After Retry Attempts (#689)
1 parent d29acbf commit f9914f7

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

src/common.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,6 @@ export class GaxiosError<T = any> extends Error {
129129
if (error && 'code' in error && error.code) {
130130
this.code = error.code;
131131
}
132-
133-
if (config.errorRedactor) {
134-
config.errorRedactor({
135-
config: this.config,
136-
response: this.response,
137-
});
138-
}
139132
}
140133
}
141134

@@ -476,7 +469,7 @@ export function defaultErrorRedactor<
476469
}
477470

478471
function redactObject<T extends O['data'] | R>(obj: T | null) {
479-
if (!obj) {
472+
if (!obj || typeof obj !== 'object') {
480473
return;
481474
} else if (
482475
obj instanceof FormData ||

src/gaxios.ts

+4
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ export class Gaxios implements FetchCompliance {
229229
return this._request<T>(opts);
230230
}
231231

232+
if (opts.errorRedactor) {
233+
opts.errorRedactor(err);
234+
}
235+
232236
throw err;
233237
}
234238
}

test/test.getch.ts

+47
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,53 @@ describe('🎏 data handling', () => {
11281128
scope.done();
11291129
}
11301130
});
1131+
1132+
it('should redact after final retry', async () => {
1133+
const customURL = new URL(url);
1134+
customURL.searchParams.append('token', 'sensitive');
1135+
customURL.searchParams.append('client_secret', 'data');
1136+
customURL.searchParams.append('random', 'non-sensitive');
1137+
1138+
const data = {
1139+
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
1140+
assertion: 'somesensitivedata',
1141+
unrelated: 'data',
1142+
client_secret: 'data',
1143+
};
1144+
1145+
let retryAttempted = false;
1146+
const config: GaxiosOptions = {
1147+
url: customURL,
1148+
method: 'POST',
1149+
data: new URLSearchParams(data),
1150+
retry: true,
1151+
retryConfig: {
1152+
httpMethodsToRetry: ['POST'],
1153+
onRetryAttempt: err => {
1154+
assert.deepStrictEqual(err.config.data, new URLSearchParams(data));
1155+
retryAttempted = true;
1156+
},
1157+
},
1158+
};
1159+
1160+
const scope = nock(url)
1161+
.post('/', data)
1162+
.query(() => true)
1163+
.reply(500)
1164+
.post('/', data)
1165+
.query(() => true)
1166+
.reply(204);
1167+
1168+
const gaxios = new Gaxios();
1169+
1170+
try {
1171+
await gaxios.request(config);
1172+
1173+
assert(retryAttempted);
1174+
} finally {
1175+
scope.done();
1176+
}
1177+
});
11311178
});
11321179

11331180
describe('🍂 defaults & instances', () => {

0 commit comments

Comments
 (0)