Skip to content

Commit 4fc61e3

Browse files
paodbjavier-godoy
authored andcommitted
feat: add option to copy stacktrace details to clipboard
Close #37
1 parent 7dbae75 commit 4fc61e3

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@
130130
<artifactId>javax.annotation-api</artifactId>
131131
<version>1.3.2</version>
132132
</dependency>
133+
<dependency>
134+
<groupId>org.vaadin.olli</groupId>
135+
<artifactId>clipboardhelper</artifactId>
136+
<version>1.2.0</version>
137+
</dependency>
133138
</dependencies>
134139

135140
<build>

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.vaadin.flow.component.Component;
2424
import com.vaadin.flow.component.Html;
2525
import com.vaadin.flow.component.button.Button;
26+
import com.vaadin.flow.component.button.ButtonVariant;
2627
import com.vaadin.flow.component.dependency.CssImport;
2728
import com.vaadin.flow.component.dialog.Dialog;
2829
import com.vaadin.flow.component.html.Div;
@@ -35,6 +36,7 @@
3536
import java.util.UUID;
3637
import org.slf4j.Logger;
3738
import org.slf4j.LoggerFactory;
39+
import org.vaadin.olli.ClipboardHelper;
3840

3941
/**
4042
* Component to visualize an error, caused by an exception, as a modal sub-window. <br>
@@ -145,11 +147,21 @@ private VerticalLayout createMainLayout() {
145147
mainLayout.setHorizontalComponentAlignment(Alignment.START, errorLabel);
146148

147149
HorizontalLayout buttonsLayout = new HorizontalLayout();
150+
buttonsLayout.setWidthFull();
148151
buttonsLayout.setSpacing(true);
149152
buttonsLayout.setPadding(false);
150153
buttonsLayout.setMargin(false);
151154

152155
if (!productionMode) {
156+
// copy details to clipboard button
157+
Button clipboarButton = new Button(i18n.getClipboard());
158+
clipboarButton.addThemeVariants(ButtonVariant.LUMO_TERTIARY);
159+
ClipboardHelper clipboardHelper = new ClipboardHelper(getStackTrace(), clipboarButton);
160+
buttonsLayout.add(clipboardHelper);
161+
buttonsLayout.setAlignSelf(Alignment.START, clipboardHelper);
162+
buttonsLayout.setFlexGrow(1.0, clipboardHelper);
163+
164+
// show details button
153165
Button button = createDetailsButtonLayout();
154166
buttonsLayout.add(button);
155167
mainLayout.add(createExceptionTraceLayout());
@@ -202,14 +214,18 @@ protected Component createStackTraceArea() {
202214
area.setClassName("stacktrace");
203215
area.setWidthFull();
204216
area.setHeight("15em");
217+
area.add(getStackTrace());
218+
return area;
219+
}
220+
221+
private String getStackTrace() {
205222
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
206223
final PrintWriter pw = new PrintWriter(baos);
207224
cause.printStackTrace(pw);
208225
pw.flush();
209-
area.add(baos.toString());
210-
return area;
226+
return baos.toString();
211227
}
212-
228+
213229
protected Html createErrorLabel() {
214230
String label = errorMessage == null ? i18n.getDefaultErrorMessage() : errorMessage;
215231
if (productionMode) {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ public class ErrorWindowI18n {
3333
private String close;
3434
private String details;
3535
private String defaultErrorMessage;
36+
private String clipboard;
3637

3738
private ErrorWindowI18n() {
3839
this.caption = "An error has occurred";
3940
this.instructions = "Please report the following code to your system administrator";
4041
this.close = "Close";
4142
this.details = "Show error details";
4243
this.defaultErrorMessage = "Please contact the system administrator for more information.";
44+
this.clipboard = "Copy details to clipboard";
4345
}
4446

4547
/** @return a new instance with the default messages */
@@ -102,4 +104,15 @@ public void setDefaultErrorMessage(final String defaultErrorMessage) {
102104
public String getDefaultErrorMessage() {
103105
return this.defaultErrorMessage;
104106
}
107+
108+
/** @return returns the text for the "Copy details to clipboard" button. */
109+
public String getClipboard() {
110+
return clipboard;
111+
}
112+
113+
/** Sets the text for the "Copy details to clipboard" button. */
114+
public void setClipboard(String clipboard) {
115+
this.clipboard = clipboard;
116+
}
117+
105118
}

0 commit comments

Comments
 (0)