Commit b6627be69222794cc5bcf04ed8d7667b98fe6eac

Authored by Nathanael Jourdane
1 parent a8594a14
Exists in master

Improve Exceptions

src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java
... ... @@ -27,6 +27,7 @@ import java.util.ArrayList;
27 27 import java.util.HashMap;
28 28 import java.util.List;
29 29 import java.util.Locale;
  30 +import java.util.logging.Level;
30 31 import java.util.logging.Logger;
31 32  
32 33 import javax.swing.BoxLayout;
... ... @@ -43,8 +44,7 @@ import com.google.gson.JsonObject;
43 44 import com.google.gson.JsonParser;
44 45  
45 46 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableConnection;
46   -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.BadRequestException;
47   -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.HTTPRequestException;
  47 +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.SendQueryException;
48 48  
49 49 public abstract class ParamField extends JPanel {
50 50 /** The logger for the class ParamField. */
... ... @@ -215,13 +215,9 @@ public abstract class ParamField extends JPanel {
215 215 this.add(comboBox);
216 216 }
217 217  
218   - private String[] getItems(String begining) {
  218 + private String[] getItems(String begining) throws SendQueryException {
219 219 StringBuilder resolverResult = null;
220   - try {
221   - resolverResult = VOTableConnection.sendGet(RESOLVER_URL, "q=\"" + begining + "\"");
222   - } catch (HTTPRequestException | BadRequestException e) {
223   - logger.severe("Can not send sersolver query: " + e);
224   - }
  220 + resolverResult = VOTableConnection.sendGet(RESOLVER_URL, "q=\"" + begining + "\"");
225 221 JsonObject root = new JsonParser().parse(resolverResult.toString()).getAsJsonObject();
226 222 int count = Integer.parseInt(root.get("count").toString());
227 223 String[] targetNames = new String[count];
... ... @@ -242,8 +238,12 @@ public abstract class ParamField extends JPanel {
242 238 if (content.length() >= 2) {
243 239 lastContent = content;
244 240 comboBox.removeAllItems();
245   - for (String s : getItems(content)) {
246   - comboBox.addItem(s);
  241 + try {
  242 + for (String s : getItems(content)) {
  243 + comboBox.addItem(s);
  244 + }
  245 + } catch (SendQueryException e) {
  246 + logger.log(Level.WARNING, "Can't get table names for the resolver", e);
247 247 }
248 248 comboBox.getEditor().setItem(content);
249 249 }
... ...
src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java
... ... @@ -24,6 +24,7 @@ import javax.swing.SwingUtilities;
24 24 import com.google.gson.Gson;
25 25  
26 26 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableController;
  27 +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException;
27 28  
28 29 /**
29 30 * Simple class to have a main function to display a voTable from a XML file.
... ... @@ -56,10 +57,14 @@ public class VOTableApp {
56 57 @Override
57 58 public void run() {
58 59 // TODO: Add option to export to CSV and HTML in CLI.
59   - VOTableController voTableControl;
  60 + VOTableController voTableControl = null;
60 61 logger.info("Lauching VOTable app with arguments:\n " + new Gson().toJson(args));
61 62 if (args.length == 1) {
62   - voTableControl = new VOTableController(args[0]);
  63 + try {
  64 + voTableControl = new VOTableController(args[0]);
  65 + } catch (VOTableException e) {
  66 + System.console().writer().println("Error: " + e.getMessage());
  67 + }
63 68 } else if (args.length == 3) {
64 69 voTableControl = new VOTableController(args[0], args[1], args[2]);
65 70 } else {
... ...
src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java
... ... @@ -32,7 +32,7 @@ import java.util.logging.Logger;
32 32  
33 33 import eu.omp.irap.vespa.epntapclient.utils.Const;
34 34 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.BadRequestException;
35   -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.HTTPRequestException;
  35 +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.SendQueryException;
36 36  
37 37 /**
38 38 * This class provide HTTP connection support to send requests through POST / GET protocols.
... ... @@ -59,7 +59,7 @@ public final class VOTableConnection {
59 59 * @return The path of the temporary XML file containing the http response.
60 60 */
61 61 public static String sendQuery(String targetURL, String queryLanguage,
62   - String query) throws HTTPRequestException, BadRequestException {
  62 + String query) throws SendQueryException {
63 63  
64 64 SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd_hhmmss_SSS");
65 65 String voTablePath = Const.TMP_DIR + "/votable" + ft.format(new Date()) + ".xml";
... ... @@ -71,14 +71,14 @@ public final class VOTableConnection {
71 71 .replace(".", "%2E").replace("-", "%2D").replace("*", "%2A")
72 72 .replace("_", "%5F");
73 73 } catch (UnsupportedEncodingException e) {
74   - throw new HTTPRequestException("Can not encode URI " + uri, e);
  74 + throw new SendQueryException("Can not encode URI " + uri, e);
75 75 }
76 76  
77 77 logger.info("request: " + uri + "?" + parameters);
78 78 try {
79 79 printRequestResult(sendGet(uri, parameters), voTablePath);
80 80 } catch (IOException e) {
81   - throw new HTTPRequestException("Can not print the result in a file.", e);
  81 + throw new SendQueryException("Can not print the result in a file.", e);
82 82 }
83 83  
84 84 return voTablePath;
... ... @@ -92,7 +92,7 @@ public final class VOTableConnection {
92 92 * @throws BadRequestException If the
93 93 */
94 94 public static StringBuilder sendGet(String url, String urlParameters)
95   - throws HTTPRequestException, BadRequestException {
  95 + throws SendQueryException {
96 96 String inputLine;
97 97 StringBuilder response = new StringBuilder();
98 98 URL obj;
... ... @@ -101,7 +101,7 @@ public final class VOTableConnection {
101 101 try {
102 102 obj = new URL(uri);
103 103 } catch (MalformedURLException e) {
104   - throw new HTTPRequestException("The uri " + uri + "is not correctly formated", e);
  104 + throw new SendQueryException("The URI " + uri + " is not formated correctly.", e);
105 105 }
106 106  
107 107 HttpURLConnection con;
... ... @@ -110,7 +110,7 @@ public final class VOTableConnection {
110 110 con = (HttpURLConnection) obj.openConnection();
111 111 con.setRequestMethod("GET");
112 112 } catch (IOException e) {
113   - throw new HTTPRequestException("Can not open HTTP GET connection with uri " + uri, e);
  113 + throw new SendQueryException("Can not open HTTP GET connection with uri " + uri, e);
114 114 }
115 115  
116 116 con.setRequestProperty("User-Agent", USER_AGENT);
... ... @@ -119,12 +119,11 @@ public final class VOTableConnection {
119 119 try {
120 120 code = con.getResponseCode();
121 121 } catch (IOException e) {
122   - throw new HTTPRequestException("Can not get the HTTP response code", e);
  122 + throw new SendQueryException("Can not get the HTTP response code", e);
123 123 }
124 124  
125   - if (code != HttpURLConnection.HTTP_OK
126   - && code != HttpURLConnection.HTTP_BAD_REQUEST) {
127   - throw new HTTPRequestException("The server returned a bad response code: " + code);
  125 + if (code != HttpURLConnection.HTTP_OK && code != HttpURLConnection.HTTP_BAD_REQUEST) {
  126 + throw new SendQueryException("The server returned a bad response code: " + code);
128 127 }
129 128  
130 129 try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
... ... @@ -140,10 +139,10 @@ public final class VOTableConnection {
140 139 }
141 140 in.close();
142 141 } catch (IOException e2) {
143   - throw new HTTPRequestException("Can not get the error stream.", e2);
  142 + throw new SendQueryException("Can not get the error stream.", e2);
144 143 }
145 144 if (code != HttpURLConnection.HTTP_BAD_REQUEST) {
146   - throw new HTTPRequestException(
  145 + throw new SendQueryException(
147 146 "The server returned a bad response code (not 200 neither 400).", e1);
148 147 }
149 148 }
... ...
src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java
... ... @@ -19,8 +19,8 @@ package eu.omp.irap.vespa.epntapclient.votable.controller;
19 19 import java.io.IOException;
20 20 import java.util.logging.Logger;
21 21  
  22 +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.ErrorInVOTableException;
22 23 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.VOTableCantFillVoTableException;
23   -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.VOTableNotValidQueryException;
24 24 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.VOTableParsingException;
25 25 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.VOTableSeveralResourcesException;
26 26 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.VOTableSeveralTablesException;
... ... @@ -92,31 +92,31 @@ public class VOTableController {
92 92 * @param voTablePath The path of the VOTable file.
93 93 * @throws VOTableParsingException
94 94 * @throws VOTableSeveralResourcesException
95   - * @throws VOTableNotValidQueryException
  95 + * @throws ErrorInVOTableException
96 96 * @throws VOTableSeveralTablesException
97 97 * @throws VOTableCantFillVoTableException
98 98 * @throws VOTableException Can not fill the VOTable data in the JTable.
99 99 */
100 100 public void fillTable(String voTablePath)
101 101 throws VOTableParsingException, VOTableSeveralResourcesException,
102   - VOTableNotValidQueryException, VOTableSeveralTablesException,
  102 + ErrorInVOTableException, VOTableSeveralTablesException,
103 103 VOTableCantFillVoTableException {
104 104  
105 105 voTable = VOTableParser.parseVOTable(voTablePath);
106 106  
107 107 // TODO: Handle the case when there are more than 1 resource or table.
108 108 if (voTable.getRESOURCE().size() > 1) {
109   - throw new VOTableSeveralResourcesException();
  109 + throw new VOTableSeveralResourcesException(voTablePath);
110 110 }
111 111  
112 112 // TODO: Iterate over all potential ERROR tags
113 113 if ("ERROR".equals(voTable.getRESOURCE().get(0).getINFO().get(0).getValueAttribute())) {
114 114 String errorInfo = voTable.getRESOURCE().get(0).getINFO().get(0).getValue();
115   - throw new VOTableNotValidQueryException(errorInfo);
  115 + throw new ErrorInVOTableException(errorInfo);
116 116 }
117 117  
118 118 if (voTable.getRESOURCE().get(0).getLINKAndTABLEOrRESOURCE().size() > 1) {
119   - throw new VOTableSeveralTablesException();
  119 + throw new VOTableSeveralTablesException(voTablePath);
120 120 }
121 121  
122 122 Table table = (Table) (voTable.getRESOURCE().get(0).getLINKAndTABLEOrRESOURCE().get(0));
... ...
src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java
... ... @@ -25,86 +25,65 @@ import java.util.logging.Logger;
25 25 * @author N. Jourdane
26 26 */
27 27 @SuppressWarnings({ "javadoc", "serial" })
28   -public class VOTableException extends Exception {
  28 +public abstract class VOTableException extends Exception {
29 29 /** The logger for the class VOTableException. */
30 30 private static final Logger logger = Logger.getLogger(VOTableException.class.getName());
31 31  
32   - /** The log message displayed when errors appends. */
33   - private static final String ERROR_MSG = "-- error --\n%1s\nbecause:\n%2s\n-- end of error --\n";
34   -
35   - public VOTableException() {
36   - logger.warning("A VOTable error occured.");
37   - }
38   -
39 32 public VOTableException(String message) {
40   - logger.warning(message);
  33 + super(message);
41 34 }
42 35  
43 36 public VOTableException(String message, Exception e) {
44   - logger.warning(String.format(ERROR_MSG, message, e.getMessage()));
45   - }
46   -
47   - public VOTableException(Exception e) {
48   - logger.warning(e.getMessage());
  37 + super(message, e);
49 38 }
50 39  
51   - public static class HTTPRequestException extends VOTableException {
52   - public HTTPRequestException(String message) {
53   - logger.warning(message);
  40 + public static class SendQueryException extends VOTableException {
  41 + public SendQueryException(String message) {
  42 + super("Can not send the query: " + message);
54 43 }
55 44  
56   - public HTTPRequestException(String message, Exception e) {
57   - logger.warning(String.format(ERROR_MSG, message, e.getMessage()));
  45 + public SendQueryException(String message, Exception e) {
  46 + super("Can not send the query: " + message + " because " + e.getMessage(), e);
58 47 }
59 48 }
60 49  
61 50 public static class VOTableParsingException extends VOTableException {
62 51 public VOTableParsingException(String message, Exception e) {
63   - logger.warning(String.format(ERROR_MSG, message, e.getMessage()));
  52 + super("Can not parse the VOTable: " + message + " because " + e.getMessage(), e);
64 53 }
65 54 }
66 55  
67   - // view.displayError("VOTable with more than one resource are not yet supported.\n"
68   - // "See VOTable file for more informations: " + voTablePath);
69 56 public static class VOTableSeveralResourcesException extends VOTableException {
70   - public VOTableSeveralResourcesException() {
71   - super();
  57 + public VOTableSeveralResourcesException(String voTablePath) {
  58 + super("VOTable with more than one resource are not yet supported. \n"
  59 + + "See VOTable file for more informations: " + voTablePath);
72 60 }
73 61 }
74 62  
75   - // view.displayError("VOTable with more than one table are not yet supported.\n"
76   - // "See VOTable file for more informations: " + voTablePath);
77 63 public static class VOTableSeveralTablesException extends VOTableException {
78   - public VOTableSeveralTablesException() {
79   - super();
  64 + public VOTableSeveralTablesException(String voTablePath) {
  65 + super("VOTable with more than one resource are not yet supported. \n"
  66 + + "See VOTable file for more informations: " + voTablePath);
80 67 }
81 68 }
82 69  
83   - // "Can not fill the VOTable data in the JTable."
84 70 public static class VOTableCantFillVoTableException extends VOTableException {
85 71 public VOTableCantFillVoTableException(IOException e) {
86   - super();
  72 + super("Can not fill the VOTable data in the JTable: " + e.getMessage(), e);
87 73 }
88 74 }
89 75  
90 76 // The query is not valid.
91   - public static class VOTableNotValidQueryException extends VOTableException {
92   - public VOTableNotValidQueryException(String errorInfo) {
93   - super();
  77 + public static class ErrorInVOTableException extends VOTableException {
  78 + public ErrorInVOTableException(String errorInfo) {
  79 + super("There is an error in the VOTable:\n" + errorInfo
  80 + + "\nPlease check your ADQL query.");
94 81 }
95 82 }
96 83  
97 84 public static class BadRequestException extends VOTableException {
98   - private final String info;
99   -
100 85 public BadRequestException(String message, String info) {
101 86 super(message);
102   - this.info = info;
103   - logger.warning(message + "\nDetails: " + info);
104   - }
105   -
106   - public String getInfo() {
107   - return info;
108 87 }
109 88 }
110 89  
... ...
src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableParser.java
... ... @@ -70,8 +70,8 @@ public final class VOTableParser {
70 70 public static VOTABLE parseVOTable(String voTablePath) throws VOTableParsingException {
71 71 try {
72 72 changeVOTableSchemaLocation(voTablePath);
73   - } catch (IOException e1) {
74   - throw new VOTableParsingException("Can not change the VOTable schema location.", e1);
  73 + } catch (IOException e) {
  74 + throw new VOTableParsingException("Can not change the VOTable schema location.", e);
75 75 }
76 76  
77 77 // TODO: Change the name of the 2nd INFO tag instead of editing the XSD file.
... ...