Skip to content

Commit

Permalink
Fix(App): Added more actionable error messages
Browse files Browse the repository at this point in the history
Added more actionable error messages by @rohanshah18 via PR #214 closing issue #213.
---------

Co-authored-by: Rohan Shah <rohanshah@Rohans-MBP.home>
Co-authored-by: Saptarshi Sarkar <saptarshi.programmer@gmail.com>
  • Loading branch information
3 people committed Jun 27, 2023
1 parent 30cb6be commit 0822df7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
15 changes: 6 additions & 9 deletions src/main/java/Backend/Drifty.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,20 @@ public Drifty(String url, String downloadsDirectory, String fileNameOfTheDownloa
*/
public void start() {
Utility utility = new Utility(messageBroker);
boolean errorFlag = false;
messageBroker.sendMessage("Validating the link...", DriftyConstants.LOGGER_INFO, "link");
if (url.contains(" ")) {
messageBroker.sendMessage("Link should not contain whitespace characters!", DriftyConstants.LOGGER_ERROR, "link");
errorFlag = true;
return;
} else if (url.length() == 0) {
messageBroker.sendMessage("Link cannot be empty!", DriftyConstants.LOGGER_ERROR, "link");
errorFlag = true;
return;
} else {
try {
Utility.isURLValid(url);
messageBroker.sendMessage("Link is valid!", DriftyConstants.LOGGER_INFO, "link");
} catch (Exception e) {
messageBroker.sendMessage(e.getMessage(), DriftyConstants.LOGGER_ERROR, "link");
errorFlag = true;
return;
}
}

Expand All @@ -90,7 +89,7 @@ public void start() {
new CheckDirectory(downloadsFolder);
} catch (IOException e) {
messageBroker.sendMessage(e.getMessage(), DriftyConstants.LOGGER_ERROR, "directory");
errorFlag = true;
return;
}
}
}
Expand All @@ -99,13 +98,11 @@ public void start() {
fileName = utility.findFilenameInLink(url);
if (fileName == null || fileName.length() == 0) {
messageBroker.sendMessage("Filename cannot be empty!", DriftyConstants.LOGGER_ERROR, "Filename");
errorFlag = true;
return;
}
}

