]> WPIA git - gigi.git/commitdiff
upd: better error reporting from cert-gen api
authorFelix Dörre <felix@dogcraft.de>
Thu, 31 Mar 2016 17:26:26 +0000 (19:26 +0200)
committerFelix Dörre <felix@dogcraft.de>
Thu, 7 Apr 2016 11:52:06 +0000 (13:52 +0200)
src/org/cacert/gigi/GigiApiException.java
src/org/cacert/gigi/api/CreateCertificate.java
src/org/cacert/gigi/output/template/TranslateCommand.java

index 944fe645ece5618fb2dc93afc5318d164be6c84b..c2a60e69fc6a47137738177973a20cf16a7a86f5 100644 (file)
@@ -13,6 +13,8 @@ import org.cacert.gigi.output.template.TranslateCommand;
 
 public class GigiApiException extends Exception {
 
 
 public class GigiApiException extends Exception {
 
+    private static final Language PLAIN_LANGUAGE = Language.getInstance(Locale.ENGLISH);
+
     private static final long serialVersionUID = -164928670180852588L;
 
     private SQLException e;
     private static final long serialVersionUID = -164928670180852588L;
 
     private SQLException e;
@@ -68,6 +70,24 @@ public class GigiApiException extends Exception {
 
     }
 
 
     }
 
+    public void formatPlain(PrintWriter out) {
+        if (isInternalError()) {
+            out.println(PLAIN_LANGUAGE.getTranslation("An internal error ouccured."));
+        }
+        HashMap<String, Object> map = new HashMap<>();
+        for (Outputable message : messages) {
+            if (message instanceof TranslateCommand) {
+                String m = ((TranslateCommand) message).getRaw();
+                // Skip HTML Entities
+                out.println(PLAIN_LANGUAGE.getTranslation(m));
+            } else {
+                map.clear();
+                message.output(out, PLAIN_LANGUAGE, map);
+                out.println();
+            }
+        }
+    }
+
     public boolean isEmpty() {
         return e == null && messages.size() == 0;
     }
     public boolean isEmpty() {
         return e == null && messages.size() == 0;
     }
@@ -81,7 +101,7 @@ public class GigiApiException extends Exception {
             HashMap<String, Object> map = new HashMap<>();
             for (Outputable message : messages) {
                 map.clear();
             HashMap<String, Object> map = new HashMap<>();
             for (Outputable message : messages) {
                 map.clear();
-                message.output(pw, Language.getInstance(Locale.ENGLISH), map);
+                message.output(pw, PLAIN_LANGUAGE, map);
             }
             pw.flush();
 
             }
             pw.flush();
 
index a846285778ed497892e761d98e397100320922a3..d21b9c47cabd95500bd5011cb84c797bb7283d9d 100644 (file)
@@ -1,6 +1,7 @@
 package org.cacert.gigi.api;
 
 import java.io.IOException;
 package org.cacert.gigi.api;
 
 import java.io.IOException;
+import java.io.PrintWriter;
 import java.security.GeneralSecurityException;
 
 import javax.servlet.http.HttpServletRequest;
 import java.security.GeneralSecurityException;
 
 import javax.servlet.http.HttpServletRequest;
@@ -48,11 +49,13 @@ public class CreateCertificate extends APIPoint {
             resp.getWriter().println(PEM.encode("CERTIFICATE", result.cert().getEncoded()));
             return;
         } catch (GeneralSecurityException e) {
             resp.getWriter().println(PEM.encode("CERTIFICATE", result.cert().getEncoded()));
             return;
         } catch (GeneralSecurityException e) {
-            e.printStackTrace();
+            resp.sendError(500, "Crypto failed");
         } catch (GigiApiException e) {
         } catch (GigiApiException e) {
-            e.printStackTrace();
+            resp.setStatus(500);
+            PrintWriter wr = resp.getWriter();
+            e.formatPlain(wr);
         } catch (InterruptedException e) {
         } catch (InterruptedException e) {
-            e.printStackTrace();
+            resp.sendError(500, "Interrupted");
         }
     }
 }
         }
     }
 }
index cae48a63dba9fc8ae44b30e4925c10dab348ee14..7b7014e0775feb537428ed1e2c41af8222072d5c 100644 (file)
@@ -18,4 +18,8 @@ public final class TranslateCommand implements Outputable {
     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
         out.print(HTMLEncoder.encodeHTML(l.getTranslation(raw)));
     }
     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
         out.print(HTMLEncoder.encodeHTML(l.getTranslation(raw)));
     }
+
+    public String getRaw() {
+        return raw;
+    }
 }
 }