Skip to content

Commit b8c291a

Browse files
committed
Fixed error handling
1 parent f6089da commit b8c291a

File tree

2 files changed

+35
-32
lines changed

2 files changed

+35
-32
lines changed

src/main/scala/de/upb/cs/swt/delphi/cli/commands/RetrieveCommand.scala

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
2121
import akka.http.scaladsl.unmarshalling.Unmarshal
2222
import akka.stream.ActorMaterializer
2323
import de.upb.cs.swt.delphi.cli.Config
24-
import de.upb.cs.swt.delphi.cli.artifacts.{RetrieveResult, SearchResult}
24+
import de.upb.cs.swt.delphi.cli.artifacts.RetrieveResult
2525
import de.upb.cs.swt.delphi.cli.artifacts.SearchResultJson._
26-
import de.upb.cs.swt.delphi.cli.commands.SearchCommand.{error, information, reportResult}
2726
import spray.json.DefaultJsonProtocol
2827

2928
import scala.concurrent.Await
@@ -38,7 +37,7 @@ import scala.util.{Failure, Success}
3837
object RetrieveCommand extends Command with SprayJsonSupport with DefaultJsonProtocol {
3938

4039

41-
override def execute(config: Config)(implicit system : ActorSystem): Unit = {
40+
override def execute(config: Config)(implicit system: ActorSystem): Unit = {
4241
implicit val ec = system.dispatcher
4342
implicit val materializer = ActorMaterializer()
4443

@@ -54,6 +53,7 @@ object RetrieveCommand extends Command with SprayJsonSupport with DefaultJsonPro
5453
config.id
5554
}
5655
}
56+
5757
val result = executeGet(
5858
s"/retrieve/$checkTarget",
5959
Map("pretty" -> "")
@@ -65,18 +65,19 @@ object RetrieveCommand extends Command with SprayJsonSupport with DefaultJsonPro
6565
} else {
6666
val unmarshalledFuture = Unmarshal(s).to[List[RetrieveResult]]
6767

68-
unmarshalledFuture.onComplete {
68+
unmarshalledFuture.transform {
69+
case Success(unmarshalled) => {
70+
val unmarshalled = Await.result(unmarshalledFuture, Duration.Inf)
71+
success(config)(s"Found ${unmarshalled.size} item(s).")
72+
reportResult(config)(unmarshalled)
73+
74+
Success(unmarshalled)
75+
}
6976
case Failure(e) => {
7077
error(config)(s)
78+
Failure(e)
7179
}
72-
case _ =>
7380
}
74-
75-
val unmarshalled = Await.result(unmarshalledFuture, Duration.Inf)
76-
success(config)(s"Found ${unmarshalled.size} item(s).")
77-
reportResult(config)(unmarshalled)
78-
79-
Await.ready(unmarshalledFuture, Duration.Inf)
8081
}
8182
})
8283
}

src/main/scala/de/upb/cs/swt/delphi/cli/commands/SearchCommand.scala

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package de.upb.cs.swt.delphi.cli.commands
1818

19-
2019
import java.util.concurrent.TimeUnit
2120

2221
import akka.actor.ActorSystem
@@ -34,7 +33,7 @@ import spray.json.DefaultJsonProtocol
3433

3534
import scala.concurrent.duration._
3635
import scala.concurrent.{Await, Future}
37-
import scala.util.Failure
36+
import scala.util.{Failure, Success}
3837

3938
object SearchCommand extends Command with SprayJsonSupport with DefaultJsonProtocol {
4039
/**
@@ -50,8 +49,8 @@ object SearchCommand extends Command with SprayJsonSupport with DefaultJsonProto
5049

5150
information(config)(s"Searching for artifacts matching ${'"'}$query${'"'}.")
5251
val start = System.nanoTime()
53-
implicit val queryFormat = jsonFormat2(Query)
5452

53+
implicit val queryFormat = jsonFormat2(Query)
5554
val baseUri = Uri(config.server)
5655
val prettyParam = Map("pretty" -> "")
5756
val searchUri = baseUri.withPath(baseUri.path + "/search").withQuery(akka.http.scaladsl.model.Uri.Query(prettyParam))
@@ -79,33 +78,36 @@ object SearchCommand extends Command with SprayJsonSupport with DefaultJsonProto
7978
if (config.raw || result.equals("")) {
8079
reportResult(config)(result)
8180
} else {
82-
8381
val unmarshalledFuture = Unmarshal(result).to[List[SearchResult]]
8482

85-
unmarshalledFuture.onComplete {
83+
val processFuture = unmarshalledFuture.transform {
84+
case Success(unmarshalled) => {
85+
processResults(config, unmarshalled, took)
86+
Success(unmarshalled)
87+
}
8688
case Failure(e) => {
8789
error(config)(result)
90+
Failure(e)
8891
}
89-
case _ =>
9092
}
93+
}
94+
}
9195

92-
val unmarshalled = Await.result(unmarshalledFuture, Duration.Inf)
93-
val capMessage = {
94-
config.limit match {
95-
case Some(limit) if (limit <= unmarshalled.size)
96-
=> s"Results may be capped by result limit set to $limit."
97-
case None if (unmarshalled.size >= 50)
98-
=> "Results may be capped by default limit of 50 returned results. Use --limit to extend the result set."
99-
case _
100-
=> ""
101-
}
96+
private def processResults(config: Config, results: List[SearchResult], queryRuntime: Double) = {
97+
val capMessage = {
98+
config.limit match {
99+
case Some(limit) if (limit <= results.size)
100+
=> s"Results may be capped by result limit set to $limit."
101+
case None if (results.size >= 50)
102+
=> "Results may be capped by default limit of 50 returned results. Use --limit to extend the result set."
103+
case _
104+
=> ""
102105
}
103-
success(config)(s"Found ${unmarshalled.size} item(s). $capMessage")
104-
reportResult(config)(unmarshalled)
105-
106-
107-
information(config)(f"Query took $took%.2fs.")
108106
}
107+
success(config)(s"Found ${results.size} item(s). $capMessage")
108+
reportResult(config)(results)
109+
110+
information(config)(f"Query took $queryRuntime%.2fs.")
109111
}
110112

111113
case class Query(query: String,

0 commit comments

Comments
 (0)