Home | History | Annotate | Download | only in errorReporting
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package org.jf.smalidea.errorReporting;
     18 
     19 import com.intellij.diagnostic.IdeErrorsDialog;
     20 import com.intellij.diagnostic.LogMessageEx;
     21 import com.intellij.diagnostic.ReportMessages;
     22 import com.intellij.errorreport.bean.ErrorBean;
     23 import com.intellij.ide.DataManager;
     24 import com.intellij.ide.plugins.IdeaPluginDescriptor;
     25 import com.intellij.ide.plugins.PluginManager;
     26 import com.intellij.idea.IdeaLogger;
     27 import com.intellij.notification.NotificationListener;
     28 import com.intellij.notification.NotificationType;
     29 import com.intellij.openapi.actionSystem.CommonDataKeys;
     30 import com.intellij.openapi.actionSystem.DataContext;
     31 import com.intellij.openapi.diagnostic.ErrorReportSubmitter;
     32 import com.intellij.openapi.diagnostic.IdeaLoggingEvent;
     33 import com.intellij.openapi.diagnostic.SubmittedReportInfo;
     34 import com.intellij.openapi.extensions.PluginId;
     35 import com.intellij.openapi.progress.EmptyProgressIndicator;
     36 import com.intellij.openapi.progress.ProgressManager;
     37 import com.intellij.openapi.project.Project;
     38 import com.intellij.util.Consumer;
     39 
     40 import java.awt.*;
     41 import java.util.Map;
     42 
     43 /**
     44  * Sends crash reports to Github.
     45  *
     46  * Based on the go-lang plugin's error reporter
     47  * (https://github.com/dlsniper/google-go-lang-idea-plugin/commit/c451006cc9fc926ca347189951baa94f4032c5c4)
     48  */
     49 public class ErrorReporter extends ErrorReportSubmitter {
     50 
     51   @Override
     52   public String getReportActionText() {
     53     return "Report as issue on smali's github repo";
     54   }
     55 
     56   @Override
     57   public boolean submit(IdeaLoggingEvent[] events, String additionalInfo, Component parentComponent,
     58                         final Consumer<SubmittedReportInfo> consumer) {
     59     IdeaLoggingEvent event = events[0];
     60     ErrorBean bean = new ErrorBean(event.getThrowable(), IdeaLogger.ourLastActionId);
     61 
     62     final DataContext dataContext = DataManager.getInstance().getDataContext(parentComponent);
     63 
     64     bean.setDescription(additionalInfo);
     65     bean.setMessage(event.getMessage());
     66 
     67     Throwable throwable = event.getThrowable();
     68     if (throwable != null) {
     69       final PluginId pluginId = IdeErrorsDialog.findPluginId(throwable);
     70       if (pluginId != null) {
     71         final IdeaPluginDescriptor ideaPluginDescriptor = PluginManager.getPlugin(pluginId);
     72         if (ideaPluginDescriptor != null && !ideaPluginDescriptor.isBundled()) {
     73           bean.setPluginName(ideaPluginDescriptor.getName());
     74           bean.setPluginVersion(ideaPluginDescriptor.getVersion());
     75         }
     76       }
     77     }
     78 
     79     Object data = event.getData();
     80 
     81     if (data instanceof LogMessageEx) {
     82       bean.setAttachments(((LogMessageEx)data).getAttachments());
     83     }
     84 
     85     Map<String, String> reportValues = ITNProxy.createParameters(bean);
     86 
     87     final Project project = CommonDataKeys.PROJECT.getData(dataContext);
     88 
     89     Consumer<String> successCallback = new Consumer<String>() {
     90       @Override
     91       public void consume(String token) {
     92         final SubmittedReportInfo reportInfo = new SubmittedReportInfo(
     93                 null, "Issue " + token, SubmittedReportInfo.SubmissionStatus.NEW_ISSUE);
     94         consumer.consume(reportInfo);
     95 
     96         ReportMessages.GROUP.createNotification(ReportMessages.ERROR_REPORT,
     97                 "Submitted",
     98                 NotificationType.INFORMATION,
     99                 null).setImportant(false).notify(project);
    100       }
    101     };
    102 
    103     Consumer<Exception> errorCallback = new Consumer<Exception>() {
    104       @Override
    105       public void consume(Exception e) {
    106         String message = String.format("<html>There was an error while creating a GitHub issue: %s<br>" +
    107                 "Please consider manually creating an issue on the " +
    108                 "<a href=\"https://github.com/JesusFreke/smali/issues\">Smali Issue Tracker</a></html>",
    109                 e.getMessage());
    110         ReportMessages.GROUP.createNotification(ReportMessages.ERROR_REPORT,
    111                 message,
    112                 NotificationType.ERROR,
    113                 NotificationListener.URL_OPENING_LISTENER).setImportant(false).notify(project);
    114       }
    115     };
    116 
    117     GithubFeedbackTask task = new GithubFeedbackTask(project, "Submitting error report", true,  reportValues,
    118             successCallback, errorCallback);
    119 
    120     if (project == null) {
    121       task.run(new EmptyProgressIndicator());
    122     } else {
    123       ProgressManager.getInstance().run(task);
    124     }
    125     return true;
    126   }
    127 }
    128