1 package club.wpia.gigi;
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5 import java.sql.SQLException;
6 import java.util.HashMap;
7 import java.util.LinkedList;
8 import java.util.Locale;
10 import club.wpia.gigi.localisation.Language;
11 import club.wpia.gigi.output.template.Outputable;
12 import club.wpia.gigi.output.template.TranslateCommand;
14 public class GigiApiException extends Exception {
16 private static final Language PLAIN_LANGUAGE = Language.getInstance(Locale.ENGLISH);
18 private static final long serialVersionUID = -164928670180852588L;
20 private SQLException e;
22 private LinkedList<Outputable> messages = new LinkedList<>();
24 public GigiApiException(SQLException e) {
29 public GigiApiException(String message) {
31 messages.add(new TranslateCommand(message));
34 public GigiApiException() {
38 public GigiApiException(Outputable out) {
42 public void mergeInto(GigiApiException e2) {
43 messages.addAll(e2.messages);
49 public boolean isInternalError() {
53 public void format(PrintWriter out, Language language) {
54 out.println("<div class='alert alert-danger error-msgs'>");
55 if (isInternalError()) {
58 out.println(language.getTranslation("An internal error occurred."));
61 HashMap<String, Object> map = new HashMap<>();
62 for (Outputable message : messages) {
66 message.output(out, language, map);
69 out.println("</div>");
73 public void formatPlain(PrintWriter out) {
74 if (isInternalError()) {
75 out.println(PLAIN_LANGUAGE.getTranslation("An internal error occurred."));
77 HashMap<String, Object> map = new HashMap<>();
78 for (Outputable message : messages) {
79 if (message instanceof TranslateCommand) {
80 String m = ((TranslateCommand) message).getRaw();
82 out.println(PLAIN_LANGUAGE.getTranslation(m));
85 message.output(out, PLAIN_LANGUAGE, map);
91 public boolean isEmpty() {
92 return e == null && messages.size() == 0;
96 public String getMessage() {
97 if (messages.size() != 0) {
98 StringWriter sw = new StringWriter();
99 PrintWriter pw = new PrintWriter(sw);
101 HashMap<String, Object> map = new HashMap<>();
102 for (Outputable message : messages) {
104 message.output(pw, PLAIN_LANGUAGE, map);
108 return sw.toString();