@@ -13,6 +13,7 @@ import java.io.IOException
1313private const val DEFAULT_LOG_MAX_COUNT = 10000
1414
1515
16+ class CommandExecutionException (message : String ): Exception(message)
1617private suspend fun Process.await (
1718 onOutput : (String ) -> Unit ,
1819 onError : (String ) -> Unit
@@ -78,13 +79,13 @@ class DevChatWrapper(
7879 val errors = errorLines.joinToString(" \n " )
7980
8081 if (exitCode != 0 ) {
81- throw RuntimeException (" Command failure with exit Code: $exitCode , Errors: $errors " )
82+ throw CommandExecutionException (" Command failure with exit Code: $exitCode , Errors: $errors " )
8283 } else {
8384 outputLines.joinToString(" \n " )
8485 }
8586 } catch (e: IOException ) {
86- Log .error (" Failed to execute command: $commands , Exception: $e " )
87- throw RuntimeException ( " Failed to execute command: $commands " , e)
87+ Log .warn (" Failed to execute command: $commands , Exception: $e " )
88+ throw e
8889 }
8990 }
9091
@@ -95,15 +96,15 @@ class DevChatWrapper(
9596 ): Job {
9697 Log .info(" Executing command: ${commands.joinToString(" " )} }" )
9798 val exceptionHandler = CoroutineExceptionHandler { _, exception ->
98- Log .error (" Failed to execute command: $commands , Exception: $exception " )
99- throw RuntimeException (" Failed to execute command: $commands " , exception)
99+ Log .warn (" Failed to execute command: $commands , Exception: $exception " )
100+ throw CommandExecutionException (" Failed to execute command: $commands , $ exception" )
100101 }
101102 val cmdScope = CoroutineScope (SupervisorJob () + Dispatchers .Default )
102103
103104 return cmdScope.launch(exceptionHandler) {
104105 val exitCode = executeCommand(commands, getEnv(), onOutput, onError)
105106 if (exitCode != 0 ) {
106- throw RuntimeException (" Command failure with exit Code: $exitCode " )
107+ throw CommandExecutionException (" Command failure with exit Code: $exitCode " )
107108 }
108109 }
109110 }
@@ -117,20 +118,38 @@ class DevChatWrapper(
117118 subCommand(listOf (" prompt" ))(flags, callback)
118119 }
119120
120- val logTopic: (String , Int? ) -> JSONArray get() = {topic: String , maxCount: Int? ->
121- val num: Int = maxCount ? : DEFAULT_LOG_MAX_COUNT
122- JSON .parseArray(log(mutableListOf (
123- " topic" to topic,
124- " max-count" to num.toString()
125- ), null ))
126- }
127-
128121 val run get() = subCommand(listOf (" run" ))
129122 val log get() = subCommand(listOf (" log" ))
130123 val topic get() = subCommand(listOf (" topic" ))
131124
132- val topicList: JSONArray get() = JSON .parseArray(topic(mutableListOf (" list" to null ), null ))
133- val commandList: JSONArray get() = JSON .parseArray(run (mutableListOf (" list" to null ), null ))
125+ val topicList: JSONArray get() = try {
126+ val r = topic(mutableListOf (" list" to null ), null ) ? : " []"
127+ JSON .parseArray(r)
128+ } catch (e: Exception ) {
129+ Log .warn(" Error list topics: $e " )
130+ JSONArray ()
131+ }
132+ val commandList: JSONArray get() = try {
133+ val r = run (mutableListOf (" list" to null ), null ) ? : " []"
134+ JSON .parseArray(r)
135+ } catch (e: Exception ) {
136+ Log .warn(" Error list commands: $e " )
137+ JSONArray ()
138+ }
139+
140+ val logTopic: (String , Int? ) -> JSONArray get() = {topic: String , maxCount: Int? ->
141+ val num: Int = maxCount ? : DEFAULT_LOG_MAX_COUNT
142+ try {
143+ val r = log(mutableListOf (
144+ " topic" to topic,
145+ " max-count" to num.toString()
146+ ), null ) ? : " []"
147+ JSON .parseArray(r)
148+ } catch (e: Exception ) {
149+ Log .warn(" Error log topic: $e " )
150+ JSONArray ()
151+ }
152+ }
134153
135154
136155 fun runCommand (subCommands : List <String >? , flags : List <Pair <String , String ?>>? = null, callback : ((String ) -> Unit )? = null): String? {
@@ -143,8 +162,8 @@ class DevChatWrapper(
143162 return try {
144163 callback?.let { execCommandAsync(cmd, callback); " " } ? : execCommand(cmd)
145164 } catch (e: Exception ) {
146- Log .error (" Failed to run command $cmd : ${e.message} " )
147- throw RuntimeException (" Failed to run command $cmd " , e )
165+ Log .warn (" Failed to run command $cmd : ${e.message} " )
166+ throw CommandExecutionException (" Failed to run command $cmd , $e " )
148167 }
149168 }
150169
@@ -159,8 +178,8 @@ class DevChatWrapper(
159178 try {
160179 callback?.let { execCommandAsync(cmd, callback); " " } ? : execCommand(cmd)
161180 } catch (e: Exception ) {
162- Log .error (" Failed to run command $cmd : ${e.message} " )
163- throw RuntimeException (" Failed to run command $cmd " , e )
181+ Log .warn (" Failed to run command $cmd : ${e.message} " )
182+ throw CommandExecutionException (" Failed to run command $cmd : ${e.message} " )
164183 }
165184 }
166185 }
0 commit comments