Skip to content

Commit

Permalink
Merge pull request #13 from MaciejG604/fix-NPE-from-error-stream
Browse files Browse the repository at this point in the history
Fix npe from error stream
  • Loading branch information
alexarchambault authored Mar 21, 2023
2 parents adc4b5c + 64d54d8 commit 26f4f9c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
out/
.bsp
.idea
33 changes: 21 additions & 12 deletions publish/src/coursier/publish/upload/HttpURLConnectionUpload.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,27 @@ final case class HttpURLConnectionUpload(urlSuffix: String) extends Upload {
else if (code / 100 == 2)
None
else {
es = conn.getErrorStream
val buf = Array.ofDim[Byte](16384)
val baos = new ByteArrayOutputStream
var read = -1
while ({
read = es.read(buf); read >= 0
})
baos.write(buf, 0, read)
es.close()
// FIXME Adjust charset with headers?
val content =
Try(new String(baos.toByteArray, StandardCharsets.UTF_8)).toOption.getOrElse("")
val content = {
es = Option(conn.getErrorStream)
.orElse(Try(conn.getInputStream).toOption).orNull

if (es != null) {
val buf = Array.ofDim[Byte](16384)
val baos = new ByteArrayOutputStream
var read = -1
while ({
read = es.read(buf)
read >= 0
})
baos.write(buf, 0, read)
es.close()
// FIXME Adjust charset with headers?
Try(new String(baos.toByteArray, StandardCharsets.UTF_8))
.getOrElse("")
}
else ""
}

Some(
new Upload.Error.HttpError(
code,
Expand Down

0 comments on commit 26f4f9c

Please sign in to comment.