diff options
author | Simon Rettberg | 2023-07-25 11:11:34 +0200 |
---|---|---|
committer | Simon Rettberg | 2023-07-25 11:11:34 +0200 |
commit | b89062913d187cc0a5f47cd2cbbc017202f5910c (patch) | |
tree | 5d3cd95730a5029574799f1805f85eaf0169dc7c | |
parent | Add tasks for apt update and upgrade (diff) | |
download | tmlite-bwlp-b89062913d187cc0a5f47cd2cbbc017202f5910c.tar.gz tmlite-bwlp-b89062913d187cc0a5f47cd2cbbc017202f5910c.tar.xz tmlite-bwlp-b89062913d187cc0a5f47cd2cbbc017202f5910c.zip |
[DownloadText] Better handling of errors
-rw-r--r-- | src/main/java/org/openslx/taskmanager/tasks/DownloadText.java | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/src/main/java/org/openslx/taskmanager/tasks/DownloadText.java b/src/main/java/org/openslx/taskmanager/tasks/DownloadText.java index 098b284..bf185bd 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/DownloadText.java +++ b/src/main/java/org/openslx/taskmanager/tasks/DownloadText.java @@ -2,7 +2,8 @@ package org.openslx.taskmanager.tasks; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; -import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.nio.charset.StandardCharsets; @@ -33,6 +34,28 @@ public class DownloadText extends AbstractTask return true; } + private String getError( URLConnection connection, boolean close ) + { + if ( ! ( connection instanceof HttpURLConnection ) ) + return null; + + StringBuilder sb = new StringBuilder(); + InputStream es = ( (HttpURLConnection)connection ).getErrorStream(); + final byte data[] = new byte[ 9000 ]; + try { + while ( es.read( data, 0, data.length ) != -1 ) { + if ( sb.length() < 1024 ) { + sb.append( new String( data, StandardCharsets.UTF_8 ) ); + } + } + } catch ( Exception e ) { + } + if ( close ) { + Util.multiClose( es ); + } + return sb.toString(); + } + @Override protected boolean execute() { @@ -61,10 +84,11 @@ public class DownloadText extends AbstractTask } status.content = baos.toString( StandardCharsets.UTF_8.name() ); return true; - } catch ( IOException e ) { - status.error = "Download error: " + e.toString(); + } catch ( Exception e ) { + status.error = "Download error: " + e.toString() + "\n" + getError( connection, false ); return false; } finally { + getError( connection, true ); Util.multiClose( in ); } } |