Ignore:
Timestamp:
2020-11-22T22:54:01+01:00 (3 years ago)
Author:
Don-vip
Message:

fix #20131 - remote control: report errors in case of OSM API error (load_and_zoom) or no valid identifier (load_object)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/RequestProcessor.java

    r16913 r17330  
    4343import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerErrorException;
    4444import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerForbiddenException;
     45import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerOsmApiException;
    4546import org.openstreetmap.josm.io.remotecontrol.handler.VersionHandler;
    4647import org.openstreetmap.josm.tools.Logging;
     
    185186            String get = in.readLine();
    186187            if (get == null) {
    187                 sendError(out);
     188                sendInternalError(out, null);
    188189                return;
    189190            }
     
    192193            StringTokenizer st = new StringTokenizer(get);
    193194            if (!st.hasMoreTokens()) {
    194                 sendError(out);
     195                sendInternalError(out, null);
    195196                return;
    196197            }
    197198            String method = st.nextToken();
    198199            if (!st.hasMoreTokens()) {
    199                 sendError(out);
     200                sendInternalError(out, null);
    200201                return;
    201202            }
     
    252253                String help = "No command specified! The following commands are available:<ul>" + usage
    253254                        + "</ul>" + "See <a href=\""+websiteDoc+"\">"+websiteDoc+"</a> for complete documentation.";
    254                 sendHeader(out, "400 Bad Request", "text/html", true);
    255                 out.write(String.format(
    256                         RESPONSE_TEMPLATE,
    257                         "<title>Bad Request</title>",
    258                         "<h1>HTTP Error 400: Bad Request</h1>" +
    259                         "<p>" + help + "</p>"));
    260                 out.flush();
     255                sendBadRequest(out, help);
    261256            } else {
    262257                // create handler object
     
    273268                    out.write(handler.getContent());
    274269                    out.flush();
     270                } catch (RequestHandlerOsmApiException ex) {
     271                    Logging.debug(ex);
     272                    sendBadGateway(out, ex.getMessage());
    275273                } catch (RequestHandlerErrorException ex) {
    276274                    Logging.debug(ex);
    277                     sendError(out);
     275                    sendInternalError(out, ex.getMessage());
    278276                } catch (RequestHandlerBadRequestException ex) {
    279277                    Logging.debug(ex);
     
    289287            Logging.error(e);
    290288            try {
    291                 sendError(out);
     289                sendInternalError(out, e.getMessage());
    292290            } catch (IOException e1) {
    293291                Logging.warn(e1);
     
    302300    }
    303301
    304     /**
    305      * Sends a 500 error: server error
    306      *
    307      * @param out
    308      *            The writer where the error is written
    309      * @throws IOException
    310      *             If the error can not be written
    311      */
    312     private static void sendError(Writer out) throws IOException {
    313         sendHeader(out, "500 Internal Server Error", "text/html", true);
     302    private static void sendError(Writer out, int errorCode, String errorName, String help) throws IOException {
     303        sendHeader(out, errorCode + " " + errorName, "text/html", true);
    314304        out.write(String.format(
    315305                RESPONSE_TEMPLATE,
    316                 "<title>Internal Error</title>",
    317                 "<h1>HTTP Error 500: Internal Server Error</h1>"
     306                "<title>" + errorName + "</title>",
     307                "<h1>HTTP Error " + errorCode + ": " + errorName + "</h1>" +
     308                (help == null ? "" : "<p>"+Utils.escapeReservedCharactersHTML(help) + "</p>")
    318309        ));
    319310        out.flush();
     
    321312
    322313    /**
    323      * Sends a 501 error: not implemented
    324      *
    325      * @param out
    326      *            The writer where the error is written
    327      * @throws IOException
    328      *             If the error can not be written
    329      */
    330     private static void sendNotImplemented(Writer out) throws IOException {
    331         sendHeader(out, "501 Not Implemented", "text/html", true);
    332         out.write(String.format(
    333                 RESPONSE_TEMPLATE,
    334                 "<title>Not Implemented</title>",
    335                 "<h1>HTTP Error 501: Not Implemented</h1>"
    336         ));
    337         out.flush();
    338     }
    339 
    340     /**
    341      * Sends a 403 error: forbidden
     314     * Sends a 500 error: internal server error
    342315     *
    343316     * @param out
     
    348321     *             If the error can not be written
    349322     */
     323    private static void sendInternalError(Writer out, String help) throws IOException {
     324        sendError(out, 500, "Internal Server Error", help);
     325    }
     326
     327    /**
     328     * Sends a 501 error: not implemented
     329     *
     330     * @param out
     331     *            The writer where the error is written
     332     * @throws IOException
     333     *             If the error can not be written
     334     */
     335    private static void sendNotImplemented(Writer out) throws IOException {
     336        sendError(out, 501, "Not Implemented", null);
     337    }
     338
     339    /**
     340     * Sends a 502 error: bad gateway
     341     *
     342     * @param out
     343     *            The writer where the error is written
     344     * @param help
     345     *            Optional HTML help content to display, can be null
     346     * @throws IOException
     347     *             If the error can not be written
     348     */
     349    private static void sendBadGateway(Writer out, String help) throws IOException {
     350        sendError(out, 502, "Bad Gateway", help);
     351    }
     352
     353    /**
     354     * Sends a 403 error: forbidden
     355     *
     356     * @param out
     357     *            The writer where the error is written
     358     * @param help
     359     *            Optional HTML help content to display, can be null
     360     * @throws IOException
     361     *             If the error can not be written
     362     */
    350363    private static void sendForbidden(Writer out, String help) throws IOException {
    351         sendHeader(out, "403 Forbidden", "text/html", true);
    352         out.write(String.format(
    353                 RESPONSE_TEMPLATE,
    354                 "<title>Forbidden</title>",
    355                 "<h1>HTTP Error 403: Forbidden</h1>" +
    356                 (help == null ? "" : "<p>"+Utils.escapeReservedCharactersHTML(help) + "</p>")
    357         ));
    358         out.flush();
     364        sendError(out, 403, "Forbidden", help);
    359365    }
    360366
     
    367373     */
    368374    private static void sendBadRequest(Writer out, String help) throws IOException {
    369         sendHeader(out, "400 Bad Request", "text/html", true);
    370         out.write(String.format(
    371                 RESPONSE_TEMPLATE,
    372                 "<title>Bad Request</title>",
    373                 "<h1>HTTP Error 400: Bad Request</h1>" +
    374                 (help == null ? "" : ("<p>" + Utils.escapeReservedCharactersHTML(help) + "</p>"))
    375         ));
    376         out.flush();
     375        sendError(out, 400, "Bad Request", help);
    377376    }
    378377
Note: See TracChangeset for help on using the changeset viewer.