@@ -196,6 +196,20 @@ export async function getFile(urlOrPath) {
196
196
}
197
197
}
198
198
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
+ }
199
213
/**
200
214
* Helper method to handle fatal errors that occur while trying to load a file from the Hugging Face Hub.
201
215
* @param {number } status The HTTP status code of the error.
@@ -211,33 +225,8 @@ function handleError(status, remoteURL, fatal) {
211
225
return null ;
212
226
}
213
227
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 } ".` ) ;
241
230
}
242
231
243
232
class FileCache {
0 commit comments