Project

General

Profile

« Previous | Next » 

Revision fa518bf8

Added by Cherian Mathew over 9 years ago

Added copy button to popup and context info to the error trace

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/model/MessagingUtils.java
8 8
import org.apache.log4j.Logger;
9 9
import org.eclipse.core.runtime.IStatus;
10 10
import org.eclipse.core.runtime.MultiStatus;
11
import org.eclipse.core.runtime.Platform;
11 12
import org.eclipse.core.runtime.Status;
12 13
import org.eclipse.jface.dialogs.MessageDialog;
13 14
import org.eclipse.swt.widgets.Display;
14 15

  
15 16
import eu.etaxonomy.cdm.persistence.hibernate.permission.SecurityExceptionUtils;
17
import eu.etaxonomy.taxeditor.store.CdmStore;
16 18
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
17 19

  
18 20
/**
......
154 156
        error(source.getClass(), t.getMessage(), t);
155 157
    }
156 158

  
159

  
160

  
157 161
    /**
158
     * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
162
     * Returns a list of strings, providing info on,
163
     *  - login
164
     *  - editor version
165
     *  - server (address + source name)
166
     *  - db schema version
159 167
     *
160
     * @param title
161
     *            a {@link java.lang.String} object.
162
     * @param source
163
     *            a {@link java.lang.Object} object.
164
     * @param status
165
     *            a {@link org.eclipse.core.runtime.IStatus} object.
168
     * @return
166 169
     */
167
    private static void errorDialog(final String title,
168
            final Object source,
169
            final String message,
170
            final IStatus status) {
170
    public static List<String> getContextInfo() {
171
        List<String> contextInfo = new ArrayList<String>();
172
        String name = "";
173
        String schemaVersion = "";
174
        String server = "";
175
        String version = "";
176
        String login = "";
177
        try {
178
            version = Platform.getBundle("eu.etaxonomy.taxeditor.application").getHeaders().get(org.osgi.framework.Constants.BUNDLE_VERSION);
179

  
180
            if(CdmStore.getActiveCdmSource() != null ) {
181
                login = CdmStore.getLoginManager().getAuthenticatedUser().getUsername();
182
                name = CdmStore.getActiveCdmSource().getName();
183
                schemaVersion = CdmStore.getActiveCdmSource().getDbSchemaVersion();
184
                server = CdmStore.getActiveCdmSource().getServer();
185
            }
171 186

  
172
        Display.getDefault().asyncExec(new Runnable() {
187
        } catch (Exception e) {
188
            // Nothing to do
189
        }
190
        contextInfo.add("login : " + login);
191
        contextInfo.add("editor version : " + version);
192
        contextInfo.add("server : " + server + " / " + name);
193
        contextInfo.add("schema version : " + schemaVersion);
173 194

  
174
            @Override
175
            public void run() {
176
                CdmErrorDialog ced = new CdmErrorDialog(AbstractUtility.getShell(), title, message, status);
177
                ced.open();
178
                Class<? extends Object> clazz = source != null ? source.getClass() : this.getClass();
179
                error(clazz, status);
180
            }
181
        });
195
        return contextInfo;
196
    }
197

  
198
    public static String getStackTraceAndContextInfo(Throwable t, List<String> contextInfo)  {
199
        StringBuffer stackTraceAndContextInfo = new StringBuffer();
200

  
201
        for(String infoItem : contextInfo) {
202
            stackTraceAndContextInfo.append(infoItem + System.getProperty("line.separator"));
203
        }
204

  
205
        StringWriter sw = new StringWriter();
206
        t.printStackTrace(new PrintWriter(sw));
207

  
208
        stackTraceAndContextInfo.append(sw.toString());
209

  
210
        return stackTraceAndContextInfo.toString();
182 211
    }
183 212

  
213
    /**
214
     * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
215
     *
216
     * @param title
217
     * @param source
218
     * @param t
219
     * @param contextInfo
220
     * @param message
221
     * @param status
222
     */
184 223
    private static void errorDialog(final String title,
185 224
            final Object source,
186 225
            final Throwable t,
226
            final List<String> contextInfo,
187 227
            final String message,
188 228
            final MultiStatus status) {
189 229

  
......
191 231

  
192 232
            @Override
193 233
            public void run() {
194
                CdmErrorDialog ced = new CdmErrorDialog(AbstractUtility.getShell(), title, message, status);
234
                String stackTraceWithContext = getStackTraceAndContextInfo(t, contextInfo);
235
                CdmErrorDialog ced = new CdmErrorDialog(AbstractUtility.getShell(), title, message, status, stackTraceWithContext);
195 236
                ced.open();
196 237
                Class<? extends Object> clazz = source != null ? source.getClass() : this.getClass();
197 238

  
198
                // Usually the status contains only the first line of the stack trace.
199
                // For the unexpected messages we need the entire stack trace so we
200
                // create a new status with the entire stacktrace
201
                StringWriter sw = new StringWriter();
202
                t.printStackTrace(new PrintWriter(sw));
239

  
203 240
                IStatus singleStatus = new Status(IStatus.ERROR,
204 241
                        status.getPlugin(),
205 242
                        message,
206
                        new Exception(sw.toString()));
243
                        new Exception(stackTraceWithContext));
207 244

  
208 245
                error(clazz, singleStatus);
209 246
            }
......
233 270
        // idea of writing out the stack trace as a single string
234 271
        // leads to a single line on windows
235 272
        List<Status> childStatuses = new ArrayList<Status>();
273

  
274
        // add context info
275
        List<String> contextInfo = getContextInfo();
276
        for(String infoItem : contextInfo) {
277
            childStatuses.add(new Status(IStatus.ERROR, pluginId, infoItem));
278
        }
279

  
280
        // add main execption
236 281
        for (StackTraceElement ste : t.getStackTrace()) {
237
            // build & add status
238 282
            childStatuses.add(new Status(IStatus.ERROR, pluginId, "at " + ste.toString()));
239 283
        }
240 284

  
285
        // add cause
241 286
        if(t.getCause() != null) {
242 287
            childStatuses.add(new Status(IStatus.ERROR, pluginId, ""));
243 288
            childStatuses.add(new Status(IStatus.ERROR, pluginId, "Caused by : " + t.getCause().toString()));
......
247 292
            }
248 293
        }
