Skip to content

Commit 676297e

Browse files
committed
better error formatting with CranberriRuntimeError
1 parent 0c7c016 commit 676297e

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/RunningScript.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ open class RunningScript(private val computer: Computer, val script: Script) {
3434
fun handleError(e: Exception) {
3535
computer.status = Computer.Status.ERROR
3636
e.cause?.printStackTrace() ?: e.printStackTrace()
37-
computer.runningScript?.logger?.printError(e.cause.toString())
37+
computer.runningScript?.logger?.printError(if (e.cause is CranberriRuntimeError) e.cause!!.message!! else e.cause.toString())
3838
}
3939

4040
try { script.invokeSetup() }
@@ -72,7 +72,7 @@ class ArduinoModeRunningScript(computer: Computer, script: Script) : RunningScri
7272

7373
fun setPinMode(pin: Int, mode: Arduino.PinMode) {
7474
if ((mode == Arduino.PinMode.INPUT && pins[pin-1] !is InputPin) || (mode == Arduino.PinMode.OUTPUT && pins[pin-1] !is OutputPin)) {
75-
throw Exception("Tried to set to wrong pin mode!")
75+
throw CranberriRuntimeError("Tried to set to wrong pin mode!")
7676
}
7777
pinModesSet += pin
7878
}
@@ -104,4 +104,6 @@ class ArduinoModeRunningScript(computer: Computer, script: Script) : RunningScri
104104
}
105105
}
106106
}
107-
}
107+
}
108+
109+
class CranberriRuntimeError(msg: String): Exception(msg)

cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/runtime/api/IO.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ object IO {
3131
assertModeSet(pin)
3232

3333
runningScript.pins[pin-1].let {
34-
if (it is OutputPin) it.writeValue(value) else throw Exception("Tried to write to input pin!")
34+
if (it is OutputPin) it.writeValue(value) else throw CranberriRuntimeError("Tried to write to input pin!")
3535
}
3636

3737
val valueStr = when (value) {
@@ -47,13 +47,13 @@ object IO {
4747

4848
runningScript.logger.printDebug("in $pin")
4949
runningScript.pins[pin-1].let {
50-
if (it is InputPin) return it.readValue() else throw Exception("Tried to write to input pin!")
50+
if (it is InputPin) return it.readValue() else throw CranberriRuntimeError("Tried to write to input pin!")
5151
}
5252
}
5353

5454
private fun assertModeSet(pin: Int) {
5555
val runningScript = getComputer()?.runningScript ?: return
56-
if (runningScript is ArduinoModeRunningScript && !runningScript.pinModesSet.contains(pin)) throw Exception("Tried to access pin without mode set!")
56+
if (runningScript is ArduinoModeRunningScript && !runningScript.pinModesSet.contains(pin)) throw CranberriRuntimeError("Tried to access pin without mode set!")
5757
}
5858
}
5959

@@ -64,15 +64,15 @@ object Arduino {
6464
}
6565

6666
@JvmStatic fun pinMode(pin: Int, mode: PinMode) {
67-
if (getScriptContext() != "setup") throw Exception("You can only call pinMode() from within setup()!")
67+
if (getScriptContext() != "setup") throw CranberriRuntimeError("You can only call pinMode() from within setup()!")
6868

6969
val runningScript = (getComputer()?.runningScript as ArduinoModeRunningScript?) ?: return
7070
runningScript.setPinMode(pin, mode)
7171
}
7272

7373
class Delay(ticks: Int) {
7474
init {
75-
if (getScriptContext() != "loop") throw Exception("You can only call delay() from within loop()!")
75+
if (getScriptContext() != "loop") throw CranberriRuntimeError("You can only call delay() from within loop()!")
7676

7777
val runningScript = getComputer()?.runningScript as ArduinoModeRunningScript?
7878
if (runningScript != null) {

cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/runtime/compilation/ProjectCompiler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ object ProjectCompiler {
4141
lookForSourceFiles(File("$PROJECTS_ROOT/$projectName/lib"), false)
4242

4343
val manifest = ProjectManifest.read(File("$PROJECTS_ROOT/$projectName/project.yaml"))
44-
if (manifest.arduinoMode && (manifest.projectType != ProjectManifest.ProjectType.SIMPLE || manifest.language != ProjectManifest.ProjectLanguage.JAVA)) throw Exception("Invalid project configuration: Arduino mode is only allowed for Java/simpe projects!")
44+
if (manifest.arduinoMode && (manifest.projectType != ProjectManifest.ProjectType.SIMPLE || manifest.language != ProjectManifest.ProjectLanguage.JAVA)) throw Exception("Invalid project configuration: Arduino mode is only allowed for Java/simple projects!")
4545

4646
val compiler = when (manifest.projectType) {
4747
ProjectManifest.ProjectType.SIMPLE -> SimpleProjectCompiler

0 commit comments

Comments
 (0)