Introduce basic error handling
This commit is contained in:
@@ -3,7 +3,7 @@ package io.smnp.ext.lang.method
|
||||
import io.smnp.callable.method.Method
|
||||
import io.smnp.callable.method.MethodDefinitionTool
|
||||
import io.smnp.callable.signature.Signature.Companion.simple
|
||||
import io.smnp.error.RuntimeException
|
||||
import io.smnp.error.EvaluationException
|
||||
import io.smnp.type.enumeration.DataType.INT
|
||||
import io.smnp.type.enumeration.DataType.STRING
|
||||
import io.smnp.type.matcher.Matcher.Companion.ofType
|
||||
@@ -12,7 +12,7 @@ import io.smnp.type.model.Value
|
||||
class CharAtMethod : Method(ofType(STRING),"charAt") {
|
||||
override fun define(new: MethodDefinitionTool) {
|
||||
new method simple(ofType(INT)) body { _, obj, (index) ->
|
||||
Value.string((obj.value!! as String).getOrNull(index.value!! as Int)?.toString() ?: throw RuntimeException("Index '${index.value!!}' runs out of string bounds"))
|
||||
Value.string((obj.value!! as String).getOrNull(index.value!! as Int)?.toString() ?: throw EvaluationException("Index '${index.value!!}' runs out of string bounds"))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package io.smnp.ext.lang.method
|
||||
import io.smnp.callable.method.Method
|
||||
import io.smnp.callable.method.MethodDefinitionTool
|
||||
import io.smnp.callable.signature.Signature.Companion.simple
|
||||
import io.smnp.error.RuntimeException
|
||||
import io.smnp.error.EvaluationException
|
||||
import io.smnp.type.enumeration.DataType.INT
|
||||
import io.smnp.type.enumeration.DataType.LIST
|
||||
import io.smnp.type.matcher.Matcher.Companion.ofType
|
||||
@@ -16,7 +16,7 @@ class ListAccessMethod : Method(ofType(LIST), "get") {
|
||||
val i = index.value!! as Int
|
||||
|
||||
if(i >= list.size) {
|
||||
throw RuntimeException("Index '$i' runs out of array bounds")
|
||||
throw EvaluationException("Index '$i' runs out of array bounds")
|
||||
}
|
||||
|
||||
list[i]
|
||||
|
||||
@@ -3,7 +3,7 @@ package io.smnp.ext.lang.method
|
||||
import io.smnp.callable.method.Method
|
||||
import io.smnp.callable.method.MethodDefinitionTool
|
||||
import io.smnp.callable.signature.Signature.Companion.simple
|
||||
import io.smnp.error.RuntimeException
|
||||
import io.smnp.error.EvaluationException
|
||||
import io.smnp.type.enumeration.DataType.MAP
|
||||
import io.smnp.type.matcher.Matcher.Companion.allTypes
|
||||
import io.smnp.type.matcher.Matcher.Companion.ofType
|
||||
@@ -13,7 +13,7 @@ class MapAccessMethod : Method(ofType(MAP), "get") {
|
||||
override fun define(new: MethodDefinitionTool) {
|
||||
new method simple(allTypes()) body { _, obj, (key) ->
|
||||
val map = (obj.value!! as Map<Value, Value>)
|
||||
map[key] ?: throw RuntimeException("Key '${key.value!!}' not found")
|
||||
map[key] ?: throw EvaluationException("Key '${key.value!!}' not found")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user