1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 08:27:12 -07:00

Handle errors better in KobaltServer.

This commit is contained in:
Cedric Beust 2015-10-31 12:28:44 -07:00
parent 00722cc637
commit d26ae330a7
3 changed files with 25 additions and 15 deletions

View file

@ -120,13 +120,14 @@ private class Main @Inject constructor(
try {
allProjects = buildFileCompilerFactory.create(listOf(buildFile)).compileBuildFiles(args)
} catch(ex: Throwable) {
error("Couldn't build", ex)
log(2, "Couldn't parse preBuildScript.jar: ${ex.message}")
if (! File(".kobalt").deleteRecursively()) {
warn("Couldn't delete .kobalt, please delete it manually")
} else {
log(1, "Deleted .kobalt")
allProjects = buildFileCompilerFactory.create(listOf(buildFile)).compileBuildFiles(args)
}
// if (! File(".kobalt").deleteRecursively()) {
// warn("Couldn't delete .kobalt, please delete it manually")
// } else {
// log(1, "Deleted .kobalt")
// allProjects = buildFileCompilerFactory.create(listOf(buildFile)).compileBuildFiles(args)
// }
}
if (args.tasks) {

View file

@ -6,13 +6,13 @@ import com.beust.kobalt.misc.log
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import javax.inject.Inject
import com.google.inject.Singleton
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.PrintWriter
import java.net.ServerSocket
import java.net.SocketException
import javax.inject.Inject
/**
* All commands implement this interface.
@ -51,7 +51,7 @@ interface ICommandSender {
* it into a Kotlin *Data class. The downside of this approach is a double parsing,
* but since the data part is parsed as a string first, this is probably not a huge deal.
*/
class CommandData(val name: String, val data: String)
class CommandData(val name: String, val data: String?, val error: String? = null)
@Singleton
public class KobaltServer @Inject constructor(val args: Args) : Runnable, ICommandSender {
@ -69,7 +69,7 @@ public class KobaltServer @Inject constructor(val args: Args) : Runnable, IComma
log(1, "Listening to port $portNumber")
var quit = false
val serverSocket = ServerSocket(portNumber)
val clientSocket = serverSocket.accept()
var clientSocket = serverSocket.accept()
while (!quit) {
outgoing = PrintWriter(clientSocket.outputStream, true)
if (pending.size > 0) {
@ -80,12 +80,14 @@ public class KobaltServer @Inject constructor(val args: Args) : Runnable, IComma
}
}
val ins = BufferedReader(InputStreamReader(clientSocket.inputStream))
var commandName: String? = null
try {
var line = ins.readLine()
while (!quit && line != null) {
log(1, "Received from client $line")
val jo = JsonParser().parse(line) as JsonObject
if ("quit" == jo.get("name").asString) {
commandName = jo.get("name").asString
if ("quit" == commandName) {
log(1, "Quitting")
quit = true
} else {
@ -99,7 +101,10 @@ public class KobaltServer @Inject constructor(val args: Args) : Runnable, IComma
}
} catch(ex: SocketException) {
log(1, "Client disconnected, resetting")
} catch(ex: Exception) {
clientSocket = serverSocket.accept()
} catch(ex: Throwable) {
ex.printStackTrace()
sendData(CommandData(commandName!!, null, ex.message))
log(1, "Command failed: ${ex.message}")
}
}

View file

@ -199,10 +199,14 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b
cls.methods.forEach { method ->
// Invoke vals and see if they return a Project
if (method.name.startsWith("get") && Modifier.isStatic(method.modifiers)) {
val r = method.invoke(null)
if (r is Project) {
log(2, "Found project $r in class $cls")
projects.add(r)
try {
val r = method.invoke(null)
if (r is Project) {
log(2, "Found project $r in class $cls")
projects.add(r)
}
} catch(ex: Throwable) {
throw ex.cause ?: KobaltException(ex)
}
} else {
val taskAnnotation = method.getAnnotation(Task::class.java)