-
Notifications
You must be signed in to change notification settings - Fork 21
Generate webpage in case of exceptions #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,8 @@ | |
|
|
||
| package dev.starfix; | ||
| import io.quarkus.runtime.annotations.RegisterForReflection; | ||
| import jdk.jfr.StackTrace; | ||
|
|
||
| import org.zeroturnaround.exec.ProcessExecutor; | ||
| import org.zeroturnaround.exec.ProcessResult; | ||
| import picocli.CommandLine; | ||
|
|
@@ -32,10 +34,14 @@ | |
| import java.util.regex.Pattern; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
| import java.awt.Desktop; | ||
| import java.io.*; | ||
|
|
||
| @CommandLine.Command(name = "starfix", mixinStandardHelpOptions = true, defaultValueProvider = YAMLDefaultProvider.class) | ||
| @RegisterForReflection(classNames = "java.util.Properties") | ||
| public class Starfix implements Runnable{ | ||
|
|
||
| private static String OS = System.getProperty("os.name").toLowerCase(); | ||
|
|
||
| @Parameters(arity = "0..1") | ||
| String uri; | ||
|
|
@@ -54,12 +60,15 @@ public int config() throws Exception { | |
| } | ||
|
|
||
| @Command(name = "clone") | ||
| public int cloneCmd(@Parameters(index = "0") String url) { | ||
| public int cloneCmd(@Parameters(index = "0") String url)throws Exception { | ||
| CloneUrl cloneUrl = new CloneUrl(url); | ||
| // URL Validation to check a valid git repository | ||
| if (!validate_url(cloneUrl.url)) { // Incase URI doesn't macth our scheme we'll terminate | ||
| System.out.println(url); | ||
| throw new IllegalArgumentException("Not a valid URI for git repository"); | ||
| String message = "Not a valid URI for git repository: "+cloneUrl.url; | ||
| Exception illegalArgumentException = new IllegalArgumentException(message); | ||
| generateHTML(illegalArgumentException.getStackTrace(),message); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. think better to just pass in the exception then you can also pass message and other context in.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes 👍 |
||
| throw illegalArgumentException; | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -100,7 +109,11 @@ public void run() { | |
| new CommandLine(new Starfix()).usage(System.out); // Will invoke Picocli Help | ||
| return; | ||
| } | ||
| try{ | ||
| cloneCmd(uri); | ||
| }catch(Exception e){ | ||
| e.printStackTrace(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. never ok to just print out stacktrace in case of error. need to handle it or guide the user. |
||
| } | ||
| } | ||
|
|
||
| // Function to validate URL using with Regex | ||
|
|
@@ -116,11 +129,17 @@ public static boolean isBlob(String url){ | |
| return Pattern.matches(pattern,url); | ||
| } | ||
|
|
||
| // Function yo determine if the current OS is Windows | ||
| // Function to determine if the current OS is Windows | ||
| public static boolean isWindows() { | ||
| return System.getProperty("os.name").toLowerCase().contains("windows"); | ||
| return OS.contains("windows"); | ||
| } | ||
|
|
||
| public static boolean isMac() { | ||
| return OS.contains("mac"); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| // Function to fetch config file | ||
| public static File getConfigFile() { | ||
| String userHome = System.getProperty("user.home"); // Get User Home Directory: /home/user_name | ||
|
|
@@ -341,6 +360,44 @@ public static void processCloneURL(CloneUrl cloneUrl) throws URISyntaxException, | |
|
|
||
| } | ||
|
|
||
| public static void generateHTML(StackTraceElement[] stacktrace, String message)throws IOException, InterruptedException{ | ||
| String userHome = System.getProperty("user.home"); | ||
| File f = new File(userHome+"/starfixException.html"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this would cause conflicts if concurrent runs.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have made it a temp file now and delete it on exit . |
||
| BufferedWriter bw = new BufferedWriter(new FileWriter(f)); | ||
| bw.write("<html><body><h1>Starfix</h1>"); | ||
| bw.write("<a href='ide://config'>Launch Starfix Config Editor</a>"); | ||
| bw.write("<h2>"+message+"</h2>"); | ||
| bw.write("<p>"); | ||
|
|
||
| for(StackTraceElement line:stacktrace){ | ||
| bw.write(line.toString()+"<br>"); | ||
| } | ||
|
|
||
| bw.write("</p>"); | ||
| bw.write("</body></html>"); | ||
| bw.close(); | ||
|
|
||
| openWebPageInBrowser(f.toURI()); | ||
|
|
||
|
|
||
| } | ||
|
|
||
| public static void openWebPageInBrowser(URI uri) throws IOException, InterruptedException{ | ||
|
|
||
| runCommand(Paths.get(""),getBrowserLaunchCommandBasedOnOS(),uri.toString()); | ||
|
|
||
| } | ||
|
|
||
| public static String getBrowserLaunchCommandBasedOnOS(){ | ||
| if(isWindows()){ | ||
| return "start"; // If Windows | ||
| } else if(isMac()){ | ||
| return "open"; //If macosx | ||
| } else { | ||
| return "xdg-open"; // If Linux | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public static abstract class IDE{ | ||
| public abstract void launch_editor(Path directory, String ide, String path, String filePath)throws IOException, InterruptedException; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed