Skip to content

Commit ced1c90

Browse files
committed
added logic for to avoid unexpected error
1 parent da00c3a commit ced1c90

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

apps/webapp/app/v3/r2.server.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,22 @@ export async function downloadPacketFromObjectStore(
9494
logger.debug("Downloading from object store", { url: url.href });
9595

9696

97-
async function fetchWithRetry(url: string, retries = 3, delay = 500): Promise<Response> {
97+
class NonRetryableError extends Error {
98+
constructor(message: string) {
99+
super(message);
100+
this.name = "NonRetryableError";
101+
}
102+
}
103+
104+
async function fetchWithRetry(url: string, retries = 3, delay = 500): Promise<Response> {
98105
for (let attempt = 1; attempt <= retries; attempt++) {
99106
try {
100107
const response = await r2.fetch(url);
108+
101109
if (response.ok) return response;
102110

103111
if (response.status >= 400 && response.status < 500) {
104-
throw new Error(`Client error (non-retryable): ${response.statusText}`);
112+
throw new NonRetryableError(`Client error: ${response.statusText}`);
105113
}
106114

107115
if (response.status >= 500 && response.status < 600) {
@@ -119,8 +127,12 @@ export async function downloadPacketFromObjectStore(
119127
continue;
120128
}
121129

122-
throw new Error(`Unexpected status ${response.status}: ${response.statusText}`);
130+
throw new NonRetryableError(`Unexpected status ${response.status}: ${response.statusText}`);
123131
} catch (error: unknown) {
132+
if (error instanceof NonRetryableError) {
133+
throw error;
134+
}
135+
124136
if (attempt === retries) throw error;
125137

126138
const errorMessage = error instanceof Error ? error.message : String(error);
@@ -137,6 +149,7 @@ export async function downloadPacketFromObjectStore(
137149
throw new Error(`Failed to fetch ${url} after ${retries} retries`);
138150
}
139151

152+
140153
const response = await fetchWithRetry(url.toString());
141154

142155
const data = await response.text();

0 commit comments

Comments
 (0)