Skip to content

Commit 01c1cda

Browse files
Michael Kniggejavier-godoy
authored andcommitted
feat: make ErrorWindow internationalizable
Close #16
1 parent 497fe80 commit 01c1cda

File tree

3 files changed

+164
-13
lines changed

3 files changed

+164
-13
lines changed

src/main/java/com/flowingcode/vaadin/addons/errorwindow/ErrorWindow.java

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ public class ErrorWindow extends Dialog {
4848

4949
private static final Logger logger = LoggerFactory.getLogger(ErrorWindow.class);
5050

51-
private static final String DEFAULT_CAPTION = "<h3 style='margin-top:0px;text-align:center'>An error has occurred</h3>";
52-
53-
private static final String DEFAULT_ERROR_LABEL_MESSAGE = "Please contact the system administrator for more information.";
54-
5551
private VerticalLayout exceptionTraceLayout;
5652

5753
private final Throwable cause;
@@ -62,32 +58,50 @@ public class ErrorWindow extends Dialog {
6258

6359
private boolean productionMode;
6460

61+
private ErrorWindowI18n i18n;
62+
6563
public ErrorWindow(final Throwable cause) {
66-
this(cause, null, isProductionMode());
64+
this(cause, null, isProductionMode(), ErrorWindowI18n.createDefault());
65+
}
66+
67+
public ErrorWindow(final Throwable cause, final ErrorWindowI18n i18n) {
68+
this(cause, null, isProductionMode(), i18n);
6769
}
6870

6971
public ErrorWindow(final Throwable cause, final String errorMessage) {
70-
this(cause, errorMessage, isProductionMode());
72+
this(cause, errorMessage, isProductionMode(), ErrorWindowI18n.createDefault());
73+
}
74+
75+
public ErrorWindow(final Throwable cause, final String errorMessage, final ErrorWindowI18n i18n) {
76+
this(cause, errorMessage, isProductionMode(), i18n);
7177
}
7278

7379
public ErrorWindow(final Throwable cause, final String errorMessage, boolean productionMode) {
80+
this(cause, errorMessage, productionMode, ErrorWindowI18n.createDefault());
81+
}
82+
public ErrorWindow(final Throwable cause, final String errorMessage, boolean productionMode, final ErrorWindowI18n i18n) {
7483
super();
7584

7685
uuid = UUID.randomUUID().toString();
7786
this.cause = cause;
7887
this.errorMessage = errorMessage;
7988
this.productionMode = productionMode;
89+
this.i18n = i18n;
8090
initWindow();
8191
}
8292

8393
public ErrorWindow(ErrorDetails errorDetails) {
84-
this(errorDetails.getThrowable(), errorDetails.getCause());
94+
this(errorDetails, ErrorWindowI18n.createDefault());
8595
}
8696

8797
public ErrorWindow(ErrorDetails errorDetails, boolean productionMode) {
8898
this(errorDetails.getThrowable(), errorDetails.getCause(), productionMode);
8999
}
90100

101+
public ErrorWindow(ErrorDetails errorDetails, final ErrorWindowI18n i18n) {
102+
this(errorDetails.getThrowable(), errorDetails.getCause(), i18n);
103+
}
104+
91105
private static boolean isProductionMode() {
92106
return ErrorManager.getErrorWindowFactory().isProductionMode();
93107
}
@@ -110,7 +124,7 @@ private VerticalLayout createMainLayout() {
110124
mainLayout.setPadding(false);
111125
mainLayout.setMargin(false);
112126

113-
final Html title = new Html(DEFAULT_CAPTION);
127+
final Html title = new Html(String.format("<h3 style='margin-top:0px;text-align:center'>%s</h3>", i18n.getCaption()));
114128
title.getElement().getStyle().set("width", "100%");
115129
mainLayout.add(title);
116130

@@ -129,7 +143,7 @@ private VerticalLayout createMainLayout() {
129143
mainLayout.add(createExceptionTraceLayout());
130144
}
131145

132-
final Button closeButton = new Button("Close", event -> close());
146+
final Button closeButton = new Button(i18n.getClose(), event -> close());
133147
buttonsLayout.add(closeButton);
134148
mainLayout.add(buttonsLayout);
135149
mainLayout.setHorizontalComponentAlignment(Alignment.END, buttonsLayout);
@@ -138,7 +152,7 @@ private VerticalLayout createMainLayout() {
138152
}
139153

140154
private Button createDetailsButtonLayout() {
141-
final Button errorDetailsButton = new Button("Show error detail", event -> {
155+
final Button errorDetailsButton = new Button(i18n.getDetails(), event -> {
142156
boolean visible = !exceptionTraceLayout.isVisible();
143157
exceptionTraceLayout.setVisible(visible);
144158
if(visible) {
@@ -175,9 +189,9 @@ protected TextArea createStackTraceArea() {
175189
}
176190

177191
protected Html createErrorLabel() {
178-
String label = errorMessage == null ? DEFAULT_ERROR_LABEL_MESSAGE : errorMessage;
192+
String label = errorMessage == null ? i18n.getDefaultErrorMessage() : errorMessage;
179193
if (productionMode) {
180-
label = label.concat(String.format("<br />Please report the following code to system administrator:<h4><p><center>%s<center/></p></h4>", uuid));
194+
label = label.concat(String.format("<br />%s<h4><p><center>%s<center/></p></h4>", i18n.getInstructions(), uuid));
181195
}
182196
final Html errorLabel = new Html("<span>" + label + "</span>");
183197
errorLabel.getElement().getStyle().set("width", "100%");
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*-
2+
* #%L
3+
* Error Window Add-on Project
4+
* %%
5+
* Copyright (C) 2018 - 2020 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.addons.errorwindow;
21+
22+
/**
23+
* Internationalization object for customizing the component UI texts.
24+
* An instance with the default messages can be obtained using {@link ErrorWindowI18n#createDefault()}
25+
*
26+
27+
*
28+
*/
29+
public class ErrorWindowI18n {
30+
31+
private String caption;
32+
private String instructions;
33+
private String close;
34+
private String details;
35+
private String defaultErrorMessage;
36+
37+
private ErrorWindowI18n() {
38+
this.caption = "An error has occurred";
39+
this.instructions = "Please report the following code to your system administrator";
40+
this.close = "Close";
41+
this.details = "Show error details";
42+
this.defaultErrorMessage = "Please contact the system administrator for more information.";
43+
}
44+
45+
/**
46+
* @return a new instance with the default messages
47+
*/
48+
public static ErrorWindowI18n createDefault() {
49+
return new ErrorWindowI18n();
50+
}
51+
52+
/**
53+
* Sets the caption of the error window.
54+
*/
55+
public void setCaption(final String caption) {
56+
this.caption = caption;
57+
}
58+
59+
/**
60+
* @return returns the caption of the error window.
61+
*/
62+
public String getCaption() {
63+
return this.caption;
64+
}
65+
66+
/**
67+
* Sets the instructions for the user if the UUID is displayed in production mode.
68+
*/
69+
public void setInstructions(final String instructions) {
70+
this.instructions = instructions;
71+
}
72+
73+
/**
74+
* @return returns the instructions for the user if the UUID is displayed in production mode.
75+
*/
76+
public String getInstructions() {
77+
return this.instructions;
78+
}
79+
80+
/**
81+
* Sets the text of the "Close"-Button.
82+
*/
83+
public void setClose(final String close) {
84+
this.close = close;
85+
}
86+
87+
/**
88+
* @return returns the text of the "Close"-Button.
89+
*/
90+
public String getClose() {
91+
return this.close;
92+
}
93+
94+
/**
95+
* Sets the text of the "Details"-Button.
96+
*/
97+
public void setDetails(final String details) {
98+
this.details = details;
99+
}
100+
101+
/**
102+
* @return returns the text of the "Details"-Button.
103+
*/
104+
public String getDetails() {
105+
return this.details;
106+
}
107+
108+
/**
109+
* Sets the default error message shown if the message passed to the error window is <code>null</code>.
110+
*/
111+
public void setDefaultErrorMessage(final String defaultErrorMessage) {
112+
this.defaultErrorMessage = defaultErrorMessage;
113+
}
114+
115+
/**
116+
* @return returns the default error message shown if the message passed to the error window is <code>null</code>.
117+
*/
118+
public String getDefaultErrorMessage() {
119+
return this.defaultErrorMessage;
120+
}
121+
122+
}

src/test/java/com/flowingcode/vaadin/addons/errorwindow/ErrorwindowDemo.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,29 @@ public ErrorwindowDemo() {
4949
}
5050
});
5151

52+
Button throwErrorWithCustomMessageAndCustomTexts = new Button("Throw Error with custom message (custom labels)", ev -> {
53+
try {
54+
Integer.parseInt("asdf");
55+
} catch (NumberFormatException e) {
56+
ErrorWindowI18n i18n = ErrorWindowI18n.createDefault();
57+
i18n.setCaption("Uh oh... that's embarrassing");
58+
i18n.setInstructions("Please pass this unique error identifier to your administrator");
59+
i18n.setClose("Ok");
60+
i18n.setDetails("Show me technical details");
61+
i18n.setDefaultErrorMessage("Something really strange happened");
62+
ErrorWindow w = new ErrorWindow(e, "CUSTOM ERROR MESSAGE", i18n);
63+
w.open();
64+
}
65+
});
66+
5267
Checkbox productionModeCb = new Checkbox("Production Mode");
5368
productionModeCb.addValueChangeListener(e -> {
5469
System.setProperty("productionMode", "" + productionModeCb.getValue().toString());
5570
Notification.show("Currently production mode is: " + System.getProperty("productionMode"));
5671
});
5772

5873
setSizeFull();
59-
add(productionModeCb, errorButton, throwErrorWithoutErrorHandler, throwErrorWithCustomMessage);
74+
add(productionModeCb, errorButton, throwErrorWithoutErrorHandler, throwErrorWithCustomMessageAndCustomTexts, throwErrorWithCustomMessage);
6075
}
6176

6277
}

0 commit comments

Comments
 (0)