Skip to content

Commit 7b99d33

Browse files
authored
Merge pull request #1328 from slskd/bugfix
Fix cryptic message if a remote client doesn't return any files when directory contents are requested
2 parents f63238c + 951b65d commit 7b99d33

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

src/slskd/Users/API/Controllers/UsersController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ namespace slskd.Users.API
2626
using Asp.Versioning;
2727
using Microsoft.AspNetCore.Authorization;
2828
using Microsoft.AspNetCore.Mvc;
29+
using Serilog;
30+
2931
using Soulseek;
3032

3133
/// <summary>
@@ -57,6 +59,7 @@ public UsersController(ISoulseekClient soulseekClient, IBrowseTracker browseTrac
5759
private ISoulseekClient Client { get; }
5860
private IUserService Users { get; }
5961
private IOptionsSnapshot<Options> OptionsSnapshot { get; }
62+
private ILogger Log { get; set; } = Serilog.Log.ForContext<UsersController>();
6063

6164
/// <summary>
6265
/// Retrieves the address of the specified <paramref name="username"/>.
@@ -169,6 +172,9 @@ public async Task<IActionResult> Directory([FromRoute, Required] string username
169172
try
170173
{
171174
var result = await Client.GetDirectoryContentsAsync(username, request.Directory);
175+
176+
Log.Debug("{Endpoint} response from {User} for directory '{Directory}': {@Response}", nameof(Directory), username, request.Directory, result);
177+
172178
return Ok(result);
173179
}
174180
catch (UserOfflineException ex)

src/web/src/components/Search/Response.jsx

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)