Skip to content

Commit

Permalink
Improve download file error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Redm4x committed Oct 19, 2023
1 parent 974d757 commit f01618b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ export const ShellDownloadModal = ({ selectedLease, onCloseClick, selectedServic
<Dialog open={true} maxWidth="xs" fullWidth onClose={onCloseClick}>
<DialogTitle className={classes.dialogTitle}>Download file</DialogTitle>
<DialogContent>
<Alert severity="info" className={classes.alert}>
<Typography variant="caption">Enter the path of a file on the server to be downloaded to your computer. Example: ~/app/logs.txt</Typography>
<Typography variant="caption">Enter the path of a file on the server to be downloaded to your computer. Example: /app/logs.txt</Typography>
<Alert severity="warning" className={classes.alert}>
<Typography variant="caption">This is an experimental feature and may not work reliably.</Typography>
</Alert>

<form onSubmit={handleSubmit(onSubmit)} ref={formRef}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,24 +158,29 @@ export const BackgroundTaskProvider = ({ children }) => {
let fileContent: Buffer | null = null;

ws.onmessage = event => {
let jsonData, exitCode, errorMessage;
let exitCode, errorMessage;
try {
const message = JSON.parse(event.data).message;

const bufferData = Buffer.from(message.data.slice(1));
const stringData = bufferData.toString("utf-8").replace(/^\n|\n$/g, "");

jsonData = JSON.parse(stringData);
exitCode = jsonData["exit_code"];
errorMessage = jsonData["message"];
try {
const jsonData = JSON.parse(stringData);
exitCode = jsonData["exit_code"];
errorMessage = jsonData["message"];
} catch (err) {}

if (exitCode !== undefined) {
if (errorMessage) {
console.error(`An error has occured: ${errorMessage}`);
} else if (fileContent === null) {
console.log("File content null");
} else {
console.log("Download done: " + fileContent.length);
isFinished = true;
}
console.log("Download done: " + fileContent.length);

isFinished = true;
ws.close();
} else {
if (!fileContent) {
Expand All @@ -188,6 +193,7 @@ export const BackgroundTaskProvider = ({ children }) => {
}
} catch (error) {
console.log(error);
ws.close();
}
};

Expand All @@ -202,7 +208,7 @@ export const BackgroundTaskProvider = ({ children }) => {
} else {
console.log("No file / Failed");
closeSnackbar(snackbarKey);
enqueueSnackbar("Failed to download logs", { variant: "error" });
enqueueSnackbar("Failed to download file", { variant: "error" });
}
};
ws.onopen = () => {
Expand Down

0 comments on commit f01618b

Please sign in to comment.