Changeset 15084 in josm


Ignore:
Timestamp:
2019-05-18T16:29:09+02:00 (5 months ago)
Author:
Don-vip
Message:

see #17722 - proper notification of user block

Location:
trunk
Files:
5 edited

Legend:

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

    r14810 r15084  
    286286            msg = tr("Access to redacted version ''{0}'' of {1} {2} is forbidden.",
    287287                    version, tr(type), id);
    288         } else if (OsmApi.isUsingOAuth()) {
     288        } else if (OsmApi.isUsingOAuth() && !ExceptionUtil.isUserBlocked(e)) {
    289289            msg = ExceptionUtil.explainFailedOAuthAuthorisation(e);
    290290        } else {
  • trunk/src/org/openstreetmap/josm/io/MessageNotifier.java

    r14326 r15084  
    1414import org.openstreetmap.josm.data.preferences.BooleanProperty;
    1515import org.openstreetmap.josm.data.preferences.IntegerProperty;
     16import org.openstreetmap.josm.gui.ExceptionDialogUtil;
    1617import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    1718import org.openstreetmap.josm.io.auth.CredentialsAgentException;
     
    8990                }
    9091            } catch (OsmTransferException e) {
    91                 Logging.warn(e);
     92                ExceptionDialogUtil.explainOsmTransferException(e);
    9293            }
    9394        }
  • trunk/src/org/openstreetmap/josm/io/OsmApiException.java

    r15077 r15084  
    213213    public String getDisplayMessage() {
    214214        StringBuilder sb = new StringBuilder();
    215         if (errorHeader != null) {
    216             sb.append(tr(errorHeader));
    217             sb.append(tr("(Code={0})", responseCode));
    218         } else if (errorBody != null && !errorBody.trim().isEmpty()) {
    219             errorBody = errorBody.trim();
    220             sb.append(tr(errorBody));
    221             sb.append(tr("(Code={0})", responseCode));
     215        String header = Utils.strip(errorHeader);
     216        String body = Utils.strip(errorBody);
     217        if ((header == null || header.isEmpty()) && (body == null || body.isEmpty())) {
     218            sb.append(tr("The server replied an error with code {0}.", responseCode));
    222219        } else {
    223             sb.append(tr("The server replied an error with code {0}.", responseCode));
     220            if (header != null && !header.isEmpty()) {
     221                sb.append(tr(header));
     222            }
     223            if (body != null && !body.isEmpty()) {
     224                if (sb.length() > 0) {
     225                    sb.append(". ");
     226                }
     227                sb.append(tr(body));
     228            }
     229            sb.append(' ').append(tr("(Code={0})", responseCode));
    224230        }
    225231        return sb.toString();
  • trunk/src/org/openstreetmap/josm/tools/ExceptionUtil.java

    r12992 r15084  
    22package org.openstreetmap.josm.tools;
    33
     4import static org.openstreetmap.josm.tools.I18n.marktr;
    45import static org.openstreetmap.josm.tools.I18n.tr;
    56import static org.openstreetmap.josm.tools.I18n.trn;
     
    4344public final class ExceptionUtil {
    4445
     46    /**
     47     * Error message sent by the OSM API when a user has been blocked.
     48     */
     49    private static final String OSM_API_BLOCKED =
     50            marktr("Your access to the API has been blocked. Please log-in to the web interface to find out more.");
     51
    4552    private ExceptionUtil() {
    4653        // Hide default constructor for utils classes
     
    296303    public static String explainFailedAuthorisation(OsmApiException e) {
    297304        Logging.error(e);
    298         String header = e.getErrorHeader();
    299         String body = e.getErrorBody();
    300         String msg;
    301         if (header != null) {
    302             if (body != null && !header.equals(body)) {
    303                 msg = header + " (" + body + ')';
    304             } else {
    305                 msg = header;
    306             }
    307         } else {
    308             msg = body;
    309         }
     305        String msg = e.getDisplayMessage();
    310306
    311307        if (msg != null && !msg.isEmpty()) {
     
    726722    }
    727723
     724    /**
     725     * Determines if the OSM API exception has been thrown because user has been blocked.
     726     * @param e OSM API exception
     727     * @return {@code true} if the OSM API exception has been thrown because user has been blocked
     728     * @since 15084
     729     */
     730    public static boolean isUserBlocked(OsmApiException e) {
     731        return OSM_API_BLOCKED.equals(e.getErrorHeader());
     732    }
     733
    728734    static String getUrlFromException(OsmApiException e) {
    729735        if (e.getAccessedUrl() != null) {
  • trunk/test/unit/org/openstreetmap/josm/tools/ExceptionUtilTest.java

    r13812 r15084  
    1111import java.util.TimeZone;
    1212
    13 import org.junit.BeforeClass;
     13import org.junit.Before;
     14import org.junit.Rule;
    1415import org.junit.Test;
    15 import org.openstreetmap.josm.JOSMFixture;
    1616import org.openstreetmap.josm.io.ChangesetClosedException;
    1717import org.openstreetmap.josm.io.IllegalDataException;
     
    2222import org.openstreetmap.josm.io.OsmApiInitializationException;
    2323import org.openstreetmap.josm.io.auth.CredentialsManager;
     24import org.openstreetmap.josm.testutils.JOSMTestRules;
    2425import org.openstreetmap.josm.tools.date.DateUtils;
    2526import org.openstreetmap.josm.tools.date.DateUtilsTest;
     27
     28import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    2629
    2730/**
     
    3033public class ExceptionUtilTest {
    3134
     35    /**
     36     * Setup rule.
     37     */
     38    @Rule
     39    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
     40    public JOSMTestRules test = new JOSMTestRules().preferences().fakeAPI();
     41
    3242    private static String baseUrl;
    3343    private static String serverUrl;
     
    3949     * @throws Exception in case of error
    4050     */
    41     @BeforeClass
    42     public static void setUp() throws Exception {
    43         JOSMFixture.createUnitTestFixture().init();
     51    @Before
     52    public void setUp() throws Exception {
    4453        OsmApi api = OsmApi.getOsmApi();
    45         api.initialize(null);
    4654        baseUrl = api.getBaseUrl();
    4755        serverUrl = api.getServerUrl();
     
    154162    @Test
    155163    public void testExplainFailedAuthorisation() {
    156         assertEquals("<html>Authorisation at the OSM server failed.<br></html>",
     164        assertEquals("<html>Authorisation at the OSM server failed.<br>The server reported the following error:<br>"+
     165                "'The server replied an error with code 0.'</html>",
    157166                ExceptionUtil.explainFailedAuthorisation(new OsmApiException("")));
    158         assertEquals("<html>Authorisation at the OSM server failed.<br>The server reported the following error:<br>'header'</html>",
     167        assertEquals("<html>Authorisation at the OSM server failed.<br>The server reported the following error:<br>'header (Code=403)'</html>",
    159168                ExceptionUtil.explainFailedAuthorisation(new OsmApiException(HttpURLConnection.HTTP_FORBIDDEN, "header", null)));
    160         assertEquals("<html>Authorisation at the OSM server failed.<br>The server reported the following error:<br>'header (body)'</html>",
     169        assertEquals("<html>Authorisation at the OSM server failed.<br>The server reported the following error:<br>'header. body (Code=403)'</html>",
    161170                ExceptionUtil.explainFailedAuthorisation(new OsmApiException(HttpURLConnection.HTTP_FORBIDDEN, "header", "body")));
    162         assertEquals("<html>Authorisation at the OSM server failed.<br>The server reported the following error:<br>'body'</html>",
     171        assertEquals("<html>Authorisation at the OSM server failed.<br>The server reported the following error:<br>'body (Code=403)'</html>",
    163172                ExceptionUtil.explainFailedAuthorisation(new OsmApiException(HttpURLConnection.HTTP_FORBIDDEN, null, "body")));
    164173    }
     
    247256    @Test
    248257    public void testExplainMissingOAuthAccessTokenException() {
    249         assertEquals("<html>Failed to authenticate at the OSM server 'https://api06.dev.openstreetmap.org/api'.<br>"+
     258        assertEquals("<html>Failed to authenticate at the OSM server 'http://fake.xxx/api'.<br>"+
    250259                "You are using OAuth to authenticate but currently there is no<br>OAuth Access Token configured.<br>"+
    251260                "Please open the Preferences Dialog and generate or enter an Access Token.</html>",
Note: See TracChangeset for help on using the changeset viewer.