Skip to content

Commit 2c23f1c

Browse files
committed
docs: improve javadocs
1 parent 4dab171 commit 2c23f1c

File tree

9 files changed

+192
-9
lines changed

9 files changed

+192
-9
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: 50 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,60 @@ 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+
* @param cause: The cause of the error
82+
*/
7183
public ErrorWindow(final Throwable cause) {
7284
this(cause, null, isProductionMode(), ErrorWindowI18n.createDefault());
7385
}
7486

87+
/**
88+
* Constructs and initializes an ErrorWindow object with the supplied details and and default production mode.
89+
* @param cause: The cause of the error
90+
* @param i18n: The internationalization of the ErrorWindow
91+
*/
7592
public ErrorWindow(final Throwable cause, final ErrorWindowI18n i18n) {
7693
this(cause, null, isProductionMode(), i18n);
7794
}
7895

96+
/**
97+
* Constructs and initializes an ErrorWindow object with the supplied details and default and default production mode and i18n.
98+
* @param cause: The cause of the error
99+
* @param errorMessage: An optional error message
100+
*/
79101
public ErrorWindow(final Throwable cause, final String errorMessage) {
80102
this(cause, errorMessage, isProductionMode(), ErrorWindowI18n.createDefault());
81103
}
82104

105+
/**
106+
* Constructs and initializes an ErrorWindow object with the supplied details and default production mode.
107+
* @param cause: The cause of the error
108+
* @param errorMessage: An optional error message
109+
* @param i18n: The internationalization of the ErrorWindow
110+
*/
83111
public ErrorWindow(final Throwable cause, final String errorMessage, final ErrorWindowI18n i18n) {
84112
this(cause, errorMessage, isProductionMode(), i18n);
85113
}
86114

115+
/**
116+
* Constructs and initializes an ErrorWindow object with the supplied details and default i18n.
117+
* @param cause: The cause of the error
118+
* @param errorMessage: An optional error message
119+
* @param productionMode: The mode in which the Application is running. If true, a code is displayed with error details, else debug information is shown
120+
*/
87121
public ErrorWindow(final Throwable cause, final String errorMessage, boolean productionMode) {
88122
this(cause, errorMessage, productionMode, ErrorWindowI18n.createDefault());
89123
}
90124

91-
public ErrorWindow(
92-
final Throwable cause,
93-
final String errorMessage,
125+
/**
126+
* Constructs and initializes an ErrorWindow object with the supplied details.
127+
* @param cause: The cause of the error
128+
* @param errorMessage: An optional error message
129+
* @param productionMode: The mode in which the Application is running. If true, a code is displayed with error details, else debug information is shown
130+
* @param i18n: The internationalization of the ErrorWindow
131+
*/
132+
public ErrorWindow(final Throwable cause, final String errorMessage,
94133
boolean productionMode,
95134
final ErrorWindowI18n i18n) {
96135
super();
@@ -126,6 +165,12 @@ public ErrorWindow(ErrorDetails errorDetails, boolean productionMode) {
126165
this(errorDetails.getThrowable(), errorDetails.getCause(), productionMode);
127166
}
128167

168+
/**
169+
* Constructs and initializes an ErrorWindow object with the supplied error details and internationalization.
170+
* This constructor allows for provision of additional information, which is an ErrorDetails object.
171+
* @param errorDetails: The ErrorDetails object
172+
* @param i18n: The internationalization of the ErrorWindow
173+
*/
129174
public ErrorWindow(ErrorDetails errorDetails, final ErrorWindowI18n i18n) {
130175
this(errorDetails.getThrowable(), errorDetails.getCause(), i18n);
131176
}

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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,25 @@
2121

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

24+
/** A factory class to create an error window with internationalization support */
2425
public class I18nErrorWindowFactory implements ErrorWindowFactory {
2526

2627
private final ErrorWindowI18n errorWindowI18n;
27-
28-
I18nErrorWindowFactory(ErrorWindowI18n errorWindowI18n){
28+
29+
/**
30+
* Constructs a new I18nErrorWindowFactory instance with a given ErrorWindowI18n instance
31+
*
32+
* @param errorWindowI18n the ErrorWindowI18n instance to be used
33+
*/
34+
I18nErrorWindowFactory(ErrorWindowI18n errorWindowI18n) {
2935
this.errorWindowI18n = errorWindowI18n;
3036
}
31-
37+
38+
/**
39+
* Shows an error window with the given error details and internationalization support
40+
*
41+
* @param details the ErrorDetails instance to be shown in the error window
42+
*/
3243
@Override
3344
public void showError(ErrorDetails details) {
3445
new ErrorWindow(details, errorWindowI18n).open();

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,21 @@
3131
import com.vaadin.flow.server.startup.ApplicationRouteRegistry;
3232
import java.util.Collections;
3333

34+
/**
35+
* Implementation of {@code VaadinServiceInitListener} interface. This class adds a session init
36+
* listener to the Vaadin service, sets an error handler and registers an error view.
37+
*/
3438
public class VaadinServiceInitListenerImpl implements VaadinServiceInitListener {
3539

3640
private static final long serialVersionUID = 1L;
3741

42+
/**
43+
* Implements the {@code serviceInit} method from {@code VaadinServiceInitListener} interface.
44+
* This method sets {@code ErrorManager} as the error handler and registers {@link ErrorView} as
45+
* the default error view.
46+
*
47+
* @param event The ServiceInitEvent to be handled.
48+
*/
3849
@Override
3950
public void serviceInit(ServiceInitEvent event) {
4051
event
@@ -43,6 +54,11 @@ public void serviceInit(ServiceInitEvent event) {
4354
registerErrorView(event.getSource());
4455
}
4556

57+
/**
58+
* Registers an error view to the Vaadin service.
59+
*
60+
* @param service The Vaadin service to which the error view needs to be registered.
61+
*/
4662
private void registerErrorView(VaadinService source) {
4763
VaadinContext context = VaadinService.getCurrent().getContext();
4864
ApplicationRouteRegistry registry = ApplicationRouteRegistry.getInstance(context);

0 commit comments

Comments
 (0)