Changeset 6642 in josm for trunk/src


Ignore:
Timestamp:
2014-01-06T15:57:41+01:00 (10 years ago)
Author:
Don-vip
Message:

Improve handling of network errors at startup, suggest to change proxy settings

Location:
trunk/src/org/openstreetmap/josm
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/Main.java

    r6565 r6642  
    2323import java.util.Arrays;
    2424import java.util.Collection;
     25import java.util.HashMap;
    2526import java.util.Iterator;
    2627import java.util.List;
     
    202203     */
    203204    public OsmValidator validator;
     205
    204206    /**
    205207     * The MOTD Layer.
     
    208210
    209211    private static final Collection<MapFrameListener> mapFrameListeners = new ArrayList<MapFrameListener>();
     212
     213    protected static final Map<String, Throwable> networkErrors = new HashMap<String, Throwable>();
    210214
    211215    /**
     
    303307     */
    304308    public static void error(Throwable t) {
    305         error(getErrorMessage(t));
    306         t.printStackTrace();
     309        error(t, true);
    307310    }
    308311
     
    313316     */
    314317    public static void warn(Throwable t) {
     318        warn(t, true);
     319    }
     320
     321    /**
     322     * Prints an error message for the given Throwable.
     323     * @param t The throwable object causing the error
     324     * @param stackTrace {@code true}, if the stacktrace should be displayed
     325     * @since 6442
     326     */
     327    public static void error(Throwable t, boolean stackTrace) {
     328        error(getErrorMessage(t));
     329        if (stackTrace) {
     330            t.printStackTrace();
     331        }
     332    }
     333
     334    /**
     335     * Prints a warning message for the given Throwable.
     336     * @param t The throwable object causing the error
     337     * @param stackTrace {@code true}, if the stacktrace should be displayed
     338     * @since 6442
     339     */
     340    public static void warn(Throwable t, boolean stackTrace) {
    315341        warn(getErrorMessage(t));
    316         t.printStackTrace();
    317     }
    318 
    319     private static String getErrorMessage(Throwable t) {
     342        if (stackTrace) {
     343            t.printStackTrace();
     344        }
     345    }
     346
     347    /**
     348     * Returns a human-readable message of error, also usable for developers.
     349     * @param t The error
     350     * @return The human-readable error message
     351     * @since 6642
     352     */
     353    public static String getErrorMessage(Throwable t) {
     354        if (t == null) {
     355                return null;
     356        }
    320357        StringBuilder sb = new StringBuilder(t.getClass().getName());
    321358        String msg = t.getMessage();
     
    455492                try {
    456493                    OsmApi.getOsmApi().initialize(null, true);
    457                 } catch (Exception x) {
    458                     // ignore any exception here.
     494                } catch (Exception e) {
     495                    Main.warn(getErrorMessage(Utils.getRootCause(e)));
    459496                }
    460497                return null;
     
    722759            panel.updateUI();
    723760        } catch (final Exception e) {
    724             e.printStackTrace();
     761            error(e);
    725762        }
    726763        UIManager.put("OptionPane.okIcon", ImageProvider.get("ok"));
     
    13501387        return listener != null ? mapFrameListeners.remove(listener) : false;
    13511388    }
     1389
     1390    /**
     1391     * Adds a new network error that occur to give a hint about broken Internet connection.
     1392     * Do not use this method for errors known for sure thrown because of a bad proxy configuration.
     1393     *
     1394     * @param url The accessed URL that caused the error
     1395     * @param t The network error
     1396     * @return The previous error associated to the given resource, if any. Can be {@code null}
     1397     * @since 6639
     1398     */
     1399    public static Throwable addNetworkError(URL url, Throwable t) {
     1400        if (url != null && t != null) {
     1401            Throwable old = addNetworkError(url.toExternalForm(), t);
     1402            if (old != null) {
     1403                Main.warn("Already here "+old);
     1404            }
     1405            return old;
     1406        }
     1407        return null;
     1408    }
     1409
     1410    /**
     1411     * Adds a new network error that occur to give a hint about broken Internet connection.
     1412     * Do not use this method for errors known for sure thrown because of a bad proxy configuration.
     1413     *
     1414     * @param url The accessed URL that caused the error
     1415     * @param t The network error
     1416     * @return The previous error associated to the given resource, if any. Can be {@code null}
     1417     * @since 6639
     1418     */
     1419    public static Throwable addNetworkError(String url, Throwable t) {
     1420        if (url != null && t != null) {
     1421            return networkErrors.put(url, t);
     1422        }
     1423        return null;
     1424    }
     1425
     1426    /**
     1427     * Returns the network errors that occured until now.
     1428     * @return the network errors that occured until now, indexed by URL
     1429     * @since 6639
     1430     */
     1431    public static Map<String, Throwable> getNetworkErrors() {
     1432        return new HashMap<String, Throwable>(networkErrors);
     1433    }
    13521434}
  • trunk/src/org/openstreetmap/josm/data/imagery/ImageryLayerInfo.java

    r6316 r6642  
    8181            } catch (IOException ex) {
    8282                Utils.close(stream);
    83                 ex.printStackTrace();
     83                Main.error(ex, false);
    8484                continue;
    85             } catch (SAXException sex) {
     85            } catch (SAXException ex) {
    8686                Utils.close(stream);
    87                 sex.printStackTrace();
     87                Main.error(ex);
    8888                continue;
    8989            }
  • trunk/src/org/openstreetmap/josm/gui/MainApplication.java

    r6578 r6642  
    2727import java.util.List;
    2828import java.util.Map;
     29import java.util.Set;
     30import java.util.TreeSet;
    2931
    3032import javax.swing.JFrame;
     
    8789        mainFrame.setIconImages(l);
    8890        mainFrame.addWindowListener(new WindowAdapter(){
    89             @Override public void windowClosing(final WindowEvent arg0) {
     91            @Override
     92            public void windowClosing(final WindowEvent arg0) {
    9093                Main.exitJosm(true, 0);
    9194            }
     
    297300
    298301        Thread.setDefaultUncaughtExceptionHandler(new BugReportExceptionHandler());
    299         // http://stuffthathappens.com/blog/2007/10/15/one-more-note-on-uncaught-exception-handlers/
     302        // http://stackoverflow.com/q/75218/2257172
     303        // To be replaced with official API when switching to Java 7: https://bugs.openjdk.java.net/browse/JDK-4714232
    300304        System.setProperty("sun.awt.exception.handler", BugReportExceptionHandler.class.getName());
    301305
     
    441445        public void run() {
    442446
    443             // Handle proxy errors early to inform user he should change settings to be able to use JOSM correctly
    444             handleProxyErrors();
     447            // Handle proxy/network errors early to inform user he should change settings to be able to use JOSM correctly
     448            if (!handleProxyErrors()) {
     449                handleNetworkErrors();
     450            }
    445451
    446452            // Restore autosave layers after crash and start autosave thread
     
    480486        }
    481487
    482         private void handleProxyErrors() {
    483             if (proxySelector.hasErrors()) {
     488        private boolean handleNetworkOrProxyErrors(boolean hasErrors, String title, String message) {
     489            if (hasErrors) {
    484490                ExtendedDialog ed = new ExtendedDialog(
    485                         Main.parent, tr("Proxy errors occurred"),
     491                        Main.parent, title,
    486492                        new String[]{tr("Change proxy settings"), tr("Cancel")});
    487493                ed.setButtonIcons(new String[]{"dialogs/settings.png", "cancel.png"}).setCancelButton(2);
    488494                ed.setMinimumSize(new Dimension(460, 260));
    489495                ed.setIcon(JOptionPane.WARNING_MESSAGE);
    490                 ed.setContent(tr("JOSM tried to access the following resources:<br>" +
    491                         "{0}" +
    492                         "but <b>failed</b> to do so, because of the following proxy errors:<br>" +
    493                         "{1}" +
    494                         "Would you like to change your proxy settings now?",
    495                         Utils.joinAsHtmlUnorderedList(proxySelector.getErrorResources()),
    496                         Utils.joinAsHtmlUnorderedList(proxySelector.getErrorMessages())
    497                 ));
     496                ed.setContent(message);
    498497
    499498                if (ed.showDialog().getValue() == 1) {
     
    501500                }
    502501            }
     502            return hasErrors;
     503        }
     504
     505        private boolean handleProxyErrors() {
     506            return handleNetworkOrProxyErrors(proxySelector.hasErrors(), tr("Proxy errors occurred"),
     507                    tr("JOSM tried to access the following resources:<br>" +
     508                            "{0}" +
     509                            "but <b>failed</b> to do so, because of the following proxy errors:<br>" +
     510                            "{1}" +
     511                            "Would you like to change your proxy settings now?",
     512                            Utils.joinAsHtmlUnorderedList(proxySelector.getErrorResources()),
     513                            Utils.joinAsHtmlUnorderedList(proxySelector.getErrorMessages())
     514                    ));
     515        }
     516
     517        private boolean handleNetworkErrors() {
     518            boolean condition = !networkErrors.isEmpty();
     519            if (condition) {
     520                Set<String> errors = new TreeSet<String>();
     521                for (Throwable t : networkErrors.values()) {
     522                    errors.add(t.toString());
     523                }
     524                return handleNetworkOrProxyErrors(condition, tr("Network errors occurred"),
     525                        tr("JOSM tried to access the following resources:<br>" +
     526                                "{0}" +
     527                                "but <b>failed</b> to do so, because of the following network errors:<br>" +
     528                                "{1}" +
     529                                "It may result of a missing proxy configuration.<br>" +
     530                                "Would you like to change your proxy settings now?",
     531                                Utils.joinAsHtmlUnorderedList(networkErrors.keySet()),
     532                                Utils.joinAsHtmlUnorderedList(errors)
     533                        ));
     534            }
     535            return false;
    503536        }
    504537    }
  • trunk/src/org/openstreetmap/josm/io/MirroredInputStream.java

    r6617 r6642  
    297297            con.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
    298298            con.setReadTimeout(Main.pref.getInteger("socket.timeout.read",30)*1000);
    299             con.connect();
     299            try {
     300                con.connect();
     301            } catch (IOException e) {
     302                Main.addNetworkError(downloadUrl, Utils.getRootCause(e));
     303                throw e;
     304            }
    300305            switch(con.getResponseCode()) {
    301306            case HttpURLConnection.HTTP_OK:
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r6582 r6642  
    7878    private static Map<String, OsmApi> instances = new HashMap<String, OsmApi>();
    7979
     80    private URL url = null;
     81
    8082    /**
    8183     * Replies the {@link OsmApi} for a given server URL
     
    235237            /* This is an interim solution for openstreetmap.org not currently
    236238             * transmitting their imagery blacklist in the capabilities call.
    237              * remove this as soon as openstreetmap.org adds blacklists. */
     239             * remove this as soon as openstreetmap.org adds blacklists.
     240             * If you want to update this list, please ask for update of
     241             * http://trac.openstreetmap.org/ticket/5024
     242             * This list should not be maintained by each OSM editor (see #9210) */
    238243            if (this.serverUrl.matches(".*openstreetmap.org/api.*") && capabilities.getImageryBlacklist().isEmpty())
    239244            {
     
    261266        } catch (OsmTransferCanceledException e) {
    262267            throw e;
     268        } catch (OsmTransferException e) {
     269            initialized = false;
     270            Main.addNetworkError(url, Utils.getRootCause(e));
     271            throw new OsmApiInitializationException(e);
    263272        } catch (Exception e) {
    264273            initialized = false;
     
    598607        while(true) { // the retry loop
    599608            try {
    600                 URL url = new URL(new URL(getBaseUrl()), urlSuffix);
    601                 System.out.print(requestMethod + " " + url + "... ");
     609                url = new URL(new URL(getBaseUrl()), urlSuffix);
     610                Main.info(requestMethod + " " + url + "... ");
    602611                // fix #5369, see http://www.tikalk.com/java/forums/httpurlconnection-disable-keep-alive
    603612                activeConnection = Utils.openHttpConnection(url, false);
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r6623 r6642  
    419419            return true;
    420420        } catch (IllegalStateException ex) {
    421             ex.printStackTrace();
     421            Main.error(ex);
    422422            return false;
    423423        }
     
    449449            }
    450450        } catch (UnsupportedFlavorException ex) {
    451             ex.printStackTrace();
     451            Main.error(ex);
    452452            return null;
    453453        } catch (IOException ex) {
    454             ex.printStackTrace();
     454            Main.error(ex);
    455455            return null;
    456456        }
     
    895895    }
    896896
     897    /**
     898     * Returns the root cause of a throwable object.
     899     * @param t The object to get root cause for
     900     * @return the root cause of {@code t}
     901     * @since 6639
     902     */
     903    public static Throwable getRootCause(Throwable t) {
     904        Throwable result = t;
     905        if (result != null) {
     906            Throwable cause = result.getCause();
     907            while (cause != null && cause != result) {
     908                result = cause;
     909                cause = result.getCause();
     910            }
     911        }
     912        return result;
     913    }
    897914}
  • trunk/src/org/openstreetmap/josm/tools/WikiReader.java

    r6380 r6642  
    1818    private final String baseurl;
    1919
     20    /**
     21     * Constructs a new {@code WikiReader} for the given base URL.
     22     * @param baseurl The wiki base URL
     23     */
    2024    public WikiReader(String baseurl) {
    2125        this.baseurl = baseurl;
     
    8084
    8185    private String readLang(URL url) throws IOException {
    82         BufferedReader in = Utils.openURLReader(url);
     86        BufferedReader in;
     87        try {
     88            in = Utils.openURLReader(url);
     89        } catch (IOException e) {
     90            Main.addNetworkError(url, Utils.getRootCause(e));
     91            throw e;
     92        }
    8393        try {
    8494            return readFromTrac(in, url);
     
    124134                // add a border="0" attribute to images, otherwise the internal help browser
    125135                // will render a thick  border around images inside an <a> element
    126                 //
    127136                b += line.replaceAll("<img ", "<img border=\"0\" ")
    128137                         .replaceAll("<span class=\"icon\">.</span>", "")
Note: See TracChangeset for help on using the changeset viewer.