CORDA-2632 - Handle exceptions when file does not exist. (#4967)

* corda-2632 Added catch clause to handle argument exceptions and display the proper error.

* Created specific exception for when the file is missing instead of the general catch all exception.

* Changes according to code review.
This commit is contained in:
Stefan Iliev 2019-04-25 17:23:11 +01:00 committed by Katelyn Baker
parent 6b3998c7ae
commit d67968fa88
2 changed files with 7 additions and 2 deletions

View File

@ -127,7 +127,7 @@ open class StringToMethodCallParser<in T : Any> @JvmOverloads constructor(
return method.parameters.mapIndexed { index, param ->
when {
param.isNamePresent -> param.name
// index + 1 because the first Kotlin reflection param is 'this', but that doesn't match Java reflection.
// index + 1 because the first Kotlin reflection param is 'this', but that doesn't match Java reflection.
kf != null -> kf.parameters[index + 1].name ?: throw UnparseableCallException.ReflectionDataMissing(method.name, index)
else -> throw UnparseableCallException.ReflectionDataMissing(method.name, index)
}
@ -153,6 +153,7 @@ open class StringToMethodCallParser<in T : Any> @JvmOverloads constructor(
class MissingParameter(methodName: String, val paramName: String, command: String) : UnparseableCallException("Parameter $paramName missing from attempt to invoke $methodName in command: $command")
class TooManyParameters(methodName: String, command: String) : UnparseableCallException("Too many parameters provided for $methodName: $command")
class ReflectionDataMissing(methodName: String, argIndex: Int) : UnparseableCallException("Method $methodName missing parameter name at index $argIndex")
class NoSuchFile(filename: String) : UnparseableCallException("File $filename not found")
class FailedParse(e: Exception) : UnparseableCallException(e.message ?: e.toString(), e)
}
@ -200,6 +201,8 @@ open class StringToMethodCallParser<in T : Any> @JvmOverloads constructor(
val entryType = om.typeFactory.constructType(argType)
try {
om.readValue<Any>(entry.traverse(om), entryType)
} catch (e: java.nio.file.NoSuchFileException) {
throw UnparseableCallException.NoSuchFile(e.file ?: entry.toString())
} catch (e: Exception) {
throw UnparseableCallException.FailedParse(e)
}

View File

@ -499,7 +499,9 @@ object InteractiveShell {
}
} catch (e: StringToMethodCallParser.UnparseableCallException) {
out.println(e.message, Color.red)
out.println("Please try 'man run' to learn what syntax is acceptable")
if (e !is StringToMethodCallParser.UnparseableCallException.NoSuchFile) {
out.println("Please try 'man run' to learn what syntax is acceptable")
}
} catch (e: Exception) {
out.println("RPC failed: ${e.rootCause}", Color.red)
} finally {