Changeset 4691 in josm for trunk/src


Ignore:
Timestamp:
2011-12-21T23:19:49+01:00 (13 years ago)
Author:
Don-vip
Message:

see #4093 - More user-friendly error message with failed precondition (HTTP 412) related to nodes still used by ways

File:
1 edited

Legend:

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

    r4482 r4691  
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
     5import static org.openstreetmap.josm.tools.I18n.trn;
    56
    67import java.io.IOException;
     
    1314import java.text.ParseException;
    1415import java.text.SimpleDateFormat;
     16import java.util.Collection;
    1517import java.util.Date;
    1618import java.util.Locale;
     19import java.util.Set;
     20import java.util.TreeSet;
    1721import java.util.regex.Matcher;
    1822import java.util.regex.Pattern;
     
    8488        return msg;
    8589    }
     90   
     91    /**
     92     * Explains a precondition exception when a child node could not be deleted because
     93     * it is still referred to by undeleted parent ways.
     94     *
     95     * @param e the exception
     96     * @param childNode the child node
     97     * @param parentWays the parent ways
     98     * @return
     99     */
     100    public static String explainDeletedNodeStillInUse(OsmApiException e, long childNode, Collection<Long> parentWays) {
     101        String ids = parentWays.size() == 1 ? parentWays.iterator().next().toString() : parentWays.toString();
     102        String msg = trn(
     103                "<html><strong>Failed</strong> to delete <strong>node {0}</strong>."
     104                + " It is still referred to by way {1}.<br>"
     105                + "Please load way {1}, remove the reference to node {0}, and upload again.</html>",
     106                "<html><strong>Failed</strong> to delete <strong>node {0}</strong>."
     107                + " It is still referred to by ways {1}.<br>"
     108                + "Please load ways {1}, remove the reference to node {0}, and upload again.</html>",
     109                parentWays.size(), childNode, ids
     110        );
     111        return msg;
     112    }
    86113
    87114    /**
     
    94121        String msg = e.getErrorHeader();
    95122        if (msg != null) {
    96             String pattern = "Precondition failed: The relation (\\d+) is used in relation (\\d+)\\.";
    97             Pattern p = Pattern.compile(pattern);
    98             Matcher m = p.matcher(msg);
     123            Matcher m = Pattern.compile("Precondition failed: The relation (\\d+) is used in relation (\\d+)\\.").matcher(msg);
    99124            if (m.matches()) {
    100125                long childRelation = Long.parseLong(m.group(1));
    101126                long parentRelation = Long.parseLong(m.group(2));
    102127                return explainDeletedRelationStillInUse(e, childRelation, parentRelation);
     128            }
     129            m = Pattern.compile("Precondition failed: Node (\\d+) is still used by ways (\\d+(?:,\\d+)*)\\.").matcher(msg);
     130            if (m.matches()) {
     131                long childNode = Long.parseLong(m.group(1));
     132                Set<Long> parentWays = new TreeSet<Long>(); // Error message can contain several times the same way
     133                for (String s : m.group(2).split(",")) {
     134                    parentWays.add(Long.parseLong(s));
     135                }
     136                return explainDeletedNodeStillInUse(e, childNode, parentWays);
    103137            }
    104138        }
Note: See TracChangeset for help on using the changeset viewer.