Skip to content

Commit

Permalink
Handle http unknown host exception (#7422)
Browse files Browse the repository at this point in the history
* Handle http unknown host exception

* Update changelog

* change wording in comment

---------

Co-authored-by: Florian M <[email protected]>
  • Loading branch information
frcroth and fm3 authored Nov 6, 2023
1 parent f484cb7 commit 99f2f0a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ For upgrade instructions, please check the [migration guide](MIGRATIONS.released
- Searching the segments in the sidebar will highlight newly focused segments properly now. [#7406](https://github.com/scalableminds/webknossos/pull/7406)
- Fixed a bug when opening a task for which a mag restriction exists. The bug only occurred when the referenced mag didn't exist in the dataset. [#7403](https://github.com/scalableminds/webknossos/pull/7403)
- Fixed styling issues with the maintenance banner so that it no longer overlaps other menus, tabs, and buttons. [#7421](https://github.com/scalableminds/webknossos/pull/7421)
- Exploring HTTP uris of unknown hosts no longer causes an exception error message to be displayed. [#7422](https://github.com/scalableminds/webknossos/pull/7422)

### Removed

Expand Down
12 changes: 12 additions & 0 deletions util/src/main/scala/com/scalableminds/util/tools/Fox.scala
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,18 @@ object Fox extends FoxImplicits {

failure.msg + formatStackTrace(failure) + formatChain(failure.chain)
}

/**
* Transform a Future[T] into a Fox[T] such that if the Future contains an exception, it is turned into a Fox.failure
*/
def transformFuture[T](future: Future[T])(implicit ec: ExecutionContext): Fox[T] =
for {
fut <- future.transform {
case Success(value) => Try(Fox.successful(value))
case scala.util.Failure(e) => Try(Fox.failure(e.getMessage, Full(e)))
}
f <- fut
} yield f
}

class Fox[+A](val futureBox: Future[Box[A]])(implicit ec: ExecutionContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class HttpsDataVault(credential: Option[DataVaultCredential], ws: WSClient) exte
headerInfoCache.getOrLoad(
uri, { uri =>
for {
response <- ws.url(uri.toString).withRequestTimeout(readTimeout).head()
response <- Fox.transformFuture(ws.url(uri.toString).withRequestTimeout(readTimeout).head())
acceptsPartialRequests = response.headerValues("Accept-Ranges").contains("bytes")
dataSize = response.header("Content-Length").map(_.toLong).getOrElse(0L)
} yield (acceptsPartialRequests, dataSize)
Expand All @@ -60,19 +60,20 @@ class HttpsDataVault(credential: Option[DataVaultCredential], ws: WSClient) exte
private def getWithRange(uri: URI, range: NumericRange[Long])(implicit ec: ExecutionContext): Fox[WSResponse] =
for {
_ <- ensureRangeRequestsSupported(uri)
response <- buildRequest(uri).withHttpHeaders("Range" -> s"bytes=${range.start}-${range.end - 1}").get()
response <- Fox.transformFuture(
buildRequest(uri).withHttpHeaders("Range" -> s"bytes=${range.start}-${range.end - 1}").get())
_ = updateRangeRequestsSupportedForResponse(response)
} yield response

private def getWithSuffixRange(uri: URI, length: Long)(implicit ec: ExecutionContext): Fox[WSResponse] =
for {
_ <- ensureRangeRequestsSupported(uri)
response <- buildRequest(uri).withHttpHeaders("Range" -> s"bytes=-$length").get()
response <- Fox.transformFuture(buildRequest(uri).withHttpHeaders("Range" -> s"bytes=-$length").get())
_ = updateRangeRequestsSupportedForResponse(response)
} yield response

private def getComplete(uri: URI)(implicit ec: ExecutionContext): Fox[WSResponse] =
buildRequest(uri).get()
Fox.transformFuture(buildRequest(uri).get())

private def ensureRangeRequestsSupported(uri: URI)(implicit ec: ExecutionContext): Fox[Unit] =
for {
Expand Down

0 comments on commit 99f2f0a

Please sign in to comment.