Skip to content

Commit 721af85

Browse files
committed
docs: improve javadocs
1 parent 4dab171 commit 721af85

File tree

9 files changed

+203
-14
lines changed

9 files changed

+203
-14
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,18 @@
2121

2222
package com.flowingcode.vaadin.addons.errorwindow;
2323

24+
/**
25+
* Default implementation of {@link ErrorWindowFactory}. Displays error details using an {@link
26+
* ErrorWindow}.
27+
*/
2428
public class DefaultErrorWindowFactory implements ErrorWindowFactory {
2529

30+
/**
31+
* Displays the error details creating a new instance of {@link ErrorWindow}. The error window is
32+
* displayed on the screen by invoking {@link ErrorWindow#open()}.
33+
*
34+
* @param details the error details to display
35+
*/
2636
@Override
2737
public void showError(ErrorDetails details) {
2838
ErrorWindow w = new ErrorWindow(details);

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,56 @@
2121

2222
package com.flowingcode.vaadin.addons.errorwindow;
2323

24+
/** Encapsulates error details, including the throwable object and the cause of the error. */
2425
public class ErrorDetails {
2526

2627
private Throwable throwable;
2728
private String cause;
2829

30+
/**
31+
* Constructs a new ErrorDetails object with the given throwable and cause.
32+
*
33+
* @param throwable the throwable object that caused the error
34+
* @param cause the cause of the error
35+
*/
2936
public ErrorDetails(Throwable throwable, String cause) {
3037
super();
3138
this.throwable = throwable;
3239
this.cause = cause;
3340
}
3441

42+
/**
43+
* Retrieves the throwable object that caused the error.
44+
*
45+
* @return the throwable object that caused the error
46+
*/
3547
public Throwable getThrowable() {
3648
return throwable;
3749
}
3850

51+
/**
52+
* Sets the throwable object that caused the error.
53+
*
54+
* @param throwable the throwable object that caused the error
55+
*/
3956
public void setThrowable(Throwable throwable) {
4057
this.throwable = throwable;
4158
}
4259

60+
/**
61+
* Retrieves the cause of the error.
62+
*
63+
* @return the cause of the error
64+
*/
4365
public String getCause() {
4466
return cause;
4567
}
4668

69+
/**
70+
* Sets the cause of the error.
71+
*
72+
* @param cause the cause of the error
73+
*/
4774
public void setCause(String cause) {
4875
this.cause = cause;
4976
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import java.util.Optional;
2727
import java.util.concurrent.ConcurrentHashMap;
2828

29+
/**
30+
* Manages errors and displays error messages through different ErrorWindowFactory implementations.
31+
*/
2932
public final class ErrorManager {
3033

3134
private static final Map<Class<?>, ErrorWindowFactory> factories = new ConcurrentHashMap<>();
@@ -46,29 +49,64 @@ private ErrorManager() {
4649
throw new IllegalStateException("Utility class not meant to be instantiated");
4750
}
4851

52+
/**
53+
* Displays an error message for the given Throwable.
54+
*
55+
* @param throwable the Throwable object for which the error message should be displayed
56+
*/
4957
public static void showError(Throwable throwable) {
5058
showError(throwable, throwable.getLocalizedMessage());
5159
}
5260

61+
/**
62+
* Displays an error message for the given Throwable with the specified cause message.
63+
*
64+
* @param throwable the Throwable object for which the error message should be displayed
65+
* @param cause the cause for the error
66+
*/
5367
public static void showError(Throwable throwable, String cause) {
5468
ErrorDetails details = new ErrorDetails(throwable, cause);
5569
getErrorWindowFactory(throwable.getClass()).showError(details);
5670
}
5771

72+
/**
73+
* Sets the ErrorWindowFactory for the specified Throwable class.
74+
*
75+
* @param clazz the class of Throwable for which the ErrorWindowFactory should be set
76+
* @param errorWindowFactory the ErrorWindowFactory implementation to be set for the specified
77+
* class
78+
*/
5879
public static void setErrorWindowFactory(Class<? extends Throwable> clazz,
5980
ErrorWindowFactory errorWindowFactory) {
6081
factories.put(clazz.asSubclass(Throwable.class), errorWindowFactory);
6182
}
6283

84+
/**
85+
* Gets the ErrorWindowFactory implementation for the specified class or its super classes.
86+
*
87+
* @param clazz the class for which the ErrorWindowFactory implementation is needed
88+
* @return the ErrorWindowFactory implementation associated with the specified class or its super
89+
* classes
90+
*/
6391
public static ErrorWindowFactory getErrorWindowFactory(Class<?> clazz) {
6492
return Optional.ofNullable(factories.get(clazz))
6593
.orElseGet(() -> getErrorWindowFactory(clazz.getSuperclass()));
6694
}
6795

96+
/**
97+
* Sets the default ErrorWindowFactory implementation.
98+
*
99+
* @param errorWindowFactory the default ErrorWindowFactory implementation to be set
100+
*/
68101
public static void setErrorWindowFactory(ErrorWindowFactory errorWindowFactory) {
69102
factories.put(Throwable.class, errorWindowFactory);
70103
}
71104

105+
/**
106+
* Gets the default ErrorWindowFactory implementation.
107+
*
108+
* @return the default ErrorWindowFactory implementation
109+
*/
72110
public static ErrorWindowFactory getErrorWindowFactory() {
73111
return getErrorWindowFactory(Throwable.class);
74112
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,21 @@
2727
import com.vaadin.flow.router.internal.DefaultErrorHandler;
2828
import org.apache.http.HttpStatus;
2929

30+
/** View used to display an error message when navigation fails due to an exception. */
3031
@SuppressWarnings("serial")
3132
@DefaultErrorHandler
3233
@javax.annotation.security.PermitAll
3334
@jakarta.annotation.security.PermitAll
3435
public class ErrorView extends VerticalLayout implements HasErrorParameter<Exception> {
3536

37+
/**
38+
* This method sets the error parameter to display the error message and returns an HTTP status
39+
* code of 500 Internal Server Error.
40+
*
41+
* @param event a {@code BeforeEnterEvent} object
42+
* @param parameter an {@code ErrorParameter} object containing the caught exception
43+
* @return an int representing the HTTP status code 500 Internal Server Error
44+
*/
3645
@Override
3746
public int setErrorParameter(BeforeEnterEvent event, ErrorParameter<Exception> parameter) {
3847
setSizeFull();

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

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,18 @@
4242
import org.slf4j.LoggerFactory;
4343

4444
/**
45-
* Component to visualize an error, caused by an exception, as a modal sub-window. <br>
46-
* When in production mode it shows a code to report. <br>
45+
* Component to visualize an error, caused by an exception, as a modal sub-window.
46+
* When in production mode it shows a code to report.
4747
* When in debug mode it allows to visualize the stack trace of the error.
4848
*
49+
* <p>This class has several constructors that allow specifying the cause of the error, an error message, whether the application is in production mode,
50+
* and the internationalization of the error message.</p>
51+
*
52+
* <p>This class also provides constructors that accept an {@link ErrorDetails} instance, which provides additional information about the error.</p>
53+
*
54+
* <p>A unique identifier is assigned to the dialog window, for use in reporting the error when in production mode.</p>
55+
*
56+
* @see ErrorDetails
4957
* @author pbartolo
5058
*/
5159
@SuppressWarnings("serial")
@@ -68,29 +76,66 @@ public class ErrorWindow extends Dialog {
6876

6977
private Registration attachListenerRegistration;
7078

79+
/**
80+
* Constructs and initializes an ErrorWindow object with the supplied cause and default production mode and i18n.
81+
*
82+
* @param cause: The cause of the error
83+
*/
7184
public ErrorWindow(final Throwable cause) {
7285
this(cause, null, isProductionMode(), ErrorWindowI18n.createDefault());
7386
}
7487

88+
/**
89+
* Constructs and initializes an ErrorWindow object with the supplied details and and default production mode.
90+
*
91+
* @param cause: The cause of the error
92+
* @param i18n: The internationalization of the ErrorWindow
93+
*/
7594
public ErrorWindow(final Throwable cause, final ErrorWindowI18n i18n) {
7695
this(cause, null, isProductionMode(), i18n);
7796
}
7897

98+
/**
99+
* Constructs and initializes an ErrorWindow object with the supplied details and default and default production mode and i18n.
100+
*
101+
* @param cause: The cause of the error
102+
* @param errorMessage: An optional error message
103+
*/
79104
public ErrorWindow(final Throwable cause, final String errorMessage) {
80105
this(cause, errorMessage, isProductionMode(), ErrorWindowI18n.createDefault());
81106
}
82107

108+
/**
109+
* Constructs and initializes an ErrorWindow object with the supplied details and default production mode.
110+
*
111+
* @param cause: The cause of the error
112+
* @param errorMessage: An optional error message
113+
* @param i18n: The internationalization of the ErrorWindow
114+
*/
83115
public ErrorWindow(final Throwable cause, final String errorMessage, final ErrorWindowI18n i18n) {
84116
this(cause, errorMessage, isProductionMode(), i18n);
85117
}
86118

119+
/**
120+
* Constructs and initializes an ErrorWindow object with the supplied details and default i18n.
121+
*
122+
* @param cause: The cause of the error
123+
* @param errorMessage: An optional error message
124+
* @param productionMode: The mode in which the Application is running. If true, a code is displayed with error details, else debug information is shown
125+
*/
87126
public ErrorWindow(final Throwable cause, final String errorMessage, boolean productionMode) {
88127
this(cause, errorMessage, productionMode, ErrorWindowI18n.createDefault());
89128
}
90129

91-
public ErrorWindow(
92-
final Throwable cause,
93-
final String errorMessage,
130+
/**
131+
* Constructs and initializes an ErrorWindow object with the supplied details.
132+
*
133+
* @param cause: The cause of the error
134+
* @param errorMessage: An optional error message
135+
* @param productionMode: The mode in which the Application is running. If true, a code is displayed with error details, else debug information is shown
136+
* @param i18n: The internationalization of the ErrorWindow
137+
*/
138+
public ErrorWindow(final Throwable cause, final String errorMessage,
94139
boolean productionMode,
95140
final ErrorWindowI18n i18n) {
96141
super();
@@ -126,6 +171,13 @@ public ErrorWindow(ErrorDetails errorDetails, boolean productionMode) {
126171
this(errorDetails.getThrowable(), errorDetails.getCause(), productionMode);
127172
}
128173

174+
/**
175+
* Constructs and initializes an ErrorWindow object with the supplied error details and internationalization.
176+
* This constructor allows for provision of additional information, which is an ErrorDetails object.
177+
*
178+
* @param errorDetails: The ErrorDetails object
179+
* @param i18n: The internationalization of the ErrorWindow
180+
*/
129181
public ErrorWindow(ErrorDetails errorDetails, final ErrorWindowI18n i18n) {
130182
this(errorDetails.getThrowable(), errorDetails.getCause(), i18n);
131183
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,21 @@
2121

2222
package com.flowingcode.vaadin.addons.errorwindow;
2323

24+
/** A factory interface for creating error windows. */
2425
public interface ErrorWindowFactory {
2526

27+
/**
28+
* Shows the error details on a new window.
29+
*
30+
* @param details the details of the error to be shown
31+
*/
2632
void showError(ErrorDetails details);
2733

34+
/**
35+
* Checks whether the application is in production mode.
36+
*
37+
* @return true if the application is in production mode, false otherwise
38+
*/
2839
default boolean isProductionMode() {
2940
return ("true".equals(System.getProperty("productionMode")));
3041
}

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
/**
2727
* Internationalization object for customizing the component UI texts. An instance with the default
28-
* messages can be obtained using {@link ErrorWindowI18n#createDefault()}
28+
* messages can be obtained using {@link ErrorWindowI18n#createDefault()}.
2929
*
3030
3131
*/
@@ -38,6 +38,7 @@ public class ErrorWindowI18n {
3838
private String defaultErrorMessage;
3939
private String clipboard;
4040

41+
/** Constructor for creating the default instance of the object. */
4142
private ErrorWindowI18n() {
4243
this.caption = "An error has occurred";
4344
this.instructions = "Please report the following code to your system administrator";
@@ -47,6 +48,11 @@ private ErrorWindowI18n() {
4748
this.clipboard = "Copy details to clipboard";
4849
}
4950

51+
/**
52+
* Constructor for creating an instance of the object from a message translator function.
53+
*
54+
* @param i18nTranslator a message translation function
55+
*/
5056
private ErrorWindowI18n(SerializableFunction<String, String> i18nTranslator) {
5157
this.caption = i18nTranslator.apply("com.flowingcode.vaadin.addons.errorwindow.caption");
5258
this.instructions =
@@ -58,11 +64,21 @@ private ErrorWindowI18n(SerializableFunction<String, String> i18nTranslator) {
5864
this.clipboard = i18nTranslator.apply("com.flowingcode.vaadin.addons.errorwindow.clipboard");
5965
}
6066

61-
/** @return a new instance with the default messages */
67+
/**
68+
* Creates a new instance with the default messages.
69+
*
70+
* @return a new instance with the default messages
71+
*/
6272
public static ErrorWindowI18n createDefault() {
6373
return new ErrorWindowI18n();
6474
}
6575

76+
/**
77+
* Creates a new instance with the provided message translation function.
78+
*
79+
* @param i18nTranslator a message translation function
80+
* @return a new instance with the provided message translation function
81+
*/
6682
public static ErrorWindowI18n create(SerializableFunction<String, String> i18nTranslator) {
6783
return new ErrorWindowI18n(i18nTranslator);
6884
}
@@ -102,7 +118,7 @@ public void setDetails(final String details) {
102118
this.details = details;
103119
}
104120

105-
/** @return returns the text of the "Details"-Button. */
121+
/** @return returns the text of the "Details"-Button */
106122
public String getDetails() {
107123
return this.details;
108124
}
@@ -117,13 +133,13 @@ public void setDefaultErrorMessage(final String defaultErrorMessage) {
117133

118134
/**
119135
* @return returns the default error message shown if the message passed to the error window is
120-
* <code>null</code>.
136+
* <code>null</code>
121137
*/
122138
public String getDefaultErrorMessage() {
123139
return this.defaultErrorMessage;
124140
}
125141

126-
/** @return returns the text for the "Copy details to clipboard" button. */
142+
/** @return returns the text for the "Copy details to clipboard" button */
127143
public String getClipboard() {
128144
return clipboard;
129145
}
@@ -132,5 +148,4 @@ public String getClipboard() {
132148
public void setClipboard(String clipboard) {
133149
this.clipboard = clipboard;
134150
}
135-
136151
}

0 commit comments

Comments
 (0)