@@ -358,6 +358,107 @@ describe("resolveMedia getFile retry", () => {
358358 } ,
359359 ) ;
360360
361+ it . each ( [
362+ { errorCode : 400 , description : "Bad Request: wrong file identifier/HTTP URL specified" } ,
363+ { errorCode : 401 , description : "Unauthorized" } ,
364+ { errorCode : 403 , description : "Forbidden" } ,
365+ { errorCode : 404 , description : "Not Found" } ,
366+ { errorCode : 409 , description : "Conflict" } ,
367+ ] ) (
368+ "does not retry permanent Telegram getFile rejection $errorCode" ,
369+ async ( { errorCode, description } ) => {
370+ const error = new GrammyError (
371+ "Call to 'getFile' failed!" ,
372+ { ok : false , error_code : errorCode , description, parameters : { } } ,
373+ "getFile" ,
374+ { } ,
375+ ) ;
376+ const getFile = vi . fn ( ) . mockRejectedValue ( error ) ;
377+ const promise = resolveMediaWithDefaults ( makeCtx ( "document" , getFile ) ) ;
378+ const failure = expectMediaFetchError ( promise , {
379+ code : "http_error" ,
380+ messageIncludes : "Telegram getFile failed after retries" ,
381+ status : errorCode ,
382+ } ) ;
383+
384+ await flushRetryTimers ( ) ;
385+ await failure ;
386+
387+ expect ( getFile ) . toHaveBeenCalledTimes ( 1 ) ;
388+ } ,
389+ ) ;
390+
391+ it . each ( [
392+ {
393+ mediaField : "document" as const ,
394+ filePath : "documents/file_42.pdf" ,
395+ contentType : "application/pdf" ,
396+ } ,
397+ {
398+ mediaField : "sticker" as const ,
399+ filePath : "stickers/file_0.webp" ,
400+ contentType : "image/webp" ,
401+ } ,
402+ ] ) (
403+ "retries temporarily unavailable Telegram $mediaField files reported as 400" ,
404+ async ( { mediaField, filePath, contentType } ) => {
405+ const error = new GrammyError (
406+ "Call to 'getFile' failed!" ,
407+ {
408+ ok : false ,
409+ error_code : 400 ,
410+ description : "Bad Request: wrong file_id or the file is temporarily unavailable" ,
411+ parameters : { } ,
412+ } ,
413+ "getFile" ,
414+ { } ,
415+ ) ;
416+ const getFile = vi . fn ( ) . mockRejectedValueOnce ( error ) . mockResolvedValueOnce ( {
417+ file_path : filePath ,
418+ } ) ;
419+ const fileName = filePath . split ( "/" ) . at ( - 1 ) ;
420+ readRemoteMediaBuffer . mockResolvedValueOnce ( {
421+ buffer : Buffer . from ( "media" ) ,
422+ contentType,
423+ fileName,
424+ } ) ;
425+ saveMediaBuffer . mockResolvedValueOnce ( {
426+ path : `/tmp/${ fileName } ` ,
427+ contentType,
428+ } ) ;
429+
430+ const promise = resolveMediaWithDefaults ( makeCtx ( mediaField , getFile ) ) ;
431+ await flushRetryTimers ( ) ;
432+ const result = await promise ;
433+
434+ expect ( getFile ) . toHaveBeenCalledTimes ( 2 ) ;
435+ expectResolvedMediaFields ( result , `retried ${ mediaField } ` , {
436+ path : `/tmp/${ fileName } ` ,
437+ kind : mediaField ,
438+ } ) ;
439+ } ,
440+ ) ;
441+
442+ it . each ( [ 500 , 502 ] ) ( "retries Telegram getFile server error %i" , async ( errorCode ) => {
443+ const error = new GrammyError (
444+ "Call to 'getFile' failed!" ,
445+ { ok : false , error_code : errorCode , description : "Server Error" , parameters : { } } ,
446+ "getFile" ,
447+ { } ,
448+ ) ;
449+ const getFile = vi
450+ . fn ( )
451+ . mockRejectedValueOnce ( error )
452+ . mockResolvedValueOnce ( { file_path : "documents/file_42.pdf" } ) ;
453+ mockPdfFetchAndSave ( "file_42.pdf" ) ;
454+
455+ const promise = resolveMediaWithDefaults ( makeCtx ( "document" , getFile ) ) ;
456+ await flushRetryTimers ( ) ;
457+ await promise ;
458+
459+ expect ( getFile ) . toHaveBeenCalledTimes ( 2 ) ;
460+ } ) ;
461+
361462 it ( "does not catch errors from readRemoteMediaBuffer (only getFile is retried)" , async ( ) => {
362463 const getFile = vi . fn ( ) . mockResolvedValue ( { file_path : "voice/file_0.oga" } ) ;
363464 readRemoteMediaBuffer . mockRejectedValueOnce ( new Error ( "download failed" ) ) ;
0 commit comments