Skip to content

Commit 45007b1

Browse files
committed
fix(errors): add more properties to "DownloadError"
To track max retries, attempted tries and actual machine-checkable error code. re #929
1 parent 1487290 commit 45007b1

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

packages/mongodb-memory-server-core/src/util/MongoBinaryDownload.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,15 @@ export class MongoBinaryDownload {
415415
} catch (error: any) {
416416
const shouldRetry =
417417
(error instanceof DownloadError &&
418-
retryableStatusCodes.some((code) => error.message.includes(code.toString()))) ||
418+
retryableStatusCodes.some((code) => error?.code === `Status: ${code}`)) ||
419419
(error?.code && retryableErrorCodes.includes(error.code));
420420

421421
if (!shouldRetry || attempt === retries) {
422+
if (error instanceof DownloadError) {
423+
error.maxRetries = retries;
424+
error.attempts = attempt;
425+
}
426+
422427
throw error;
423428
}
424429

@@ -432,7 +437,11 @@ export class MongoBinaryDownload {
432437
}
433438
}
434439

435-
throw new DownloadError(downloadUrl, 'Max retries exceeded');
440+
const error = new DownloadError(downloadUrl, 'Max retries exceeded', 'Max retries exceeded');
441+
error.maxRetries = retries;
442+
error.attempts = retries;
443+
444+
throw error;
436445
}
437446

438447
/**
@@ -467,15 +476,20 @@ export class MongoBinaryDownload {
467476
"This means that the requested version-platform combination doesn't exist\n" +
468477
"Try to use different version 'new MongoMemoryServer({ binary: { version: 'X.Y.Z' } })'\n" +
469478
'List of available versions can be found here: ' +
470-
'https://www.mongodb.com/download-center/community/releases/archive'
479+
'https://www.mongodb.com/download-center/community/releases/archive',
480+
'Status: 403'
471481
)
472482
);
473483

474484
return;
475485
}
476486

477487
reject(
478-
new DownloadError(downloadUrl, `Status Code isn't 200! (it is ${response.statusCode})`)
488+
new DownloadError(
489+
downloadUrl,
490+
`Status Code isn't 200! (it is ${response.statusCode})`,
491+
`Status: ${response.statusCode}`
492+
)
479493
);
480494

481495
return;
@@ -504,7 +518,8 @@ export class MongoBinaryDownload {
504518
reject(
505519
new DownloadError(
506520
downloadUrl,
507-
'Response header "content-length" does not exist or resolved to NaN'
521+
'Response header "content-length" does not exist or resolved to NaN',
522+
'Invalid Content-Length'
508523
)
509524
);
510525

@@ -527,7 +542,8 @@ export class MongoBinaryDownload {
527542
reject(
528543
new DownloadError(
529544
downloadUrl,
530-
`Too small (${this.dlProgress.current} bytes) mongod binary downloaded.`
545+
`Too small (${this.dlProgress.current} bytes) mongod binary downloaded.`,
546+
'File too small'
531547
)
532548
);
533549

@@ -548,18 +564,25 @@ export class MongoBinaryDownload {
548564
});
549565

550566
response.on('error', (err: Error) => {
551-
reject(new DownloadError(downloadUrl, err.message));
567+
// use the code if available, otherwise use the entire message
568+
const code = (err as any)?.code ?? err.message;
569+
570+
reject(new DownloadError(downloadUrl, err.message, code));
552571
});
553572
});
554573

555574
request.on('error', (err: Error) => {
556575
console.error(`Could NOT download "${downloadUrl}"!`, err.message);
557-
reject(new DownloadError(downloadUrl, err.message));
576+
577+
// use the code if available, otherwise use the entire message
578+
const code = (err as any)?.code ?? err.message;
579+
580+
reject(new DownloadError(downloadUrl, err.message, code));
558581
});
559582

560583
request.setTimeout(60000, () => {
561584
request.destroy();
562-
reject(new DownloadError(downloadUrl, 'Request timeout after 60 seconds'));
585+
reject(new DownloadError(downloadUrl, 'Request timeout after 60 seconds', 'ETIMEDOUT'));
563586
});
564587
});
565588
}

packages/mongodb-memory-server-core/src/util/errors.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,16 @@ export class UnknownVersionError extends Error {
202202
* Error for when downloading fails
203203
*/
204204
export class DownloadError extends Error {
205+
/// The maximal amount of re-trying a download
206+
public maxRetries?: number;
207+
/// The attempted tries for a download
208+
public attempts?: number;
209+
205210
constructor(
206211
public url: string,
207-
public msg: string
212+
public msg: string,
213+
/// The error code, if available like "Status: 404", "ENOTFOUND" or "aborted"
214+
public code?: string
208215
) {
209216
super(`Download failed for url \"${url}\", Details:\n${msg}`);
210217
}

0 commit comments

Comments
 (0)