Skip to content

Commit 80d92cc

Browse files
committed
Use error mapping instead of switch block
1 parent 4feb8b4 commit 80d92cc

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

src/utils/hub.js

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,20 @@ export async function getFile(urlOrPath) {
196196
}
197197
}
198198

199+
const ERROR_MAPPING = {
200+
// 4xx errors (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses)
201+
400: 'Bad request error occurred while trying to load file',
202+
401: 'Unauthorized access to file',
203+
403: 'Forbidden access to file',
204+
404: 'Could not locate file',
205+
408: 'Request timeout error occurred while trying to load file',
206+
207+
// 5xx errors (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#server_error_responses)
208+
500: 'Internal server error error occurred while trying to load file',
209+
502: 'Bad gateway error occurred while trying to load file',
210+
503: 'Service unavailable error occurred while trying to load file',
211+
504: 'Gateway timeout error occurred while trying to load file',
212+
}
199213
/**
200214
* Helper method to handle fatal errors that occur while trying to load a file from the Hugging Face Hub.
201215
* @param {number} status The HTTP status code of the error.
@@ -211,33 +225,8 @@ function handleError(status, remoteURL, fatal) {
211225
return null;
212226
}
213227

214-
switch (status) {
215-
// 4xx errors (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses)
216-
case 400:
217-
throw Error(`Bad request error occurred while trying to load file: "${remoteURL}".`)
218-
case 401:
219-
throw Error(`Unauthorized access to file: "${remoteURL}".`)
220-
case 403:
221-
throw Error(`Forbidden access to file: "${remoteURL}".`)
222-
case 404:
223-
throw Error(`Could not locate file: "${remoteURL}".`)
224-
case 408:
225-
throw Error(`Request timeout error occurred while trying to load file: "${remoteURL}".`)
226-
227-
// 5xx errors (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#server_error_responses)
228-
case 500:
229-
throw Error(`Internal server error error occurred while trying to load file: "${remoteURL}".`)
230-
case 502:
231-
throw Error(`Bad gateway error occurred while trying to load file: "${remoteURL}".`)
232-
case 503:
233-
throw Error(`Service unavailable error occurred while trying to load file: "${remoteURL}".`)
234-
case 504:
235-
throw Error(`Gateway timeout error occurred while trying to load file: "${remoteURL}".`)
236-
237-
// Other:
238-
default:
239-
throw Error(`Error (${status}) occurred while trying to load file: "${remoteURL}".`)
240-
}
228+
const message = ERROR_MAPPING[status] ?? `Error (${status}) occurred while trying to load file`;
229+
throw Error(`${message}: "${remoteURL}".`);
241230
}
242231

243232
class FileCache {

0 commit comments

Comments
 (0)