Ignore:
Timestamp:
2016-01-16T00:00:30+01:00 (9 years ago)
Author:
Don-vip
Message:

ExceptionUtil: add unit test, fix javadoc, various NPEs, HTML formatting problems (missing end tags)

File:
1 edited

Legend:

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

    r9353 r9474  
    3636import org.openstreetmap.josm.tools.date.DateUtils;
    3737
     38/**
     39 * Utilities for exception handling.
     40 * @since 2097
     41 */
    3842public final class ExceptionUtil {
    3943
     
    4347
    4448    /**
    45      * handles an exception caught during OSM API initialization
     49     * Explains an exception caught during OSM API initialization.
    4650     *
    4751     * @param e the exception
     
    5357                "<html>Failed to initialize communication with the OSM server {0}.<br>"
    5458                + "Check the server URL in your preferences and your internet connection.",
    55                 OsmApi.getOsmApi().getServerUrl());
    56     }
    57 
     59                OsmApi.getOsmApi().getServerUrl())+"</html>";
     60    }
     61
     62    /**
     63     * Explains a {@link OsmApiException} which was thrown because accessing a protected
     64     * resource was forbidden.
     65     *
     66     * @param e the exception
     67     * @return The HTML formatted error message to display
     68     */
    5869    public static String explainMissingOAuthAccessTokenException(MissingOAuthAccessTokenException e) {
    5970        Main.error(e);
     
    6980
    7081    public static Pair<OsmPrimitive, Collection<OsmPrimitive>> parsePreconditionFailed(String msg) {
     82        if (msg == null)
     83            return null;
    7184        final String ids = "(\\d+(?:,\\d+)*)";
    7285        final Collection<OsmPrimitive> refs = new TreeSet<>(); // error message can contain several times the same way
    7386        Matcher m;
    74         m = Pattern.compile(".*Node (\\d+) is still used by relations " + ids + ".*").matcher(msg);
     87        m = Pattern.compile(".*Node (\\d+) is still used by relations? " + ids + ".*").matcher(msg);
    7588        if (m.matches()) {
    7689            OsmPrimitive n = new Node(Long.parseLong(m.group(1)));
     
    8093            return Pair.create(n, refs);
    8194        }
    82         m = Pattern.compile(".*Node (\\d+) is still used by ways " + ids + ".*").matcher(msg);
     95        m = Pattern.compile(".*Node (\\d+) is still used by ways? " + ids + ".*").matcher(msg);
    8396        if (m.matches()) {
    8497            OsmPrimitive n = new Node(Long.parseLong(m.group(1)));
     
    96109            return Pair.create(n, refs);
    97110        }
    98         m = Pattern.compile(".*Way (\\d+) is still used by relations " + ids + ".*").matcher(msg);
     111        m = Pattern.compile(".*Way (\\d+) is still used by relations? " + ids + ".*").matcher(msg);
    99112        if (m.matches()) {
    100113            OsmPrimitive n = new Way(Long.parseLong(m.group(1)));
     
    240253    }
    241254
     255    /**
     256     * Explains a {@link OsmApiException} which was thrown because the authentication at
     257     * the OSM server failed, with basic authentication.
     258     *
     259     * @param e the exception
     260     * @return The HTML formatted error message to display
     261     */
    242262    public static String explainFailedBasicAuthentication(OsmApiException e) {
    243263        Main.error(e);
     
    250270    }
    251271
     272    /**
     273     * Explains a {@link OsmApiException} which was thrown because the authentication at
     274     * the OSM server failed, with OAuth authentication.
     275     *
     276     * @param e the exception
     277     * @return The HTML formatted error message to display
     278     */
    252279    public static String explainFailedOAuthAuthentication(OsmApiException e) {
    253280        Main.error(e);
     
    260287    }
    261288
     289    /**
     290     * Explains a {@link OsmApiException} which was thrown because accessing a protected
     291     * resource was forbidden (HTTP 403), without OAuth authentication.
     292     *
     293     * @param e the exception
     294     * @return The HTML formatted error message to display
     295     */
    262296    public static String explainFailedAuthorisation(OsmApiException e) {
    263297        Main.error(e);
    264298        String header = e.getErrorHeader();
    265299        String body = e.getErrorBody();
    266         String msg = null;
     300        String msg;
    267301        if (header != null) {
    268302            if (body != null && !header.equals(body)) {
     
    291325    }
    292326
     327    /**
     328     * Explains a {@link OsmApiException} which was thrown because accessing a protected
     329     * resource was forbidden (HTTP 403), with OAuth authentication.
     330     *
     331     * @param e the exception
     332     * @return The HTML formatted error message to display
     333     */
    293334    public static String explainFailedOAuthAuthorisation(OsmApiException e) {
    294335        Main.error(e);
     
    356397        String msg = e.getErrorHeader();
    357398        if (msg != null) {
    358             String pattern = "The changeset (\\d+) was closed at (.*)";
    359             Pattern p = Pattern.compile(pattern);
    360             Matcher m = p.matcher(msg);
     399            Matcher m = Pattern.compile("The changeset (\\d+) was closed at (.*)").matcher(msg);
    361400            if (m.matches()) {
    362401                long changesetId = Long.parseLong(m.group(1));
     
    392431                    "<html>The server reported that it has detected a conflict.");
    393432        }
    394         return msg;
     433        return msg.endsWith("</html>") ? msg : (msg + "</html>");
    395434    }
    396435
     
    449488        return tr("<html>Failed to open a connection to the remote server<br>" + "''{0}''<br>"
    450489                + "for security reasons. This is most likely because you are running<br>"
    451                 + "in an applet and because you did not load your applet from ''{1}''.", apiUrl, host);
     490                + "in an applet and because you did not load your applet from ''{1}''.", apiUrl, host)+"</html>";
    452491    }
    453492
     
    463502        Main.error(e);
    464503        return tr("<html>Failed to open a connection to the remote server<br>" + "''{0}''.<br>"
    465                 + "Please check your internet connection.", e.getUrl());
     504                + "Please check your internet connection.", e.getUrl())+"</html>";
    466505    }
    467506
     
    479518        return tr("<html>Failed to upload data to or download data from<br>" + "''{0}''<br>"
    480519                + "due to a problem with transferring data.<br>"
    481                 + "Details (untranslated): {1}</html>", e.getUrl(), ioe
    482                 .getMessage());
     520                + "Details (untranslated): {1}</html>", e.getUrl(),
     521                ioe != null ? ioe.getMessage() : "null");
    483522    }
    484523
     
    495534        return tr("<html>Failed to download data. "
    496535                + "Its format is either unsupported, ill-formed, and/or inconsistent.<br>"
    497                 + "<br>Details (untranslated): {0}</html>", ide.getMessage());
     536                + "<br>Details (untranslated): {0}</html>", ide != null ? ide.getMessage() : "null");
    498537    }
    499538
     
    510549        Main.error(e);
    511550        return tr("<html>Failed to download data.<br>"
    512                 + "<br>Details: {0}</html>", oae.getMessage());
     551                + "<br>Details: {0}</html>", oae != null ? oae.getMessage() : "null");
    513552    }
    514553
    515554    /**
    516555     * Explains a {@link OsmApiException} which was thrown because of an internal server
    517      * error in the OSM API server..
     556     * error in the OSM API server.
    518557     *
    519558     * @param e the exception
     
    523562        Main.error(e);
    524563        return tr("<html>The OSM server<br>" + "''{0}''<br>" + "reported an internal server error.<br>"
    525                 + "This is most likely a temporary problem. Please try again later.", e.getUrl());
     564                + "This is most likely a temporary problem. Please try again later.", e.getUrl())+"</html>";
    526565    }
    527566
     
    613652        return tr("<html>Failed to open a connection to the remote server<br>" + "''{0}''.<br>"
    614653                + "Host name ''{1}'' could not be resolved. <br>"
    615                 + "Please check the API URL in your preferences and your internet connection.", apiUrl, host);
     654                + "Please check the API URL in your preferences and your internet connection.", apiUrl, host)+"</html>";
    616655    }
    617656
     
    708747        }
    709748    }
    710 
    711749}
Note: See TracChangeset for help on using the changeset viewer.