Changeset 784 in josm


Ignore:
Timestamp:
Aug 15, 2008 8:50:36 PM (5 years ago)
Author:
stoecker
Message:

Fix exception in uploading. Closes #601. Patch by Florian Heer

Location:
trunk/src/org/openstreetmap/josm/io
Files:
1 added
1 edited

Legend:

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

    r763 r784  
    1010import java.io.InputStreamReader; 
    1111import java.io.OutputStream; 
     12import java.net.ConnectException; 
    1213import java.net.HttpURLConnection; 
    1314import java.net.URL; 
     
    109110                        comment = null; 
    110111                } 
    111                 if( useChangesets && !startChangeset(10, comment) ) 
     112                try { 
     113                        if( useChangesets && !startChangeset(10, comment) ) 
     114                                return; 
     115                } 
     116                catch (OsmTransferException ex) { 
     117                        dealWithTransferException (ex); 
    112118                        return; 
     119                } 
    113120                 
    114121                NameVisitor v = new NameVisitor(); 
     
    127134                                Main.pleaseWaitDlg.progress.setValue(progress+1); 
    128135                        } 
    129                         if( useChangesets ) stopChangeset(10); 
     136                                if( useChangesets )  
     137                                        stopChangeset(10); 
    130138                } catch (RuntimeException e) { 
    131                         if( useChangesets ) stopChangeset(10); 
     139                        try { 
     140                                if( useChangesets ) stopChangeset(10); 
     141                        } 
     142                        catch (OsmTransferException ex) { 
     143                                dealWithTransferException(ex); 
     144                        } 
    132145                        e.printStackTrace(); 
    133146                        throw new SAXException(tr("An error occoured: {0}",e.getMessage())); 
     147                } 
     148                catch (OsmTransferException e) { 
     149                        try { 
     150                                if( useChangesets ) stopChangeset(10); 
     151                        } 
     152                        catch (OsmTransferException ex) { 
     153                                dealWithTransferException(ex);   
     154                        } 
     155                        dealWithTransferException(e); 
    134156                } 
    135157        } 
     
    146168         * can fix the issue where hitting cancel doesn't do anything while 
    147169         * retrying. - Mv0 Apr 2008 
    148          */ 
    149         private boolean startChangeset(int retries, String comment) { 
     170         *  
     171         * Cancelling has an effect now, maybe it does not always catch on. Florian Heer, Aug 08 
     172         */ 
     173        private boolean startChangeset(int retries, String comment) throws OsmTransferException { 
    150174                Main.pleaseWaitDlg.currentAction.setText(tr("Opening changeset...")); 
    151175                changeset = new Changeset(); 
     
    202226                                        OsmWriter.output(o, changeset); 
    203227                                        System.out.println(new String(o.toByteArray(), "UTF-8").toString()); 
    204                                         throw new RuntimeException(retCode+" "+retMsg); 
     228                                        //throw new RuntimeException(retCode+" "+retMsg); 
     229                                        throw new OsmTransferException (retCode + " " + retMsg); 
    205230                                } 
    206231                        } 
    207232                } catch (UnknownHostException e) { 
    208                         throw new RuntimeException(tr("Unknown host")+": "+e.getMessage(), e); 
     233                        //throw new RuntimeException(tr("Unknown host")+": "+e.getMessage(), e); 
     234                        throw new OsmTransferException(tr("Unknown host")+": "+e.getMessage(), e); 
    209235                } catch(SocketTimeoutException e) { 
    210236                        System.out.println(" timed out, retries left: " + retries); 
     
    214240                                startChangeset(retries, comment); 
    215241                        else 
    216                                 throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
    217                 } catch (Exception e) { 
     242                                // throw new RuntimeException (e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
     243                                throw new OsmTransferException (e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
     244                } 
     245                catch (ConnectException e) { 
     246                        System.out.println(" timed out, retries left: " + retries); 
    218247                        if (cancel) 
    219248                                return false; // assume cancel 
     249                        if (retries-- > 0) 
     250                                startChangeset(retries, comment); 
     251                        else 
     252                                // throw new RuntimeException (e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
     253                                throw new OsmTransferException (e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
     254                } 
     255                 
     256                catch (Exception e) { 
     257                        if (cancel) 
     258                                return false; // assume cancel 
     259                        if (e instanceof OsmTransferException) 
     260                                throw (OsmTransferException)e; 
    220261                        if (e instanceof RuntimeException) 
    221262                                throw (RuntimeException)e; 
     
    225266        } 
    226267 
    227         private void stopChangeset(int retries) { 
     268        private void stopChangeset(int retries) throws OsmTransferException { 
    228269                Main.pleaseWaitDlg.currentAction.setText(tr("Closing changeset...")); 
    229270                try { 
     
    278319                                        OsmWriter.output(o, changeset); 
    279320                                        System.out.println(new String(o.toByteArray(), "UTF-8").toString()); 
    280                                         throw new RuntimeException(retCode+" "+retMsg); 
     321                                        //throw new RuntimeException(retCode+" "+retMsg); 
     322                                        throw new OsmTransferException(retCode+" "+retMsg); 
    281323                                } 
    282324                        } 
    283325                } catch (UnknownHostException e) { 
    284                         throw new RuntimeException(tr("Unknown host")+": "+e.getMessage(), e); 
     326                        //throw new RuntimeException(tr("Unknown host")+": "+e.getMessage(), e); 
     327                        throw new OsmTransferException(tr("Unknown host")+": "+e.getMessage(), e); 
    285328                } catch(SocketTimeoutException e) { 
    286329                        System.out.println(" timed out, retries left: " + retries); 
     
    290333                                stopChangeset(retries); 
    291334                        else 
    292                                 throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
     335                                //throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
     336                                throw new OsmTransferException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
     337                } catch(ConnectException e) { 
     338                        System.out.println(" timed out, retries left: " + retries); 
     339                        if (cancel) 
     340                                return; // assume cancel 
     341                        if (retries-- > 0) 
     342                                stopChangeset(retries); 
     343                        else 
     344                                //throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
     345                                throw new OsmTransferException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
    293346                } catch (Exception e) { 
    294347                        if (cancel) 
    295348                                return; // assume cancel 
     349                        if (e instanceof OsmTransferException) 
     350                                throw (OsmTransferException)e; 
    296351                        if (e instanceof RuntimeException) 
    297352                                throw (RuntimeException)e; 
     
    361416         */ 
    362417        private void sendRequestRetry(String requestMethod, String urlSuffix, 
    363                         OsmPrimitive osm, OsmWriterInterface body, int retries) { 
     418                        OsmPrimitive osm, OsmWriterInterface body, int retries) throws OsmTransferException { 
    364419                try { 
    365420                        if (cancel) 
     
    371426                                        urlSuffix +  
    372427                                        "/" + (osm.id==0 ? "create" : osm.id), 
    373                                         (java.net.URLStreamHandler)new MyHttpHandler()); 
     428                                        new MyHttpHandler()); 
    374429                        System.out.print("upload to: "+url+ "..." ); 
    375430                        activeConnection = (HttpURLConnection)url.openConnection(); 
     
    421476                                        OsmWriter.output(o, body); 
    422477                                        System.out.println(new String(o.toByteArray(), "UTF-8").toString()); 
    423                                         throw new RuntimeException(retCode+" "+retMsg); 
     478                                        //throw new RuntimeException(retCode+" "+retMsg); 
     479                                        throw new OsmTransferException(retCode+" "+retMsg); 
    424480                                } 
    425481                        } 
    426482                } catch (UnknownHostException e) { 
    427                         throw new RuntimeException(tr("Unknown host")+": "+e.getMessage(), e); 
     483                        //throw new RuntimeException(tr("Unknown host")+": "+e.getMessage(), e); 
     484                        throw new OsmTransferException(tr("Unknown host")+": "+e.getMessage(), e); 
    428485                } catch(SocketTimeoutException e) { 
    429486                        System.out.println(" timed out, retries left: " + retries); 
     
    433490                                sendRequestRetry(requestMethod, urlSuffix, osm, body, retries); 
    434491                        else 
    435                                 throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
     492                                //throw new RuntimeException (e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
     493                                throw new OsmTransferException (e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
     494                } catch(ConnectException e) { 
     495                        System.out.println(" timed out, retries left: " + retries); 
     496                        if (cancel) 
     497                                return; // assume cancel 
     498                        if (retries-- > 0) 
     499                                sendRequestRetry(requestMethod, urlSuffix, osm, body, retries); 
     500                        else 
     501                                //throw new RuntimeException (e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
     502                                throw new OsmTransferException (e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
    436503                } catch (Exception e) { 
    437504                        if (cancel) 
    438505                                return; // assume cancel 
     506                        if (e instanceof OsmTransferException) 
     507                                throw (OsmTransferException)e; 
    439508                        if (e instanceof RuntimeException) 
    440509                                throw (RuntimeException)e; 
     
    442511                } 
    443512        } 
     513         
    444514        private void sendRequest(String requestMethod, String urlSuffix, 
    445                         OsmPrimitive osm, boolean addBody) { 
     515                        OsmPrimitive osm, boolean addBody)  { 
    446516                XmlWriter.OsmWriterInterface body = null; 
    447517                if (addBody) { 
    448518                                body = new OsmWriter.Single(osm, true, changeset); 
    449519                } 
    450                 sendRequestRetry(requestMethod, urlSuffix, osm, body, 10); 
     520                try { 
     521                        sendRequestRetry(requestMethod, urlSuffix, osm, body, 10); 
     522                } 
     523                catch (OsmTransferException e) { 
     524                        dealWithTransferException (e); 
     525                } 
     526        } 
     527         
     528        private void dealWithTransferException (OsmTransferException e) { 
     529                Main.pleaseWaitDlg.currentAction.setText(tr("Transfer aborted due to error (will wait now 5 seconds):") + e.getMessage()); 
     530                cancel = true; 
     531                try { 
     532                        Thread.sleep(5000); 
     533                } 
     534                catch (InterruptedException ex) {} 
    451535        } 
    452536} 
Note: See TracChangeset for help on using the changeset viewer.