Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wire up client to send errors to http server / Change 'shows details' button to 'report to TripleA' #4687

Merged
merged 5 commits into from
Feb 22, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 48 additions & 11 deletions game-core/src/main/java/games/strategy/debug/ErrorMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,25 @@
import java.util.logging.LogManager;
import java.util.logging.LogRecord;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import org.triplea.http.client.ServiceClient;
import org.triplea.http.client.error.report.ErrorReportClientFactory;
import org.triplea.http.client.error.report.create.ErrorReport;
import org.triplea.http.client.error.report.create.ErrorReportResponse;
import org.triplea.swing.JButtonBuilder;
import org.triplea.swing.JLabelBuilder;
import org.triplea.swing.JPanelBuilder;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;

import games.strategy.triplea.settings.ClientSetting;
import games.strategy.debug.error.reporting.StackTraceReportView;
import games.strategy.engine.lobby.client.login.LobbyPropertyFetcherConfiguration;
import games.strategy.engine.lobby.client.login.LobbyServerProperties;

/**
* Class for showing a modal error dialog to the user. The dialog has an 'ok' button to close it and a 'show details'
Expand All @@ -44,6 +51,11 @@ public enum ErrorMessage {
private final AtomicBoolean isVisible = new AtomicBoolean(false);
private volatile boolean enableErrorPopup = false;

private final JButton uploadButton = JButtonBuilder.builder()
.title("Report To TripleA")
.toolTip("Upload error report to TripleA support.")
.build();

ErrorMessage() {
windowReference.setAlwaysOnTop(true);
windowReference.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
Expand All @@ -55,7 +67,7 @@ public void windowClosing(final WindowEvent e) {
});
windowReference.add(JPanelBuilder.builder()
.borderLayout()
.borderEmpty(10)
.border(10)
.addCenter(JPanelBuilder.builder()
.horizontalBoxLayout()
.addHorizontalGlue()
Expand All @@ -64,22 +76,15 @@ public void windowClosing(final WindowEvent e) {
.build())
.addSouth(JPanelBuilder.builder()
.horizontalBoxLayout()
.borderEmpty(20, 0, 0, 0)
.border(20, 0, 0, 0)
.addHorizontalGlue()
.add(JButtonBuilder.builder()
.okTitle()
.actionListener(this::hide)
.selected(true)
.build())
.addHorizontalStrut(5)
.add(JButtonBuilder.builder()
.title("Show Details")
.toolTip("Shows the error console window with full error details.")
.actionListener(() -> {
hide();
ClientSetting.showConsole.setValueAndFlush(true);
})
.build())
.add(uploadButton)
.addHorizontalGlue()
.build())
.build());
Expand All @@ -100,6 +105,8 @@ public static void initialize() {

public static void show(final LogRecord record) {
if (INSTANCE.enableErrorPopup && INSTANCE.isVisible.compareAndSet(false, true)) {
INSTANCE.setUploadRecord(record);

SwingUtilities.invokeLater(() -> {
INSTANCE.errorMessage.setText(TextUtils.textToHtml(Strings.nullToEmpty(record.getMessage())));
INSTANCE.windowReference.pack();
Expand All @@ -109,6 +116,36 @@ public static void show(final LogRecord record) {
}
}

private void setUploadRecord(final LogRecord record) {

final ServiceClient<ErrorReport, ErrorReportResponse> serviceClient = serviceClient();

if (serviceClient == null) {
// if no internet connection, do not show 'upload button' as it will not work anyways.
INSTANCE.uploadButton.setVisible(false);
} else {
INSTANCE.uploadButton.setVisible(true);

// replace button upload action to use the new log record object
if (INSTANCE.uploadButton.getActionListeners().length > 0) {
INSTANCE.uploadButton.removeActionListener(INSTANCE.uploadButton.getActionListeners()[0]);
}
INSTANCE.uploadButton.addActionListener(e -> {
hide();
StackTraceReportView.showWindow(windowReference, serviceClient, record);
});
}
}


private static ServiceClient<ErrorReport, ErrorReportResponse> serviceClient() {
return LobbyPropertyFetcherConfiguration.lobbyServerPropertiesFetcher()
.fetchLobbyServerProperties()
.map(LobbyServerProperties::getHttpServerUri)
.map(ErrorReportClientFactory::newErrorUploader)
.orElse(null);
}

private void hide() {
windowReference.setVisible(false);
isVisible.set(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private static void doShowFailureConfirmation(final ServiceResponse<ErrorReportR
});

final JPanel messageToShow = JPanelBuilder.builder()
.borderEmpty(10)
.border(10)
.add(editorPane)
.build();

Expand All @@ -74,7 +74,7 @@ private static void doShowSuccessConfirmation(final URI reportLinkCreated) {
});

final JPanel messageToShow = JPanelBuilder.builder()
.borderEmpty(10)
.border(10)
.add(editorPane)
.build();

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
package games.strategy.debug.error.reporting;

import java.net.URI;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;

import javax.annotation.Nonnull;
import javax.swing.JFrame;

import org.triplea.http.client.SendResult;
import org.triplea.http.client.ServiceClient;
import org.triplea.http.client.ServiceResponse;
import org.triplea.http.client.error.report.create.ErrorReport;
import org.triplea.http.client.error.report.create.ErrorReportResponse;
import org.triplea.swing.DialogBuilder;

import lombok.Builder;

/**
* Strategy object to upload an error report to http server.
*/
@Builder
class ErrorReportUploadAction implements BiConsumer<JFrame, UserErrorReport> {

static final BiConsumer<JFrame, UserErrorReport> OFFLINE_STRATEGY =
(frame, report) -> DialogBuilder.builder()
.parent(frame)
.title("Unable to connect to server")
.errorMessage(
"TripleA is unable to get the servers network adddress, please restart "
+ "Triplea and try again, if this problem keeps happening please contact Triplea")
.showDialog();
class ErrorReportUploadAction implements Predicate<ErrorReport> {

@Nonnull
private final ServiceClient<ErrorReport, ErrorReportResponse> serviceClient;
Expand All @@ -37,18 +29,17 @@ class ErrorReportUploadAction implements BiConsumer<JFrame, UserErrorReport> {


@Override
public void accept(final JFrame frame, final UserErrorReport errorReport) {
final ServiceResponse<ErrorReportResponse> response = serviceClient.apply(errorReport.toErrorReport());
public boolean test(final ErrorReport errorReport) {
final ServiceResponse<ErrorReportResponse> response = serviceClient.apply(errorReport);
final URI githubLink =
response.getPayload().map(pay -> pay.getGithubIssueLink().orElse(null)).orElse(null);

if ((response.getSendResult() == SendResult.SENT) && (githubLink != null)) {
successConfirmation.accept(githubLink);
frame.dispose();
return true;
} else {
failureConfirmation.accept(response);
// We close the frame on success, but not on failure.
// This is so the user can recover any data they have typed.
return false;
}
}
}
Loading