if (!errorFlag) {
new FileDownloader(url, fileName, downloadsFolder).run();
}
new FileDownloader(url, fileName, downloadsFolder).run();
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/Backend/FileDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ private static void downloadFile() {

} catch (SecurityException e) {
messageBroker.sendMessage("Write access to " + dir + fileName + " denied !", LOGGER_ERROR, "download");
} catch (FileNotFoundException fileNotFoundException) {
messageBroker.sendMessage(FILENOTFOUND, LOGGER_ERROR, "download");
} catch (IOException e) {
messageBroker.sendMessage(FAILED_TO_DOWNLOAD_CONTENTS, LOGGER_ERROR, "download");
}
Expand Down Expand Up @@ -284,6 +286,14 @@ public void run() {
messageBroker.sendMessage(USER_INTERRUPTION, LOGGER_ERROR, "download");
} catch (IOException io1) {
messageBroker.sendMessage(FAILED_TO_DOWNLOAD_YOUTUBE_VIDEO, LOGGER_ERROR, "download");
String message = e.getMessage();
String[] messageArray = message.split(",");
if(messageArray.length >= 1 && messageArray[1].toLowerCase().trim().replaceAll(" ", "").equals("permissiondenied")) {
messageBroker.sendMessage(PERMISSIONDENIED_YOUTUBE_VIDEO, LOGGER_ERROR, "download");
}
else {
System.out.println(e.getMessage());
}
}
} catch (IOException io) {
messageBroker.sendMessage(FAILED_TO_INITIALISE_YOUTUBE_VIDEO, LOGGER_ERROR, "download");
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/CLI/Drifty_CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,17 @@ public static void main(String[] args) {
}
if ((fileName == null || (fileName.length() == 0)) && (!isYoutubeURL)) {
System.out.print(ENTER_FILE_NAME_WITH_EXTENSION);
fileName = SC.nextLine();
fileName = SC.next();
} else {
if (isYoutubeURL) {
System.out.print("Do you like to use the video title as the filename? (Enter Y for yes and N for no) : ");
System.out.print(RENAME_VIDEO_TITLE);
} else {
System.out.print(RENAME_FILE);
}
SC.nextLine(); // To remove 'whitespace' from input buffer.
String choiceString = SC.nextLine();
boolean choice = utility.yesNoValidation(choiceString, RENAME_FILE);
if (!choice) {
if (choice) {
System.out.print(ENTER_FILE_NAME_WITH_EXTENSION);
fileName = SC.nextLine();
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/Utils/DriftyConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public final class DriftyConstants {
public static final String CLI_APPLICATION_TERMINATED = "Drifty CLI (Command Line Interface) Application Terminated!";
public static final String GUI_APPLICATION_TERMINATED = "Drifty GUI (Graphical User Interface) Application Terminated!";
public static final String INVALID_LINK = "Invalid Link!";
public static final String AUTO_FILE_NAME_DETECTION_FAILED = "Automatic file name detection failed!";
public static final String AUTO_FILE_NAME_DETECTION_FAILED = "An error occurred! Either the file name or the extension was missing in the url. The url must be of the form of https://www.example.com/fileName.extension";
public static final String TRYING_TO_AUTO_DETECT_DOWNLOADS_FOLDER = "Trying to automatically detect default Downloads folder...";
public static final String TRYING_TO_DOWNLOAD_FILE = "Trying to download the file ...";
public static final String HELP_FLAG = "-help";
Expand All @@ -32,12 +32,13 @@ public final class DriftyConstants {
public static final String VERSION_FLAG_SHORT = "-v";
public static final String LOCATION_FLAG_SHORT = "-l";
public static final String BATCH_FLAG_SHORT = "-b";
public static final String ENTER_FILE_NAME_WITH_EXTENSION = "Enter the filename (with file extension) : ";
public static final String ENTER_FILE_LINK = "Enter the link to the file : ";
public static final String ENTER_FILE_NAME_WITH_EXTENSION = "Please enter the filename with file extension (fileName.extension) you would like to download : ";
public static final String ENTER_FILE_LINK = "Enter the link to the file (in the form of https://www.example.com/fileName.extension) : ";
public static final String OS_NAME = "os.name";
public static final String WINDOWS_OS_NAME = "Windows";
public static final String USER_HOME_PROPERTY = "user.home";
public static final String RENAME_FILE = "Would you like to rename this file? (Enter Y for yes and N for no) : ";
public static final String RENAME_VIDEO_TITLE = "Would you like to rename the video title? (Enter Y for yes and N for no) : ";
public static final String QUIT_OR_CONTINUE = "Press Q to Quit Or Press any Key to Continue";
public static final String FAILED_TO_RETRIEVE_DEFAULT_DOWNLOAD_FOLDER = "Failed to retrieve default download folder!";
public static final String FAILED_TO_CONNECT_TO_URL = "Failed to connect to ";
Expand All @@ -55,11 +56,13 @@ public final class DriftyConstants {
public static final String DIRECTORY_CREATED = "Directory Created";
public static final String ERROR_WHILE_CHECKING_FOR_DIRECTORY = "Error while checking for directory !";

public static final String FILENOTFOUND = "An error occurred! Requested file does not exist, please check the url.";
public static final String FAILED_TO_DOWNLOAD = "Failed to download ";
public static final String FAILED_TO_DOWNLOAD_CONTENTS = "Failed to download the contents ! ";
public static final String FAILED_TO_READ_DATA_STREAM = "Failed to get I/O operations channel to read from the data stream !";

public static final String FAILED_TO_DOWNLOAD_YOUTUBE_VIDEO = "Failed to download YouTube video!";
public static final String PERMISSIONDENIED_YOUTUBE_VIDEO = "You do not have access to download the video, permission is denied.";
public static final String FAILED_TO_INITIALISE_YOUTUBE_VIDEO = "Failed to initialise YouTube video downloader!";
public static final String THREAD_ERROR_ENCOUNTERED = "Error: thread encountered an error";

Expand Down

1 comment on commit 0822df7

@vercel
Copy link

@vercel vercel bot commented on 0822df7 Jun 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.