@Override public void run() { try { ByteArrayOutputStream output = new ByteArrayOutputStream(); ByteStreams.copy(input, output); result = output.toString(Charset.defaultCharset().name()); } catch (Exception e) { result = Throwables.getStackTraceAsString(e); } }
/** * Default behavior of {@link Errors#dialog()} is @{link Throwable#printStackTrace()} * and {@link javax.swing.JOptionPane#showMessageDialog(java.awt.Component, Object, String, int) JOptionPane.showMessageDialog}. * * The `JOptionPane` part is called using reflection, and fails silently if swing isn't available. The `Throwable.printStackTrace` * part works whether swing is available or not. */ static void defaultDialog(Throwable error) { error.printStackTrace(); try { Method invokeLater = Class.forName("javax.swing.SwingUtilities").getMethod("invokeLater", Runnable.class); Class<?> javaAwtComponent = Class.forName("java.awt.Component"); Method showMessageDialog = Class.forName("javax.swing.JOptionPane").getMethod("showMessageDialog", javaAwtComponent, Object.class, String.class, int.class); Runnable runnable = () -> { try { String title = error.getClass().getSimpleName(); showMessageDialog.invoke(null, null, error.getMessage() + "\n\n" + Throwables.getStackTraceAsString(error), title, 0); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { // if the reflection fails (e.g. because we're on Android) that's no problem, we already dumped the stacktrace to console } }; invokeLater.invoke(null, runnable); } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { // if the reflection fails (e.g. because we're on Android) that's no problem, we already dumped the stacktrace to console } }