Skip to content

Commit

Permalink
Fix download logs button (#1227)
Browse files Browse the repository at this point in the history
## Problem

The button to download logs does not work. There are two problems:

* `response.blob()` is an asynchronous function, so `createObjectURL`
receives a promise instead of a Blob.
* Fetch returns a successful promise even if the response is not OK. So
the `catch` call does not handle that kind of problem. And, what is
worse, it is hiding the real issue.

## Solution

Wait for `response.blob()` before using its result and log errors in
`catch`.
  • Loading branch information
teclator authored May 17, 2024
2 parents 2837ade + 2b6efdb commit e23d2b3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions web/src/client/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ class ManagerBaseClient {
*/
async fetchLogs() {
const response = await fetch(`${this.client.baseUrl}/manager/logs`);
if (!response.ok) {
throw new Error("Could not fetch the logs");
}

return response;
}

Expand Down
10 changes: 8 additions & 2 deletions web/src/components/core/LogsButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,15 @@ const LogsButton = ({ ...props }) => {
const collectAndDownload = () => {
setError(null);
setIsCollecting(true);
cancellablePromise(client.manager.fetchLogs().then(response => URL.createObjectURL(response.blob())))
cancellablePromise(
client.manager.fetchLogs().then((response) => response.blob()),
)
.then(URL.createObjectURL)
.then(autoDownload)
.catch(setError)
.catch((error) => {
console.error(error);
setError(true);
})
.finally(() => setIsCollecting(false));
};

Expand Down

0 comments on commit e23d2b3

Please sign in to comment.