Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(WIP) Improve error handling when detecting processor execution failure #2

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 44 additions & 33 deletions src/main/scala/us/jubat/jubaql_client/JubaQLClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,14 @@ object JubaQLClient {
}
})

// login if session id is not given
val sessionId = sessionIdParameter match {
case Some(session) =>
session
case _ =>
val serverResponse = getSessionId(hostname, port)
if (serverResponse.isEmpty) {
System.exit(1)
}
serverResponse.get
val serverResponse = sessionIdParameter match {
case Some(session) => getSessionId(hostname, port, session)
case _ => getSessionId(hostname, port)
}
if (serverResponse.isEmpty) {
System.exit(1)
}
val sessionId = serverResponse.get
Console.err.println("Using session id \"%s\"".format(sessionId))

// read from stdin or the given scriptfile
Expand Down Expand Up @@ -104,33 +101,47 @@ object JubaQLClient {
System.exit(0)
}

/** Gets a session id from a JubaQL gateway server.
*
* @return some session id if login was successful
*/
/**
* Gets a session id from a JubaQL gateway server.
*
* @return some session id if login was successful
*/
def getSessionId(hostname: String, port: Int,
err: PrintStream = Console.err): Option[String] = {
sessionId: String = null, err: PrintStream = Console.err): Option[String] = {
val url = :/(hostname, port) / "login"
val req = Http(url.POST OK as.String)
val req = sessionId match {
case null => Http(url.POST)
case _ => {
val payloadData = ("session_id" -> sessionId)
val json: String = compact(render(payloadData))
Http(url << json)
}
}
req.either.apply() match {
case Right(resp) =>
if (resp.getStatusCode / 100 != 2) {
println("[ERROR/%s] %s".format(resp.getStatusCode, resp.getResponseBody))
None
} else {
val resultJson = resp.getResponseBody
parseOpt(resultJson) match {
case Some(result) =>
(result \ "session_id") match {
case JString(session_id) =>
Some(session_id)
case _ =>
err.println("[ERROR] JSON did not contain session_id")
None
}
case None =>
err.println("[ERROR] failed to parse JSON: \"%s\"".format(resultJson))
None
}
}
case Left(error) =>
// if request fails or is non-2xx, print error
// request fails with network error
err.println("[ERROR] " + error.getCause + ": " + error.getMessage)
None
case Right(resultJson) =>
parseOpt(resultJson) match {
case Some(result) =>
(result \ "session_id") match {
case JString(session_id) =>
Some(session_id)
case _ =>
err.println("[ERROR] JSON did not contain session_id")
None
}
case None =>
err.println("[ERROR] failed to parse JSON: \"%s\"".format(resultJson))
None
}
}
}

Expand All @@ -152,7 +163,7 @@ object JubaQLClient {
parseOpt(resultJson) match {
case Some(result) =>
if (resp.getStatusCode / 100 != 2) {
out.print("[ERROR/%s] ".format(resp.getStatusCode))
out.print("[ERROR/%s] %s".format(resp.getStatusCode, resp.getResponseBody))
}
result \ "result" match {
case JString(status) if status == "STATUS" =>
Expand Down Expand Up @@ -186,7 +197,7 @@ object JubaQLClient {
req.either.apply() match {
case Left(error) =>
// if request failed, print error
out.println("[ERROR] " + error.getCause + ": " + error.getMessage)
out.println("[ERROR] cause = " + error.getCause + ": message = " + error.getMessage)
out.flush()
true
case Right(continue_?) =>
Expand Down