249 294

  
250
        // build message with contact info
251 295
        String finalMessage = message;
252 296

  
253 297
        if(finalMessage == null || finalMessage.isEmpty()) {
......
255 299
        }
256 300

  
257 301
        if(addContactMesg) {
302
            // add edit support contact info to message
258 303
            finalMessage += MessagingUtils.CONTACT_MESSAGE;
259 304
        }
260 305

  
......
264 309
                t.toString(),
265 310
                t);
266 311

  
267
        errorDialog(title, source, t, finalMessage, ms);
312
        errorDialog(title, source, t, contextInfo, finalMessage, ms);
268 313
    }
269 314

  
315
    /**
316
     * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
317
     *
318
     * @param title
319
     *            a {@link java.lang.String} object.
320
     * @param source
321
     *            a {@link java.lang.Object} object.
322
     * @param status
323
     *            a {@link org.eclipse.core.runtime.IStatus} object.
324
     */
325
    private static void errorDialog(final String title,
326
            final Object source,
327
            final String message,
328
            final IStatus status) {
329

  
330
        Display.getDefault().asyncExec(new Runnable() {
331

  
332
            @Override
333
            public void run() {
334
                CdmErrorDialog ced = new CdmErrorDialog(AbstractUtility.getShell(), title, message, status);
335
                ced.open();
336
                Class<? extends Object> clazz = source != null ? source.getClass() : this.getClass();
337
                error(clazz, status);
338
            }
339
        });
340
    }
270 341

  
271 342
    /**
272 343
     * Displays a dialog for an exception occurring in an operation.

Also available in: Unified diff