From b6627be69222794cc5bcf04ed8d7667b98fe6eac Mon Sep 17 00:00:00 2001 From: Nathanael Jourdane Date: Mon, 14 Mar 2016 12:57:03 +0100 Subject: [PATCH] Improve Exceptions --- src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java | 20 ++++++++++---------- src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java | 9 +++++++-- src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java | 25 ++++++++++++------------- src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java | 12 ++++++------ src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java | 61 ++++++++++++++++++++----------------------------------------- src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableParser.java | 4 ++-- 6 files changed, 57 insertions(+), 74 deletions(-) diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java b/src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java index fb906cb..07e5aee 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java @@ -27,6 +27,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.BoxLayout; @@ -43,8 +44,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableConnection; -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.BadRequestException; -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.HTTPRequestException; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.SendQueryException; public abstract class ParamField extends JPanel { /** The logger for the class ParamField. */ @@ -215,13 +215,9 @@ public abstract class ParamField extends JPanel { this.add(comboBox); } - private String[] getItems(String begining) { + private String[] getItems(String begining) throws SendQueryException { StringBuilder resolverResult = null; - try { - resolverResult = VOTableConnection.sendGet(RESOLVER_URL, "q=\"" + begining + "\""); - } catch (HTTPRequestException | BadRequestException e) { - logger.severe("Can not send sersolver query: " + e); - } + resolverResult = VOTableConnection.sendGet(RESOLVER_URL, "q=\"" + begining + "\""); JsonObject root = new JsonParser().parse(resolverResult.toString()).getAsJsonObject(); int count = Integer.parseInt(root.get("count").toString()); String[] targetNames = new String[count]; @@ -242,8 +238,12 @@ public abstract class ParamField extends JPanel { if (content.length() >= 2) { lastContent = content; comboBox.removeAllItems(); - for (String s : getItems(content)) { - comboBox.addItem(s); + try { + for (String s : getItems(content)) { + comboBox.addItem(s); + } + } catch (SendQueryException e) { + logger.log(Level.WARNING, "Can't get table names for the resolver", e); } comboBox.getEditor().setItem(content); } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java index 5fa7a60..23059c2 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java @@ -24,6 +24,7 @@ import javax.swing.SwingUtilities; import com.google.gson.Gson; import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableController; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException; /** * Simple class to have a main function to display a voTable from a XML file. @@ -56,10 +57,14 @@ public class VOTableApp { @Override public void run() { // TODO: Add option to export to CSV and HTML in CLI. - VOTableController voTableControl; + VOTableController voTableControl = null; logger.info("Lauching VOTable app with arguments:\n " + new Gson().toJson(args)); if (args.length == 1) { - voTableControl = new VOTableController(args[0]); + try { + voTableControl = new VOTableController(args[0]); + } catch (VOTableException e) { + System.console().writer().println("Error: " + e.getMessage()); + } } else if (args.length == 3) { voTableControl = new VOTableController(args[0], args[1], args[2]); } else { diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java index 6700b8f..6b62a3e 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java @@ -32,7 +32,7 @@ import java.util.logging.Logger; import eu.omp.irap.vespa.epntapclient.utils.Const; import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.BadRequestException; -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.HTTPRequestException; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.SendQueryException; /** * This class provide HTTP connection support to send requests through POST / GET protocols. @@ -59,7 +59,7 @@ public final class VOTableConnection { * @return The path of the temporary XML file containing the http response. */ public static String sendQuery(String targetURL, String queryLanguage, - String query) throws HTTPRequestException, BadRequestException { + String query) throws SendQueryException { SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd_hhmmss_SSS"); String voTablePath = Const.TMP_DIR + "/votable" + ft.format(new Date()) + ".xml"; @@ -71,14 +71,14 @@ public final class VOTableConnection { .replace(".", "%2E").replace("-", "%2D").replace("*", "%2A") .replace("_", "%5F"); } catch (UnsupportedEncodingException e) { - throw new HTTPRequestException("Can not encode URI " + uri, e); + throw new SendQueryException("Can not encode URI " + uri, e); } logger.info("request: " + uri + "?" + parameters); try { printRequestResult(sendGet(uri, parameters), voTablePath); } catch (IOException e) { - throw new HTTPRequestException("Can not print the result in a file.", e); + throw new SendQueryException("Can not print the result in a file.", e); } return voTablePath; @@ -92,7 +92,7 @@ public final class VOTableConnection { * @throws BadRequestException If the */ public static StringBuilder sendGet(String url, String urlParameters) - throws HTTPRequestException, BadRequestException { + throws SendQueryException { String inputLine; StringBuilder response = new StringBuilder(); URL obj; @@ -101,7 +101,7 @@ public final class VOTableConnection { try { obj = new URL(uri); } catch (MalformedURLException e) { - throw new HTTPRequestException("The uri " + uri + "is not correctly formated", e); + throw new SendQueryException("The URI " + uri + " is not formated correctly.", e); } HttpURLConnection con; @@ -110,7 +110,7 @@ public final class VOTableConnection { con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); } catch (IOException e) { - throw new HTTPRequestException("Can not open HTTP GET connection with uri " + uri, e); + throw new SendQueryException("Can not open HTTP GET connection with uri " + uri, e); } con.setRequestProperty("User-Agent", USER_AGENT); @@ -119,12 +119,11 @@ public final class VOTableConnection { try { code = con.getResponseCode(); } catch (IOException e) { - throw new HTTPRequestException("Can not get the HTTP response code", e); + throw new SendQueryException("Can not get the HTTP response code", e); } - if (code != HttpURLConnection.HTTP_OK - && code != HttpURLConnection.HTTP_BAD_REQUEST) { - throw new HTTPRequestException("The server returned a bad response code: " + code); + if (code != HttpURLConnection.HTTP_OK && code != HttpURLConnection.HTTP_BAD_REQUEST) { + throw new SendQueryException("The server returned a bad response code: " + code); } try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) { @@ -140,10 +139,10 @@ public final class VOTableConnection { } in.close(); } catch (IOException e2) { - throw new HTTPRequestException("Can not get the error stream.", e2); + throw new SendQueryException("Can not get the error stream.", e2); } if (code != HttpURLConnection.HTTP_BAD_REQUEST) { - throw new HTTPRequestException( + throw new SendQueryException( "The server returned a bad response code (not 200 neither 400).", e1); } } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java index 4a63d80..bc9d96f 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java @@ -19,8 +19,8 @@ package eu.omp.irap.vespa.epntapclient.votable.controller; import java.io.IOException; import java.util.logging.Logger; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.ErrorInVOTableException; import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.VOTableCantFillVoTableException; -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.VOTableNotValidQueryException; import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.VOTableParsingException; import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.VOTableSeveralResourcesException; import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.VOTableSeveralTablesException; @@ -92,31 +92,31 @@ public class VOTableController { * @param voTablePath The path of the VOTable file. * @throws VOTableParsingException * @throws VOTableSeveralResourcesException - * @throws VOTableNotValidQueryException + * @throws ErrorInVOTableException * @throws VOTableSeveralTablesException * @throws VOTableCantFillVoTableException * @throws VOTableException Can not fill the VOTable data in the JTable. */ public void fillTable(String voTablePath) throws VOTableParsingException, VOTableSeveralResourcesException, - VOTableNotValidQueryException, VOTableSeveralTablesException, + ErrorInVOTableException, VOTableSeveralTablesException, VOTableCantFillVoTableException { voTable = VOTableParser.parseVOTable(voTablePath); // TODO: Handle the case when there are more than 1 resource or table. if (voTable.getRESOURCE().size() > 1) { - throw new VOTableSeveralResourcesException(); + throw new VOTableSeveralResourcesException(voTablePath); } // TODO: Iterate over all potential ERROR tags if ("ERROR".equals(voTable.getRESOURCE().get(0).getINFO().get(0).getValueAttribute())) { String errorInfo = voTable.getRESOURCE().get(0).getINFO().get(0).getValue(); - throw new VOTableNotValidQueryException(errorInfo); + throw new ErrorInVOTableException(errorInfo); } if (voTable.getRESOURCE().get(0).getLINKAndTABLEOrRESOURCE().size() > 1) { - throw new VOTableSeveralTablesException(); + throw new VOTableSeveralTablesException(voTablePath); } Table table = (Table) (voTable.getRESOURCE().get(0).getLINKAndTABLEOrRESOURCE().get(0)); diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java index ab937f8..53dc4a1 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java @@ -25,86 +25,65 @@ import java.util.logging.Logger; * @author N. Jourdane */ @SuppressWarnings({ "javadoc", "serial" }) -public class VOTableException extends Exception { +public abstract class VOTableException extends Exception { /** The logger for the class VOTableException. */ private static final Logger logger = Logger.getLogger(VOTableException.class.getName()); - /** The log message displayed when errors appends. */ - private static final String ERROR_MSG = "-- error --\n%1s\nbecause:\n%2s\n-- end of error --\n"; - - public VOTableException() { - logger.warning("A VOTable error occured."); - } - public VOTableException(String message) { - logger.warning(message); + super(message); } public VOTableException(String message, Exception e) { - logger.warning(String.format(ERROR_MSG, message, e.getMessage())); - } - - public VOTableException(Exception e) { - logger.warning(e.getMessage()); + super(message, e); } - public static class HTTPRequestException extends VOTableException { - public HTTPRequestException(String message) { - logger.warning(message); + public static class SendQueryException extends VOTableException { + public SendQueryException(String message) { + super("Can not send the query: " + message); } - public HTTPRequestException(String message, Exception e) { - logger.warning(String.format(ERROR_MSG, message, e.getMessage())); + public SendQueryException(String message, Exception e) { + super("Can not send the query: " + message + " because " + e.getMessage(), e); } } public static class VOTableParsingException extends VOTableException { public VOTableParsingException(String message, Exception e) { - logger.warning(String.format(ERROR_MSG, message, e.getMessage())); + super("Can not parse the VOTable: " + message + " because " + e.getMessage(), e); } } - // view.displayError("VOTable with more than one resource are not yet supported.\n" - // "See VOTable file for more informations: " + voTablePath); public static class VOTableSeveralResourcesException extends VOTableException { - public VOTableSeveralResourcesException() { - super(); + public VOTableSeveralResourcesException(String voTablePath) { + super("VOTable with more than one resource are not yet supported. \n" + + "See VOTable file for more informations: " + voTablePath); } } - // view.displayError("VOTable with more than one table are not yet supported.\n" - // "See VOTable file for more informations: " + voTablePath); public static class VOTableSeveralTablesException extends VOTableException { - public VOTableSeveralTablesException() { - super(); + public VOTableSeveralTablesException(String voTablePath) { + super("VOTable with more than one resource are not yet supported. \n" + + "See VOTable file for more informations: " + voTablePath); } } - // "Can not fill the VOTable data in the JTable." public static class VOTableCantFillVoTableException extends VOTableException { public VOTableCantFillVoTableException(IOException e) { - super(); + super("Can not fill the VOTable data in the JTable: " + e.getMessage(), e); } } // The query is not valid. - public static class VOTableNotValidQueryException extends VOTableException { - public VOTableNotValidQueryException(String errorInfo) { - super(); + public static class ErrorInVOTableException extends VOTableException { + public ErrorInVOTableException(String errorInfo) { + super("There is an error in the VOTable:\n" + errorInfo + + "\nPlease check your ADQL query."); } } public static class BadRequestException extends VOTableException { - private final String info; - public BadRequestException(String message, String info) { super(message); - this.info = info; - logger.warning(message + "\nDetails: " + info); - } - - public String getInfo() { - return info; } } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableParser.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableParser.java index 0ae3d3d..4c6df04 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableParser.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableParser.java @@ -70,8 +70,8 @@ public final class VOTableParser { public static VOTABLE parseVOTable(String voTablePath) throws VOTableParsingException { try { changeVOTableSchemaLocation(voTablePath); - } catch (IOException e1) { - throw new VOTableParsingException("Can not change the VOTable schema location.", e1); + } catch (IOException e) { + throw new VOTableParsingException("Can not change the VOTable schema location.", e); } // TODO: Change the name of the 2nd INFO tag instead of editing the XSD file. -- libgit2 0.21.2