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;
11 import club.wpia.gigi.localisation.Language;
12 import club.wpia.gigi.output.template.Outputable;
13 import club.wpia.gigi.output.template.TranslateCommand;
15 public class GigiApiException extends Exception {
17 private static final Language PLAIN_LANGUAGE = Language.getInstance(Locale.ENGLISH);
19 private static final long serialVersionUID = -164928670180852588L;
21 private SQLException e;
23 private LinkedList<Outputable> messages = new LinkedList<>();
25 public GigiApiException(SQLException e) {
30 public GigiApiException(String message) {
32 messages.add(new TranslateCommand(message));
35 public GigiApiException() {
39 public GigiApiException(Outputable out) {
43 public void mergeInto(GigiApiException e2) {
44 messages.addAll(e2.messages);
50 public boolean isInternalError() {
54 public void format(PrintWriter out, Language language, Map<String, Object> vars) {
55 out.println("<div class='alert alert-danger error-msgs'>");
56 if (isInternalError()) {
59 out.println(language.getTranslation("An internal error occurred."));
62 for (Outputable message : messages) {
63 HashMap<String, Object> map = new HashMap<>(vars);
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();