Skip to content

Commit 5d2dc69

Browse files
committed
Review changes.
- Added tests for ExceptionWrapper - Added tracktrace information - Fixed possble memory leak - Added resizable property to the popup - Refactoring
1 parent 3a7fb39 commit 5d2dc69

File tree

5 files changed

+75
-28
lines changed

5 files changed

+75
-28
lines changed

ui/jetbrains/src/main/java/com/neueda/jetbrains/plugin/graphdb/jetbrains/ui/console/log/LogPanel.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,12 @@
3030
import static com.neueda.jetbrains.plugin.graphdb.jetbrains.ui.console.event.QueryParametersRetrievalErrorEvent.PARAMS_ERROR_COMMON_MSG;
3131
import static com.neueda.jetbrains.plugin.graphdb.jetbrains.ui.datasource.interactions.DataSourceDialog.HEIGHT;
3232
import static com.neueda.jetbrains.plugin.graphdb.jetbrains.ui.datasource.interactions.DataSourceDialog.THICKNESS;
33-
import static com.neueda.jetbrains.plugin.graphdb.jetbrains.util.ExceptionWrapper.SHORT_STRING_LENGTH;
34-
import static com.neueda.jetbrains.plugin.graphdb.jetbrains.util.ExceptionWrapper.getCause;
35-
import static com.neueda.jetbrains.plugin.graphdb.jetbrains.util.ExceptionWrapper.truncateString;
36-
import static com.neueda.jetbrains.plugin.graphdb.jetbrains.util.ExceptionWrapper.wrapExceptionInMeaningMessage;
33+
import static com.neueda.jetbrains.plugin.graphdb.jetbrains.util.ExceptionWrapper.*;
3734

