Reputation: 1761
I have a nested future in my Scala application. I'm aware that there must always be a path to the exit/a return method - and I believe that's what's preventing this nested future.
if (validRequest) {
onComplete("a mongo query happens here") {
case Success(result) =>
if (result.isEmpty) {
if (queryType.equals("id")) {
onComplete("a different mongo query happens here") {
case Success(result) =>
complete(HttpResponse(200))
case Failure(f) =>
complete(HttpResponse(500))
}
}
} else {
complete(HttpResponse(200))
}
case Failure(f) =>
complete(HttpResponse(500))
}
} else {
complete(HttpResponse(500))
}
Previously, when this didn't support running a separate mongo query, it looked as follows;
onComplete("a mongo query") {
case Success(result) =>
complete(HttpResponse(200))
case Failure(f) =>
complete(HttpResponse(500))
}
So, much more simplistic. However, with the need for the nested query, everything appears to have gone awry. The current error being thrown back by the compiler is;
type mismatch;
[error] found : Unit
[error] required: spray.routing.RequestContext => Unit
[error] if (queryType.equals("id")) {
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
This would normally suggest to me that something is not returning - i.e: a complete() statement is not being hit.
With the pointer on the error, the only thing I could imagine is that the switch statement could have a result differing from Success/Failure, which could potentially wind up with the method never returning. I've attempted to add in a "case _ =>" segment which threw back the standard 500 error, but to no avail.
I've also attempted a more simplistic way of setting the a var with the response int (200 or 500), and throwing back the complete() response at the end including this.. but of course, the complete() statement gets hit early, and the threads never return their results as expected.
Could someone please advise me on what the best route to take towards solving this problem is?
Upvotes: 0
Views: 119
Reputation: 1153
You should debug it and look where this exception is thrown. From what i see for now:
if (queryType.equals("id")) {
onComplete("a different mongo query happens here") {
case Success(result) =>
complete(HttpResponse(200))
case Failure(f) =>
complete(HttpResponse(500))
}
}
This if don't have else, so when this condition isn't met, you have Unit. But i'm pretty sure you need to debug it
I noticed that you've said that compiler indicates error. Try to add else to this if and see if this helps
I managed to reproduce it
if (validRequest) {
onComplete("a mongo query happens here") {
case Success(result) =>
if (result.isEmpty) {
if (queryType.equals("id")) {
onComplete("a different mongo query happens here") {
case Success(result) =>
complete(HttpResponse(200))
case Failure(f) =>
complete(HttpResponse(500))
}
}
else {
complete(HttpResponse(500))
}
} else {
complete(HttpResponse(200))
}
case Failure(f) =>
complete(HttpResponse(500))
}
} else {
complete(HttpResponse(500))
}
Above code works
Upvotes: 2