@@ -83,30 +83,42 @@ class Response extends Component {
8383 const oldTree = { ...this . state . tree } ;
8484 const oldFiles = oldTree [ directory ] ;
8585
86- // some clients might send more than one directory in the response,
87- // if the requested directory contains subdirectories. the root directory
88- // is always first, and for now we'll only display the contents of that.
89- const allDirectories = await getDirectoryContents ( {
90- directory,
91- username,
92- } ) ;
86+ try {
87+ // some clients might send more than one directory in the response,
88+ // if the requested directory contains subdirectories. the root directory
89+ // is always first, and for now we'll only display the contents of that.
90+ const allDirectories = await getDirectoryContents ( {
91+ directory,
92+ username,
93+ } ) ;
94+ const theRootDirectory = allDirectories ?. [ 0 ] ;
9395
94- const theRootDirectory = allDirectories ?. [ 0 ] ;
95- const { files, name } = theRootDirectory ;
96+ // some clients might send an empty response for some reason
97+ if ( ! theRootDirectory ) {
98+ throw new Error ( 'No directories were included in the response' ) ;
99+ }
96100
97- // the api returns file names only, so we need to prepend the directory
98- // to make it look like a search result. we also need to preserve
99- // any file selections, so check the old files and assign accordingly
100- const fixedFiles = files . map ( ( file ) => ( {
101- ...file ,
102- filename : `${ directory } \\${ file . filename } ` ,
103- selected :
104- oldFiles . find ( ( f ) => f . filename === `${ directory } \\${ file . filename } ` )
105- ?. selected ?? false ,
106- } ) ) ;
101+ const { files, name } = theRootDirectory ;
102+
103+ // the api returns file names only, so we need to prepend the directory
104+ // to make it look like a search result. we also need to preserve
105+ // any file selections, so check the old files and assign accordingly
106+ const fixedFiles = files . map ( ( file ) => ( {
107+ ...file ,
108+ filename : `${ directory } \\${ file . filename } ` ,
109+ selected :
110+ oldFiles . find (
111+ ( f ) => f . filename === `${ directory } \\${ file . filename } ` ,
112+ ) ?. selected ?? false ,
113+ } ) ) ;
107114
108- oldTree [ name ] = fixedFiles ;
109- this . setState ( { tree : { ...oldTree } } ) ;
115+ oldTree [ name ] = fixedFiles ;
116+ this . setState ( { tree : { ...oldTree } } ) ;
117+ } catch ( error ) {
118+ throw new Error ( `Failed to process directory response: ${ error } ` , {
119+ cause : error ,
120+ } ) ;
121+ }
110122 } catch ( error ) {
111123 console . error ( error ) ;
112124 toast . error ( error ?. response ?. data ?? error ?. message ?? error ) ;
0 commit comments