3835
public class LogPanel implements Disposable {
3936
private static final String SHOW_DETAILS = "Details...";
4037

4138
private ConsoleView log;
42-
private Map<String, String> exceptions = new HashMap<>();
43-
private Map<String, String> causes = new HashMap<>();
4439

4540
public void initialize(GraphConsoleView graphConsoleView, Project project) {
4641
MessageBus messageBus = project.getMessageBus();
@@ -123,6 +118,7 @@ public void metadataRefreshSucceed(DataSourceApi nodeDataSource, DataSourceMetad
123118

124119
@Override
125120
public void metadataRefreshFailed(DataSourceApi nodeDataSource, Exception exception) {
121+
Map<String, String> exceptions = new HashMap<>();
126122
String prefix = String.format("DataSource[%s] - metadata refresh failed. Reason: ", nodeDataSource.getName());
127123
error(prefix);
128124
String errorMessage = prefix + printException(exception) + "\n";
@@ -168,8 +164,9 @@ private String printException(Exception exception) {
168164
errorMessage = exception.toString();
169165
}
170166
error(errorMessage);
171-
String details = exception.getMessage() + '\n' + getCause(exception);
172-
log.printHyperlink(" " + SHOW_DETAILS, p -> showPopup("Error details", details));
167+
String newLine = "\n";
168+
String details = exception.getMessage() + newLine + getCause(exception) + newLine + getStackTrace(exception);
169+
log.printHyperlink(" " + SHOW_DETAILS, p -> showPopup("Error details", details, exception));
173170
newLine();
174171
return errorMessage;
175172
}
@@ -178,14 +175,14 @@ private void newLine() {
178175
log.print("\n", ConsoleViewContentType.NORMAL_OUTPUT);
179176
}
180177

181-
private void showPopup(String title, String details) {
178+
private void showPopup(String title, String details, Exception exception) {
182179
JPanel popupPanel = new JPanel(new BorderLayout());
183180
popupPanel.setBorder(JBUI.Borders.empty(THICKNESS));
184181

185182
JTextArea exceptionDetails = new JTextArea();
186183
exceptionDetails.setLineWrap(false);
187184
exceptionDetails.append(details);
188-
JLabel jLabel = new JLabel(truncateString(details, SHORT_STRING_LENGTH), AllIcons.Process.State.RedExcl, JLabel.LEFT);
185+
JLabel jLabel = new JLabel(wrapExceptionInMeaningMessage(exception), AllIcons.Process.State.RedExcl, JLabel.LEFT);
189186

190187
JBScrollPane scrollPane = new JBScrollPane(exceptionDetails);
191188
scrollPane.setPreferredSize(new Dimension(-1, HEIGHT));
@@ -197,6 +194,8 @@ private void showPopup(String title, String details) {
197194
popupPanel,
198195
log.getComponent())
199196
.setTitle(title)
197+
.setResizable(true)
198+
.setMovable(true)
200199
.setCancelButton(new IconButton("Close", AllIcons.Actions.Close, AllIcons.Actions.CloseHovered))
201200
.createPopup()
202201
.showInFocusCenter();

ui/jetbrains/src/main/java/com/neueda/jetbrains/plugin/graphdb/jetbrains/ui/datasource/interactions/DataSourceDialog.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
import javax.swing.*;
2323
import java.awt.*;
2424

25-
import static com.neueda.jetbrains.plugin.graphdb.jetbrains.util.ExceptionWrapper.SHORT_STRING_LENGTH;
26-
import static com.neueda.jetbrains.plugin.graphdb.jetbrains.util.ExceptionWrapper.getCause;
27-
import static com.neueda.jetbrains.plugin.graphdb.jetbrains.util.ExceptionWrapper.truncateString;
25+
import static com.neueda.jetbrains.plugin.graphdb.jetbrains.util.ExceptionWrapper.*;
2826

2927
public abstract class DataSourceDialog extends DialogWrapper {
3028
public static final int THICKNESS = 10;
@@ -68,6 +66,8 @@ private void createPopup(JPanel popupPanel, JComponent contentPanel) {
6866
.createComponentPopupBuilder(popupPanel, getPreferredFocusedComponent())
6967
.setCancelButton(new IconButton("Close", AllIcons.Actions.Close))
7068
.setTitle("Test connection")
69+
.setResizable(true)
70+
.setMovable(true)
7171
.setCancelButton(new IconButton("Close", AllIcons.Actions.Close, AllIcons.Actions.CloseHovered))
7272
.createPopup()
7373
.showInCenterOf(contentPanel);
@@ -119,7 +119,7 @@ private void connectionFailed(
119119
hideLoading();
120120

121121
JLabel connectionFailed = new JLabel("Connection failed: " +
122-
truncateString(exception.getMessage(), SHORT_STRING_LENGTH), AllIcons.Process.State.RedExcl, JLabel.LEFT);
122+
wrapExceptionInMeaningMessage(exception), AllIcons.Process.State.RedExcl, JLabel.LEFT);
123123

124124
JTextArea exceptionCauses = new JTextArea();
125125
exceptionCauses.setLineWrap(false);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.neueda.jetbrains.plugin.graphdb.jetbrains.util;
2+
3+
public enum ExceptionErrorMessages {
4+
SYNTAX_EXCEPTION("Please note that Cypher query is translated to Gremlin and may fail" +
5+
"because of translation or database specifics. Make sure that flavor is properly configured in database connection configuration."),
6+
SERIALIZER_EXCEPTION("Wrong serializer selected. Please check connection configuration."),
7+
RESPONSE_EXCEPTION("Database connection failed. Please check database configuration and retry to connect."),
8+
CONNECTION_EXCEPTION("Database connection failed. Please check database configuration and retry to connect.");
9+
10+
private final String description;
11+
12+
ExceptionErrorMessages(String description) {
13+
this.description = description;
14+
}
15+
16+
public String getDescription() {
17+
return description;
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return description;
23+
}
24+
}

ui/jetbrains/src/main/java/com/neueda/jetbrains/plugin/graphdb/jetbrains/util/ExceptionWrapper.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package com.neueda.jetbrains.plugin.graphdb.jetbrains.util;
22

3-
import org.apache.commons.lang.WordUtils;
3+
import java.io.PrintWriter;
4+
import java.io.StringWriter;
45

56
public class ExceptionWrapper {
6-
public static final int SHORT_STRING_LENGTH = 140;
7+
public static final int SHORT_STRING_LENGTH = 150;
78
private static final String NON_THIN_CHARS = "[^iIl1\\.,']";
89

910
private static int textWidth(String str) {
1011
return str.length() - str.replaceAll(NON_THIN_CHARS, "").length() / 2;
1112
}
1213

13-
public static String truncateString(String text, int targetLength) {
14+
public static String ellipseString(String text, int targetLength) {
1415
if (textWidth(text) <= targetLength) {
1516
return text;
1617
}
@@ -31,10 +32,6 @@ public static String truncateString(String text, int targetLength) {
3132
return text.substring(0, end) + "...";
3233
}
3334

34-
public static String wrapLongLine(String longLine) {
35-
return WordUtils.wrap(longLine, 100);
36-
}
37-
3835
public static String getCause(Exception exception) {
3936
StringBuilder exceptionCauses = new StringBuilder();
4037
Throwable cause = exception.getCause();
@@ -45,19 +42,29 @@ public static String getCause(Exception exception) {
4542
return exceptionCauses.toString();
4643
}
4744

45+
public static String getStackTrace(final Throwable throwable) {
46+
final StringWriter sw = new StringWriter();
47+
final PrintWriter pw = new PrintWriter(sw, true);
48+
throwable.printStackTrace(pw);
49+
return sw.getBuffer().toString();
50+
}
51+
4852
public static String wrapExceptionInMeaningMessage(Exception exception) {
4953
String exceptionMessage = exception.getMessage();
50-
if (exceptionMessage.contains("org.apache.tinkerpop.gremlin.driver.ser.SerializationException")) {
51-
return "Wrong serializer selected. Please check connection configuration";
54+
if (exceptionMessage.contains("SerializationException")) {
55+
return ExceptionErrorMessages.SERIALIZER_EXCEPTION.getDescription();
56+
}
57+
if (exceptionMessage.contains("ResponseException")) {
58+
return ExceptionErrorMessages.RESPONSE_EXCEPTION.getDescription();
5259
}
53-
if (exceptionMessage.contains("org.apache.tinkerpop.gremlin.driver.exception.ResponseException")) {
54-
return "Database connection failed with gremlin driver response exception. Please check database configuration.";
60+
if (exceptionMessage.contains("ConnectionException")) {
61+
return ExceptionErrorMessages.CONNECTION_EXCEPTION.getDescription();
5562
}
56-
if (exceptionMessage.contains("org.apache.tinkerpop.gremlin.driver.exception.ConnectionException")) {
57-
return "Database connection failed with gremlin driver connection exception. Please check database configuration.";
63+
if (exceptionMessage.contains("SyntaxException")) {
64+
return ExceptionErrorMessages.SYNTAX_EXCEPTION.getDescription();
5865
}
5966
if (exceptionMessage.length() > SHORT_STRING_LENGTH) {
60-
return truncateString(exceptionMessage, SHORT_STRING_LENGTH);
67+
return ellipseString(exceptionMessage, SHORT_STRING_LENGTH);
6168
}
6269
return exceptionMessage;
6370
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.neueda.jetbrains.plugin.graphdb.jetbrains.ui.helpers;
2+
3+
import com.neueda.jetbrains.plugin.graphdb.jetbrains.util.ExceptionWrapper;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class ExceptionWrapperTest {
9+
@Test
10+
public void shouldEllipseString() throws Exception {
11+
String shortString = "DataSource[Cosmos] - metadata refresh failed. Reason: java.util.concurrent.ExecutionException:org.apache.tinkerpop.gremlin.driver.exception.ResponseException:...";
12+
String longString = "DataSource[Cosmos] - metadata refresh failed. Reason: java.util.concurrent.ExecutionException:" +
13+
"org.apache.tinkerpop.gremlin.driver.exception.ResponseException: ActivityId : d0015df6-09d2-48a6-b3ab-32d026213a42";
14+
String truncatedString = ExceptionWrapper.ellipseString(longString, ExceptionWrapper.SHORT_STRING_LENGTH);
15+
assertEquals(shortString, truncatedString);
16+
}
17+
}

0 commit comments

Comments
 (0)