Improve decoration design-pattern model of exceptions
This commit is contained in:
@@ -3,6 +3,5 @@ package io.smnp.error
|
||||
import io.smnp.environment.Environment
|
||||
|
||||
class EnvironmentException(exception: SmnpException, val environment: Environment) : SmnpException(
|
||||
exception.friendlyName,
|
||||
"${exception.message}\n\nStack trace:\n${environment.stackTrace()}"
|
||||
exception.friendlyName, exception.message, exception
|
||||
)
|
||||
@@ -1,3 +1,12 @@
|
||||
package io.smnp.error
|
||||
|
||||
abstract class SmnpException(val friendlyName: String, message: String? = null) : Exception(message)
|
||||
abstract class SmnpException(val friendlyName: String, message: String? = null, val exception: SmnpException? = null) : Exception(message) {
|
||||
val exceptionChain: List<SmnpException>
|
||||
get() {
|
||||
if(exception == null) {
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
return listOf(this, *exception.exceptionChain.toTypedArray())
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import com.xenomachina.argparser.mainBody
|
||||
import io.smnp.cli.model.entity.Arguments
|
||||
import io.smnp.cli.model.enumeration.ModulesPrintMode
|
||||
import io.smnp.environment.DefaultEnvironment
|
||||
import io.smnp.error.EnvironmentException
|
||||
import io.smnp.error.PositionException
|
||||
import io.smnp.error.SmnpException
|
||||
import io.smnp.ext.DefaultModuleRegistry
|
||||
import io.smnp.interpreter.DefaultInterpreter
|
||||
@@ -41,8 +43,13 @@ fun main(args: Array<String>): Unit = mainBody {
|
||||
DefaultModuleRegistry.registeredModules().forEach { println(it) }
|
||||
}
|
||||
} catch (e: SmnpException) {
|
||||
System.err.println(e.friendlyName)
|
||||
val position = e.exceptionChain.mapNotNull { it as? PositionException }.lastOrNull()?.position ?: ""
|
||||
val stacktrace = e.exceptionChain.mapNotNull { it as? EnvironmentException }.lastOrNull()?.let {
|
||||
"\nStack trace:\n${it.environment.stackTrace()}"
|
||||
} ?: ""
|
||||
System.err.println(e.friendlyName + " " + position)
|
||||
System.err.println(e.message)
|
||||
System.err.println(stacktrace)
|
||||
exitProcess(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,5 @@ package io.smnp.error
|
||||
import io.smnp.dsl.token.model.entity.TokenPosition
|
||||
|
||||
class PositionException(exception: SmnpException, val position: TokenPosition) : SmnpException(
|
||||
"${exception.friendlyName} $position",
|
||||
exception.message
|
||||
exception.friendlyName, exception.message, exception
|
||||
)
|
||||
@@ -24,7 +24,13 @@ class DefaultInterpreter : Interpreter {
|
||||
return run(lines, environment, printTokens, printAst, dryRun)
|
||||
}
|
||||
|
||||
private fun run(lines: List<String>, environment: Environment, printTokens: Boolean, printAst: Boolean, dryRun: Boolean): Environment {
|
||||
private fun run(
|
||||
lines: List<String>,
|
||||
environment: Environment,
|
||||
printTokens: Boolean,
|
||||
printAst: Boolean,
|
||||
dryRun: Boolean
|
||||
): Environment {
|
||||
environment.loadModule("smnp.lang")
|
||||
|
||||
val tokens = tokenizer.tokenize(lines)
|
||||
@@ -44,7 +50,13 @@ class DefaultInterpreter : Interpreter {
|
||||
return environment
|
||||
}
|
||||
|
||||
fun run(file: File, environment: Environment = DefaultEnvironment(), printTokens: Boolean = false, printAst: Boolean = false, dryRun: Boolean = false): Environment {
|
||||
fun run(
|
||||
file: File,
|
||||
environment: Environment = DefaultEnvironment(),
|
||||
printTokens: Boolean = false,
|
||||
printAst: Boolean = false,
|
||||
dryRun: Boolean = false
|
||||
): Environment {
|
||||
val lines = file.readLines()
|
||||
return run(lines, environment, printTokens, printAst, dryRun)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user