Added BugReporter and added logging when RequestManagers fail

This commit is contained in:
Rohit Awate 2018-03-01 15:38:44 +05:30
parent 2361da62eb
commit a679c6d1a2
6 changed files with 223 additions and 19 deletions

1
.gitignore vendored
View file

@ -7,3 +7,4 @@ src/main/java/META-INF/
dependency-reduced-pom.xml dependency-reduced-pom.xml
config/ config/
logs/ logs/
/RESTaurant/BugReporter.jar

View file

@ -180,14 +180,15 @@ public class DashboardController implements Initializable {
requestManager.setOnFailed(e -> { requestManager.setOnFailed(e -> {
loadingLayer.setVisible(false); loadingLayer.setVisible(false);
promptLayer.setVisible(false); promptLayer.setVisible(false);
Throwable exception = requestManager.getException(); Throwable throwable = requestManager.getException();
exception.printStackTrace(); Exception exception = (Exception) throwable;
Services.loggingService.logWarning("GET request could not be processed.", exception, LocalDateTime.now());
if (exception.getClass() == UnreliableResponseException.class) { if (throwable.getClass() == UnreliableResponseException.class) {
UnreliableResponseException URE = (UnreliableResponseException) exception; UnreliableResponseException URE = (UnreliableResponseException) throwable;
errorTitle.setText(URE.getExceptionTitle()); errorTitle.setText(URE.getExceptionTitle());
errorDetails.setText(URE.getExceptionDetails()); errorDetails.setText(URE.getExceptionDetails());
} else if (exception.getClass() == ProcessingException.class) { } else if (throwable.getClass() == ProcessingException.class) {
errorTitle.setText("RESTaurant couldn't connect."); errorTitle.setText("RESTaurant couldn't connect.");
errorDetails.setText("Either you are not connected to the Internet or the server is offline."); errorDetails.setText("Either you are not connected to the Internet or the server is offline.");
} }
@ -233,23 +234,24 @@ public class DashboardController implements Initializable {
requestManager.setOnFailed(e -> { requestManager.setOnFailed(e -> {
loadingLayer.setVisible(false); loadingLayer.setVisible(false);
promptLayer.setVisible(false); promptLayer.setVisible(false);
Throwable exception = requestManager.getException(); Throwable throwable = requestManager.getException();
exception.printStackTrace(); Exception exception = (Exception) throwable;
Services.loggingService.logWarning(httpMethodBox.getValue() + " request could not be processed.", exception, LocalDateTime.now());
if (exception.getClass() == UnreliableResponseException.class) { if (throwable.getClass() == UnreliableResponseException.class) {
UnreliableResponseException URE = (UnreliableResponseException) exception; UnreliableResponseException URE = (UnreliableResponseException) throwable;
errorTitle.setText(URE.getExceptionTitle()); errorTitle.setText(URE.getExceptionTitle());
errorDetails.setText(URE.getExceptionDetails()); errorDetails.setText(URE.getExceptionDetails());
} else if (exception.getClass() == ProcessingException.class) { } else if (throwable.getClass() == ProcessingException.class) {
if (exception.getCause().getClass() == UnknownHostException.class || if (throwable.getCause().getClass() == UnknownHostException.class ||
exception.getCause().getClass() == ConnectException.class) { throwable.getCause().getClass() == ConnectException.class) {
errorTitle.setText("RESTaurant couldn't connect."); errorTitle.setText("RESTaurant couldn't connect.");
errorDetails.setText("Either you are not connected to the Internet or the server is offline."); errorDetails.setText("Either you are not connected to the Internet or the server is offline.");
} else if (exception.getCause().getClass() == IllegalArgumentException.class) { } else if (throwable.getCause().getClass() == IllegalArgumentException.class) {
errorTitle.setText("Did you forget something?"); errorTitle.setText("Did you forget something?");
errorDetails.setText("Please specify at least one body part for your " + httpMethodBox.getValue() + " request."); errorDetails.setText("Please specify at least one body part for your " + httpMethodBox.getValue() + " request.");
} }
} else if (exception.getClass() == FileNotFoundException.class) } else if (throwable.getClass() == FileNotFoundException.class)
snackBar.show("File could not be found.", 5000); snackBar.show("File could not be found.", 5000);
errorLayer.setVisible(true); errorLayer.setVisible(true);
requestManager.reset(); requestManager.reset();
@ -288,14 +290,15 @@ public class DashboardController implements Initializable {
requestManager.setOnFailed(e -> { requestManager.setOnFailed(e -> {
loadingLayer.setVisible(false); loadingLayer.setVisible(false);
promptLayer.setVisible(false); promptLayer.setVisible(false);
Throwable exception = requestManager.getException(); Throwable throwable = requestManager.getException();
exception.printStackTrace(); Exception exception = (Exception) throwable;
Services.loggingService.logWarning("DELETE request could not be processed.", exception, LocalDateTime.now());
if (exception.getClass() == UnreliableResponseException.class) { if (throwable.getClass() == UnreliableResponseException.class) {
UnreliableResponseException URE = (UnreliableResponseException) exception; UnreliableResponseException URE = (UnreliableResponseException) throwable;
errorTitle.setText(URE.getExceptionTitle()); errorTitle.setText(URE.getExceptionTitle());
errorDetails.setText(URE.getExceptionDetails()); errorDetails.setText(URE.getExceptionDetails());
} else if (exception.getClass() == ProcessingException.class) { } else if (throwable.getClass() == ProcessingException.class) {
errorTitle.setText("No Internet Connection"); errorTitle.setText("No Internet Connection");
errorDetails.setText("Could not connect to the server. Please check your connection."); errorDetails.setText("Could not connect to the server. Please check your connection.");
} }

View file

@ -15,6 +15,7 @@
*/ */
package com.rohitawate.restaurant.main; package com.rohitawate.restaurant.main;
import com.rohitawate.restaurant.util.MiscUtils;
import com.rohitawate.restaurant.util.Services; import com.rohitawate.restaurant.util.Services;
import com.rohitawate.restaurant.util.settings.SettingsLoader; import com.rohitawate.restaurant.util.settings.SettingsLoader;
import com.rohitawate.restaurant.util.themes.ThemeManager; import com.rohitawate.restaurant.util.themes.ThemeManager;
@ -44,6 +45,8 @@ public class Main extends Application {
dashboardStage.setScene(new Scene(homeWindow)); dashboardStage.setScene(new Scene(homeWindow));
dashboardStage.setTitle("RESTaurant"); dashboardStage.setTitle("RESTaurant");
dashboardStage.show(); dashboardStage.show();
MiscUtils.createBugReporter();
} }
public static void main(String args[]) { public static void main(String args[]) {

View file

@ -0,0 +1,168 @@
/*
* Copyright 2018 Rohit Awate.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rohitawate.restaurant.util;
import java.io.*;
import java.nio.charset.Charset;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class BugReporter {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("RESTaurant Bug Reporting Service");
System.out.println();
System.out.println("Please describe the issue with as much detail and clarity as possible: ");
String userMessage = scanner.nextLine();
StringBuilder builder = new StringBuilder();
builder.append("\nThank you for your input! The issue was recorded.\n\n");
builder.append("With your permission, this service can collect some anonymous, non-personal information about your system.\n");
builder.append("This information will help us to better reproduce the issue and fix it quickly.\n");
builder.append("This includes:\n");
builder.append(" - Operating system details.\n");
builder.append(" - Details about your Java Runtime Environment.\n\n");
builder.append("Allow? (Y/N)\n>> ");
System.out.print(builder.toString());
String allowSystemData = scanner.nextLine();
allowSystemData = allowSystemData.toLowerCase();
StringBuilder report = new StringBuilder();
report.append("Log date: ");
report.append(LocalDateTime.now());
report.append("\n\n");
if (allowSystemData.equals("y") || allowSystemData.equals("yes")) {
report.append(generateSystemDetails());
System.out.println("\nThat's great! We will include your system details in with the bug report.");
} else {
System.out.println("\nAlrighty! We will only include RESTaurant's log files in the report.");
}
scanner.close();
report.append("User Message:\n");
report.append(userMessage);
generateReport(report.toString());
generateZipFile();
System.out.println("\nYour issue was successfully reported and will be fixed soon.");
System.out.println("Thank you! :)");
}
private static String generateSystemDetails() {
StringBuilder builder = new StringBuilder();
String OS = System.getProperty("os.name");
if (OS.equals("Linux")) {
builder.append(getLinuxDetails());
}
builder.append("OS: ");
builder.append(OS);
builder.append(" ");
builder.append(System.getProperty("os.arch"));
builder.append(" ");
builder.append(System.getProperty("os.version"));
builder.append("\n");
builder.append("Java VM: ");
builder.append(System.getProperty("java.vm.name"));
builder.append(" ");
builder.append(System.getProperty("java.version"));
builder.append("\nJava VM Vendor: ");
builder.append(System.getProperty("java.vendor"));
builder.append("\n\n");
return builder.toString();
}
private static void generateReport(String reportContents) {
String reportFileName = "Report - " + LocalDate.now() + ".txt";
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("logs/" + reportFileName));
writer.write(reportContents);
writer.close();
} catch (IOException IOE) {
IOE.printStackTrace();
}
}
private static void generateZipFile() {
try {
Scanner scanner;
FileInputStream fileInputStream;
ZipOutputStream zipStream = new ZipOutputStream(new FileOutputStream("Logs.zip"), Charset.forName("UTF-8"));
File sourceDir = new File("logs/");
String[] logFiles = sourceDir.list();
for (String logFile : logFiles) {
zipStream.putNextEntry(new ZipEntry(logFile));
fileInputStream = new FileInputStream("logs/" + logFile);
scanner = new Scanner(fileInputStream);
while (scanner.hasNext()) {
zipStream.flush();
zipStream.write(scanner.nextLine().getBytes());
zipStream.write('\n');
}
zipStream.closeEntry();
scanner.close();
fileInputStream.close();
}
zipStream.close();
} catch (IOException IOE) {
IOE.printStackTrace();
}
}
private static String getLinuxDetails() {
String line = "";
try {
File etcDir = new File("/etc/");
String releaseFile = "";
for (String file : etcDir.list()) {
if (file.endsWith("-release")) {
releaseFile = file;
break;
}
}
BufferedReader reader = new BufferedReader(new FileReader("/etc/" + releaseFile));
while ((line = reader.readLine()) != null) {
if (line.startsWith("PRETTY_NAME"))
break;
}
reader.close();
if (!line.equals("")) {
line = "Distribution: " + line.substring(13, line.length() - 1) + "\n";
}
} catch (IOException IOE) {
line = "";
Scanner scanner = new Scanner(System.in);
System.out.println("We couldn't fetch information about your Linux distribution. Please fill in the following details:");
System.out.println("Distribution name: ");
line += "Distribution: " + scanner.nextLine() + "\n";
System.out.println("Version: ");
line += "Version: " + scanner.nextLine() + "\n";
scanner.close();
}
return line;
}
}

View file

@ -16,6 +16,14 @@
package com.rohitawate.restaurant.util; package com.rohitawate.restaurant.util;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
public class MiscUtils { public class MiscUtils {
/** /**
* Removes leading and trailing quotation marks from strings. * Removes leading and trailing quotation marks from strings.
@ -26,4 +34,25 @@ public class MiscUtils {
public static String trimString(String input) { public static String trimString(String input) {
return input.replaceAll("\"", ""); return input.replaceAll("\"", "");
} }
/**
* Copies the BugReporter from within the JAR to the installation directory.
*/
public static void createBugReporter() {
new Thread(() -> {
File bugReporterFile = new File("RESTaurant/BugReporter.jar");
if (!bugReporterFile.exists()) {
InputStream inputStream = MiscUtils.class.getResourceAsStream("/BugReporter.jar");
Path bugReporter = Paths.get("RESTaurant/BugReporter.jar");
try {
Files.copy(inputStream, bugReporter);
} catch (IOException e) {
e.printStackTrace();
}
Services.loggingService.logInfo("BugReporter was copied to installation folder.", LocalDateTime.now());
} else {
Services.loggingService.logInfo("BugReporter was found.", LocalDateTime.now());
}
}).start();
}
} }

Binary file not shown.