|
46 | 46 | @SuppressWarnings("serial") |
47 | 47 | public class ErrorWindow extends Dialog { |
48 | 48 |
|
49 | | - private static final Logger logger = LoggerFactory.getLogger(ErrorWindow.class); |
| 49 | + private static final Logger logger = LoggerFactory.getLogger(ErrorWindow.class); |
50 | 50 |
|
51 | | - private static final String DEFAULT_CAPTION = "<h3 style='margin-top:0px;text-align:center'>An error has occurred</h3>"; |
| 51 | + private static final String DEFAULT_CAPTION = "<h3 style='margin-top:0px;text-align:center'>An error has occurred</h3>"; |
52 | 52 |
|
53 | | - private static final String DEFAULT_ERROR_LABEL_MESSAGE = "Please contact the system administrator for more information."; |
| 53 | + private static final String DEFAULT_ERROR_LABEL_MESSAGE = "Please contact the system administrator for more information."; |
54 | 54 |
|
55 | | - private VerticalLayout exceptionTraceLayout; |
| 55 | + private VerticalLayout exceptionTraceLayout; |
56 | 56 |
|
57 | | - private final Throwable cause; |
| 57 | + private final Throwable cause; |
58 | 58 |
|
59 | | - private final String errorMessage; |
| 59 | + private final String errorMessage; |
60 | 60 |
|
61 | | - private final String uuid; |
| 61 | + private final String uuid; |
62 | 62 |
|
63 | 63 | private boolean productionMode; |
64 | 64 |
|
65 | | - public ErrorWindow(final Throwable cause) { |
66 | | - this(cause, null, false); |
67 | | - } |
68 | | - |
69 | | - public ErrorWindow(final Throwable cause, final String errorMessage) { |
70 | | - this(cause, errorMessage, false); |
71 | | - } |
72 | | - |
73 | | - public ErrorWindow(final Throwable cause, final String errorMessage, boolean productionMode) { |
74 | | - super(); |
75 | | - |
76 | | - uuid = UUID.randomUUID().toString(); |
77 | | - this.cause = cause; |
78 | | - this.errorMessage = errorMessage; |
79 | | - this.productionMode = productionMode; |
80 | | - initWindow(); |
81 | | - } |
82 | | - |
83 | | - public ErrorWindow(ErrorDetails errorDetails, boolean productionMode) { |
84 | | - this(errorDetails.getThrowable(),errorDetails.getCause(),productionMode); |
85 | | - } |
86 | | - |
87 | | - private void initWindow() { |
88 | | - if (logger.isErrorEnabled()) { |
89 | | - logger.error(String.format("Error occurred %s", uuid), cause); |
90 | | - } |
91 | | - setWidth("800px"); |
92 | | - setCloseOnEsc(true); |
93 | | - add(createMainLayout()); |
94 | | - } |
95 | | - |
96 | | - /** |
97 | | - * Creates the main layout of the ErrorWindow. |
98 | | - */ |
99 | | - private VerticalLayout createMainLayout() { |
100 | | - final VerticalLayout mainLayout = new VerticalLayout(); |
101 | | - mainLayout.setSpacing(false); |
102 | | - mainLayout.setPadding(false); |
103 | | - mainLayout.setMargin(false); |
104 | | - |
105 | | - final Html title = new Html(DEFAULT_CAPTION); |
106 | | - title.getElement().getStyle().set("width", "100%"); |
107 | | - mainLayout.add(title); |
108 | | - |
109 | | - final Html errorLabel = createErrorLabel(); |
110 | | - mainLayout.add(errorLabel); |
111 | | - mainLayout.setHorizontalComponentAlignment(Alignment.START,errorLabel); |
112 | | - |
113 | | - HorizontalLayout buttonsLayout = new HorizontalLayout(); |
114 | | - buttonsLayout.setSpacing(true); |
115 | | - buttonsLayout.setPadding(false); |
116 | | - buttonsLayout.setMargin(false); |
117 | | - |
118 | | - if (!productionMode) { |
119 | | - Button button = createDetailsButtonLayout(); |
120 | | - buttonsLayout.add(button); |
121 | | - mainLayout.add(createExceptionTraceLayout()); |
122 | | - } |
123 | | - |
124 | | - final Button closeButton = new Button("Close", event -> close()); |
125 | | - buttonsLayout.add(closeButton); |
126 | | - mainLayout.add(buttonsLayout); |
127 | | - mainLayout.setHorizontalComponentAlignment(Alignment.END, buttonsLayout); |
128 | | - |
129 | | - return mainLayout; |
130 | | - } |
131 | | - |
132 | | - private Button createDetailsButtonLayout() { |
133 | | - final Button errorDetailsButton = new Button("Show error detail", event -> { |
134 | | - boolean visible = !exceptionTraceLayout.isVisible(); |
135 | | - exceptionTraceLayout.setVisible(visible); |
136 | | - if(visible) { |
137 | | - event.getSource().setIcon(VaadinIcon.MINUS.create()); |
138 | | - } else { |
139 | | - event.getSource().setIcon(VaadinIcon.PLUS.create()); |
140 | | - } |
141 | | - }); |
142 | | - errorDetailsButton.setIcon(VaadinIcon.PLUS.create()); |
143 | | - return errorDetailsButton; |
144 | | - } |
145 | | - |
146 | | - |
147 | | - private VerticalLayout createExceptionTraceLayout() { |
148 | | - exceptionTraceLayout = new VerticalLayout(); |
149 | | - exceptionTraceLayout.setSpacing(false); |
150 | | - exceptionTraceLayout.setMargin(false); |
151 | | - exceptionTraceLayout.setPadding(false); |
152 | | - exceptionTraceLayout.add(createStackTraceArea()); |
153 | | - exceptionTraceLayout.setVisible(false); |
154 | | - return exceptionTraceLayout; |
155 | | - } |
156 | | - |
157 | | - protected TextArea createStackTraceArea() { |
158 | | - final TextArea area = new TextArea(); |
159 | | - area.setWidthFull(); |
160 | | - area.setHeight("15em"); |
161 | | - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
162 | | - final PrintWriter pw = new PrintWriter(baos); |
163 | | - cause.printStackTrace(pw); |
164 | | - pw.flush(); |
165 | | - area.setValue(baos.toString()); |
166 | | - return area; |
167 | | - } |
168 | | - |
169 | | - protected Html createErrorLabel() { |
170 | | - String label = errorMessage == null ? DEFAULT_ERROR_LABEL_MESSAGE : errorMessage; |
171 | | - if (productionMode) { |
172 | | - label = label.concat(String.format("<br />Please report the following code to system administrator:<h4><p><center>%s<center/></p></h4>", uuid)); |
173 | | - } |
174 | | - final Html errorLabel = new Html("<span>" + label + "</span>"); |
175 | | - errorLabel.getElement().getStyle().set("width", "100%"); |
176 | | - return errorLabel; |
177 | | - } |
| 65 | + public ErrorWindow(final Throwable cause) { |
| 66 | + this(cause, null, false); |
| 67 | + } |
| 68 | + |
| 69 | + public ErrorWindow(final Throwable cause, final String errorMessage) { |
| 70 | + this(cause, errorMessage, false); |
| 71 | + } |
| 72 | + |
| 73 | + public ErrorWindow(final Throwable cause, final String errorMessage, boolean productionMode) { |
| 74 | + super(); |
| 75 | + |
| 76 | + uuid = UUID.randomUUID().toString(); |
| 77 | + this.cause = cause; |
| 78 | + this.errorMessage = errorMessage; |
| 79 | + this.productionMode = productionMode; |
| 80 | + initWindow(); |
| 81 | + } |
| 82 | + |
| 83 | + public ErrorWindow(ErrorDetails errorDetails, boolean productionMode) { |
| 84 | + this(errorDetails.getThrowable(),errorDetails.getCause(),productionMode); |
| 85 | + } |
| 86 | + |
| 87 | + private void initWindow() { |
| 88 | + if (logger.isErrorEnabled()) { |
| 89 | + logger.error(String.format("Error occurred %s", uuid), cause); |
| 90 | + } |
| 91 | + setWidth("800px"); |
| 92 | + setCloseOnEsc(true); |
| 93 | + add(createMainLayout()); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Creates the main layout of the ErrorWindow. |
| 98 | + */ |
| 99 | + private VerticalLayout createMainLayout() { |
| 100 | + final VerticalLayout mainLayout = new VerticalLayout(); |
| 101 | + mainLayout.setSpacing(false); |
| 102 | + mainLayout.setPadding(false); |
| 103 | + mainLayout.setMargin(false); |
| 104 | + |
| 105 | + final Html title = new Html(DEFAULT_CAPTION); |
| 106 | + title.getElement().getStyle().set("width", "100%"); |
| 107 | + mainLayout.add(title); |
| 108 | + |
| 109 | + final Html errorLabel = createErrorLabel(); |
| 110 | + mainLayout.add(errorLabel); |
| 111 | + mainLayout.setHorizontalComponentAlignment(Alignment.START,errorLabel); |
| 112 | + |
| 113 | + HorizontalLayout buttonsLayout = new HorizontalLayout(); |
| 114 | + buttonsLayout.setSpacing(true); |
| 115 | + buttonsLayout.setPadding(false); |
| 116 | + buttonsLayout.setMargin(false); |
| 117 | + |
| 118 | + if (!productionMode) { |
| 119 | + Button button = createDetailsButtonLayout(); |
| 120 | + buttonsLayout.add(button); |
| 121 | + mainLayout.add(createExceptionTraceLayout()); |
| 122 | + } |
| 123 | + |
| 124 | + final Button closeButton = new Button("Close", event -> close()); |
| 125 | + buttonsLayout.add(closeButton); |
| 126 | + mainLayout.add(buttonsLayout); |
| 127 | + mainLayout.setHorizontalComponentAlignment(Alignment.END, buttonsLayout); |
| 128 | + |
| 129 | + return mainLayout; |
| 130 | + } |
| 131 | + |
| 132 | + private Button createDetailsButtonLayout() { |
| 133 | + final Button errorDetailsButton = new Button("Show error detail", event -> { |
| 134 | + boolean visible = !exceptionTraceLayout.isVisible(); |
| 135 | + exceptionTraceLayout.setVisible(visible); |
| 136 | + if(visible) { |
| 137 | + event.getSource().setIcon(VaadinIcon.MINUS.create()); |
| 138 | + } else { |
| 139 | + event.getSource().setIcon(VaadinIcon.PLUS.create()); |
| 140 | + } |
| 141 | + }); |
| 142 | + errorDetailsButton.setIcon(VaadinIcon.PLUS.create()); |
| 143 | + return errorDetailsButton; |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + private VerticalLayout createExceptionTraceLayout() { |
| 148 | + exceptionTraceLayout = new VerticalLayout(); |
| 149 | + exceptionTraceLayout.setSpacing(false); |
| 150 | + exceptionTraceLayout.setMargin(false); |
| 151 | + exceptionTraceLayout.setPadding(false); |
| 152 | + exceptionTraceLayout.add(createStackTraceArea()); |
| 153 | + exceptionTraceLayout.setVisible(false); |
| 154 | + return exceptionTraceLayout; |
| 155 | + } |
| 156 | + |
| 157 | + protected TextArea createStackTraceArea() { |
| 158 | + final TextArea area = new TextArea(); |
| 159 | + area.setWidthFull(); |
| 160 | + area.setHeight("15em"); |
| 161 | + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 162 | + final PrintWriter pw = new PrintWriter(baos); |
| 163 | + cause.printStackTrace(pw); |
| 164 | + pw.flush(); |
| 165 | + area.setValue(baos.toString()); |
| 166 | + return area; |
| 167 | + } |
| 168 | + |
| 169 | + protected Html createErrorLabel() { |
| 170 | + String label = errorMessage == null ? DEFAULT_ERROR_LABEL_MESSAGE : errorMessage; |
| 171 | + if (productionMode) { |
| 172 | + label = label.concat(String.format("<br />Please report the following code to system administrator:<h4><p><center>%s<center/></p></h4>", uuid)); |
| 173 | + } |
| 174 | + final Html errorLabel = new Html("<span>" + label + "</span>"); |
| 175 | + errorLabel.getElement().getStyle().set("width", "100%"); |
| 176 | + return errorLabel; |
| 177 | + } |
178 | 178 |
|
179 | 179 | } |
0 commit comments