Skip to content

Commit

Permalink
added error json response for broker endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Berta committed May 5, 2017
1 parent ffadcbc commit bc75719
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/scala/main/ly/stealth/mesos/kafka/cli/BrokerCli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ trait BrokerCli {

val json =
try { sendRequestObj[HttpLogResponse]("/broker/log", params) }
catch { case e: IOException => throw new Error("" + e) }
catch { case e: IOException =>throw new Error("" + e)}

val status = json.status
val content = json.content
Expand Down
14 changes: 12 additions & 2 deletions src/scala/main/ly/stealth/mesos/kafka/cli/Cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,19 @@ trait CliUtils

try { response = Source.fromInputStream(connection.getInputStream).getLines().mkString}
catch {
case e: IOException =>
if (connection.getResponseCode != 200) throw new IOException(connection.getResponseCode + " - " + connection.getResponseMessage)
case e: IOException => {
if (connection.getResponseCode != 200) {
val br = new BufferedReader(new InputStreamReader(connection.getErrorStream))
var line = br.readLine()
val body = new StringBuilder()
while(line != null){
body.append(line)
line = br.readLine()
}
throw new IOException(connection.getResponseCode + " - " + body.toString())
}
else throw e
}
}
} finally {
connection.disconnect()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ trait BrokerApiComponentImpl extends BrokerApiComponent {
Response.ok(BrokerStatusResponse(brokerNodes))
.build()
case Failure(e) =>
Response.status(Response.Status.BAD_REQUEST).build()
Response.status(Response.Status.BAD_REQUEST).entity(HttpErrorResponse(400, e.getMessage())).build()
}
}

Expand Down Expand Up @@ -120,7 +120,7 @@ trait BrokerApiComponentImpl extends BrokerApiComponent {
}

if (errors.nonEmpty) {
return Status.BadRequest(errors.mkString("; '"))
return Response.status(Response.Status.BAD_REQUEST).entity(HttpErrorResponse(400, errors.mkString("; '"))).build()
}

for (broker <- brokers) {
Expand Down Expand Up @@ -185,7 +185,7 @@ trait BrokerApiComponentImpl extends BrokerApiComponent {
Response.status(Response.Status.OK)
.entity(BrokerRemoveResponse(b.map(_.id.toString)))
.build()
case Failure(e) => Status.BadRequest(e.getMessage)
case Failure(e) => Response.status(Response.Status.BAD_REQUEST).entity(HttpErrorResponse(400, e.getMessage())).build()
}
}

Expand All @@ -205,7 +205,7 @@ trait BrokerApiComponentImpl extends BrokerApiComponent {

maybeBrokers.map(brokers => startStopBrokersImpl(brokers, start, timeout, force)) match {
case Success(s) => s
case Failure(e) => Status.BadRequest(e.getMessage)
case Failure(e) => Response.status(Response.Status.BAD_REQUEST).entity(HttpErrorResponse(400, e.getMessage())).build()
}
}

Expand Down Expand Up @@ -251,7 +251,7 @@ trait BrokerApiComponentImpl extends BrokerApiComponent {
val maybeBrokers = expandBrokers(expr)
maybeBrokers.map(b => restartBrokersImpl(b, timeout, shouldWaitForRepl)) match {
case Success(s) => s
case Failure(e) => Status.BadRequest(e.getMessage)
case Failure(e) => Response.status(Response.Status.BAD_REQUEST).entity(HttpErrorResponse(400, e.getMessage())).build()
}
}

Expand Down Expand Up @@ -342,7 +342,7 @@ trait BrokerApiComponentImpl extends BrokerApiComponent {
})
val result = maybeBrokers.map(b => brokerLogImpl(b, name, lines, timeout))
result match {
case Failure(e) => Status.BadRequest(e.getMessage)
case Failure(e) => Response.status(Response.Status.BAD_REQUEST).entity(HttpErrorResponse(400, e.getMessage())).build()
case Success(r) if r.size == 1 => Response.ok(r.head._2).build()
case Success(r) if r.size > 1 => Response.ok(r).build()
case _ => Response.noContent().build()
Expand Down Expand Up @@ -389,7 +389,7 @@ trait BrokerApiComponentImpl extends BrokerApiComponent {

addedBrokers match {
case Success(b) => Response.ok(BrokerStatusResponse(b)).build()
case Failure(f) => Status.BadRequest(f.getMessage)
case Failure(f) => Response.status(Response.Status.BAD_REQUEST).entity(HttpErrorResponse(400, f.getMessage())).build()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ package ly.stealth.mesos.kafka.scheduler.http.api
import java.lang.{Integer => JInt}
import javax.ws.rs.core.{MediaType, Response}
import javax.ws.rs.{DefaultValue, GET, POST, Path, PathParam, Produces}

import ly.stealth.mesos.kafka.scheduler.{Expr, Rebalancer}
import ly.stealth.mesos.kafka.scheduler.http.BothParam
import ly.stealth.mesos.kafka.{ListTopicsResponse, RebalanceStartResponse}
import ly.stealth.mesos.kafka.{HttpErrorResponse, ListTopicsResponse, RebalanceStartResponse}
import ly.stealth.mesos.kafka.scheduler.mesos.ClusterComponent
import net.elodina.mesos.util.Period

import scala.collection.JavaConversions._
import scala.util.Try

Expand Down Expand Up @@ -81,7 +83,7 @@ trait TopicApiComponentImpl extends TopicApiComponent {
return Status.BadRequest("options required")
if (options != null)
Option(cluster.topics.validateOptions(options)) match {
case Some(err) => return Status.BadRequest(err)
case Some(err) => return Response.status(Response.Status.BAD_REQUEST).entity(HttpErrorResponse(400, err)).build()
case _ =>
}

Expand Down

0 comments on commit bc75719

Please sign in to comment.