From e86bf54d8a08604400dda318d8ca8b96e0aad1d7 Mon Sep 17 00:00:00 2001 From: Emux Date: Thu, 4 Aug 2022 12:52:44 +0300 Subject: [PATCH] TileDataSource: catch unexpected errors (#943) --- .../mbtiles/MBTilesMvtTileDataSource.java | 5 +- .../mbtiles/MBTilesBitmapTileDataSource.java | 6 +- vtm/src/org/oscim/layers/tile/TileLoader.java | 5 +- .../layers/tile/vector/VectorTileLoader.java | 6 +- .../oscim/tiling/OverzoomTileDataSource.java | 24 +++++--- .../tiling/source/UrlTileDataSource.java | 55 +++++++++---------- 6 files changed, 55 insertions(+), 46 deletions(-) diff --git a/vtm-android-mvt/src/org/oscim/android/mvt/tiling/source/mbtiles/MBTilesMvtTileDataSource.java b/vtm-android-mvt/src/org/oscim/android/mvt/tiling/source/mbtiles/MBTilesMvtTileDataSource.java index 79cf6a86..418b3e39 100644 --- a/vtm-android-mvt/src/org/oscim/android/mvt/tiling/source/mbtiles/MBTilesMvtTileDataSource.java +++ b/vtm-android-mvt/src/org/oscim/android/mvt/tiling/source/mbtiles/MBTilesMvtTileDataSource.java @@ -1,5 +1,6 @@ /* * Copyright 2019 Kostas Tzounopoulos + * Copyright 2022 devemux86 * * This program is free software: you can redistribute it and/or modify it under the * terms of the GNU Lesser General Public License as published by the Free Software @@ -27,7 +28,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.ByteArrayInputStream; -import java.io.IOException; import java.util.Collections; import java.util.List; import java.util.Locale; @@ -134,7 +134,8 @@ public class MBTilesMvtTileDataSource extends MBTilesTileDataSource { responseDataSink.completed(success ? QueryResult.SUCCESS : QueryResult.FAILED); } else responseDataSink.completed(QueryResult.TILE_NOT_FOUND); - } catch (IOException e) { + } catch (Throwable t) { + log.error(t.getMessage(), t); responseDataSink.completed(QueryResult.FAILED); } finally { if (cursor != null) diff --git a/vtm-android/src/org/oscim/android/tiling/source/mbtiles/MBTilesBitmapTileDataSource.java b/vtm-android/src/org/oscim/android/tiling/source/mbtiles/MBTilesBitmapTileDataSource.java index 7f12f4a3..ad086d7d 100644 --- a/vtm-android/src/org/oscim/android/tiling/source/mbtiles/MBTilesBitmapTileDataSource.java +++ b/vtm-android/src/org/oscim/android/tiling/source/mbtiles/MBTilesBitmapTileDataSource.java @@ -1,6 +1,6 @@ /* * Copyright 2019 Andrea Antonello - * Copyright 2019 devemux86 + * Copyright 2019-2022 devemux86 * Copyright 2019 Kostas Tzounopoulos * * This program is free software: you can redistribute it and/or modify it under the @@ -124,8 +124,8 @@ public class MBTilesBitmapTileDataSource extends MBTilesTileDataSource { Bitmap bitmap = CanvasAdapter.decodeBitmap(new ByteArrayInputStream(bytes)); sink.setTileImage(bitmap); res = QueryResult.SUCCESS; - } catch (Exception e) { - log.debug("{} invalid bitmap", tile); + } catch (Throwable t) { + log.error(t.getMessage(), t); } finally { sink.completed(res); } diff --git a/vtm/src/org/oscim/layers/tile/TileLoader.java b/vtm/src/org/oscim/layers/tile/TileLoader.java index 70da7e3a..838dec5f 100644 --- a/vtm/src/org/oscim/layers/tile/TileLoader.java +++ b/vtm/src/org/oscim/layers/tile/TileLoader.java @@ -1,5 +1,6 @@ /* * Copyright 2013 Hannes Janetzek + * Copyright 2022 devemux86 * * This file is part of the OpenScienceMap project (http://www.opensciencemap.org). * @@ -64,8 +65,8 @@ public abstract class TileLoader extends PausableThread implements ITileDataSink try { loadTile(mTile); - } catch (Exception e) { - e.printStackTrace(); + } catch (Throwable t) { + t.printStackTrace(); completed(FAILED); } } diff --git a/vtm/src/org/oscim/layers/tile/vector/VectorTileLoader.java b/vtm/src/org/oscim/layers/tile/vector/VectorTileLoader.java index 748df2b5..0c64844d 100644 --- a/vtm/src/org/oscim/layers/tile/vector/VectorTileLoader.java +++ b/vtm/src/org/oscim/layers/tile/vector/VectorTileLoader.java @@ -121,9 +121,9 @@ public class VectorTileLoader extends TileLoader implements RenderStyle.Callback } catch (NullPointerException e) { log.debug("NPE {} {}", tile, e.getMessage()); e.printStackTrace(); - } catch (Exception e) { - log.debug("{} {}", tile, e.getMessage()); - e.printStackTrace(); + } catch (Throwable t) { + log.debug("{} {}", tile, t.getMessage()); + t.printStackTrace(); return false; } return true; diff --git a/vtm/src/org/oscim/tiling/OverzoomTileDataSource.java b/vtm/src/org/oscim/tiling/OverzoomTileDataSource.java index 9eb4cea8..9e3afcaa 100644 --- a/vtm/src/org/oscim/tiling/OverzoomTileDataSource.java +++ b/vtm/src/org/oscim/tiling/OverzoomTileDataSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 devemux86 + * Copyright 2018-2022 devemux86 * * This program is free software: you can redistribute it and/or modify it under the * terms of the GNU Lesser General Public License as published by the Free Software @@ -15,9 +15,13 @@ package org.oscim.tiling; import org.oscim.layers.tile.MapTile; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class OverzoomTileDataSource implements ITileDataSource { + private static final Logger log = LoggerFactory.getLogger(OverzoomTileDataSource.class); + private final ITileDataSource tileDataSource; private final int overZoom; @@ -32,14 +36,18 @@ public class OverzoomTileDataSource implements ITileDataSource { @Override public void query(MapTile tile, ITileDataSink sink) { - MapTile mapTile = tile; - ITileDataSink dataSink = sink; - int diff = tile.zoomLevel - overZoom; - if (diff > 0) { - mapTile = new MapTile(tile.node, tile.tileX >> diff, tile.tileY >> diff, overZoom); - dataSink = new OverzoomDataSink(sink, mapTile, tile); + try { + MapTile mapTile = tile; + ITileDataSink dataSink = sink; + int diff = tile.zoomLevel - overZoom; + if (diff > 0) { + mapTile = new MapTile(tile.node, tile.tileX >> diff, tile.tileY >> diff, overZoom); + dataSink = new OverzoomDataSink(sink, mapTile, tile); + } + tileDataSource.query(mapTile, dataSink); + } catch (Throwable t) { + log.error(t.getMessage(), t); } - tileDataSource.query(mapTile, dataSink); } @Override diff --git a/vtm/src/org/oscim/tiling/source/UrlTileDataSource.java b/vtm/src/org/oscim/tiling/source/UrlTileDataSource.java index a65ecae6..865e9a6c 100644 --- a/vtm/src/org/oscim/tiling/source/UrlTileDataSource.java +++ b/vtm/src/org/oscim/tiling/source/UrlTileDataSource.java @@ -1,6 +1,6 @@ /* * Copyright 2012 Hannes Janetzek - * Copyright 2017 devemux86 + * Copyright 2017-2022 devemux86 * * This file is part of the OpenScienceMap project (http://www.opensciencemap.org). * @@ -34,12 +34,9 @@ import java.net.SocketException; import java.net.SocketTimeoutException; import java.net.UnknownHostException; -import static org.oscim.tiling.QueryResult.DELAYED; -import static org.oscim.tiling.QueryResult.FAILED; -import static org.oscim.tiling.QueryResult.SUCCESS; - public class UrlTileDataSource implements ITileDataSource { - static final Logger log = LoggerFactory.getLogger(UrlTileDataSource.class); + + private static final Logger log = LoggerFactory.getLogger(UrlTileDataSource.class); protected final HttpEngine mConn; protected final ITileDecoder mTileDecoder; @@ -57,27 +54,27 @@ public class UrlTileDataSource implements ITileDataSource { public void query(MapTile tile, ITileDataSink sink) { ITileCache cache = mTileSource.tileCache; - if (mUseCache) { - TileReader c = cache.getTile(tile); - if (c != null) { - InputStream is = c.getInputStream(); - try { - if (mTileDecoder.decode(tile, sink, is)) { - sink.completed(SUCCESS); - return; - } - } catch (IOException e) { - log.debug("{} Cache read: {}", tile, e); - } finally { - IOUtils.closeQuietly(is); - } - } - } - - QueryResult res = FAILED; + QueryResult res = QueryResult.FAILED; TileWriter cacheWriter = null; try { + if (mUseCache) { + TileReader c = cache.getTile(tile); + if (c != null) { + InputStream is = c.getInputStream(); + try { + if (mTileDecoder.decode(tile, sink, is)) { + sink.completed(QueryResult.SUCCESS); + return; + } + } catch (IOException e) { + log.debug("{} Cache read: {}", tile, e); + } finally { + IOUtils.closeQuietly(is); + } + } + } + mConn.sendRequest(tile); InputStream is = mConn.read(); if (mUseCache) { @@ -85,23 +82,25 @@ public class UrlTileDataSource implements ITileDataSource { mConn.setCache(cacheWriter.getOutputStream()); } if (mTileDecoder.decode(tile, sink, is)) - res = SUCCESS; + res = QueryResult.SUCCESS; } catch (SocketException e) { log.debug("{} Socket Error: {}", tile, e.getMessage()); } catch (SocketTimeoutException e) { log.debug("{} Socket Timeout", tile); - res = DELAYED; + res = QueryResult.DELAYED; } catch (UnknownHostException e) { log.debug("{} Unknown host: {}", tile, e.getMessage()); } catch (IOException e) { log.debug("{} Network Error: {}", tile, e.getMessage()); } catch (Exception e) { log.debug("{} Error: {}", tile, e.getMessage()); + } catch (Throwable t) { + log.error(t.getMessage(), t); } finally { - boolean ok = (res == SUCCESS); + boolean ok = (res == QueryResult.SUCCESS); if (!mConn.requestCompleted(ok) && ok) - res = FAILED; + res = QueryResult.FAILED; if (cacheWriter != null) cacheWriter.complete(ok);