From 12bac769e8e9c1e45a2f4b4d75382349fc97fd15 Mon Sep 17 00:00:00 2001 From: Konstantin Semenov Date: Fri, 20 Jan 2017 20:01:54 +0000 Subject: [PATCH] Connection error handling [#135632495] --- .../application/ApplicationRunFailed.kt | 3 +++ .../trilogy/application/TrilogyApplication.kt | 22 ++++++++++++++++++- .../application/TrilogyApplicationRunner.kt | 15 ++++++++----- .../io/pivotal/trilogy/i18n/MessageCreator.kt | 2 +- src/main/resources/messages.properties | 4 +++- 5 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 src/main/kotlin/io/pivotal/trilogy/application/ApplicationRunFailed.kt diff --git a/src/main/kotlin/io/pivotal/trilogy/application/ApplicationRunFailed.kt b/src/main/kotlin/io/pivotal/trilogy/application/ApplicationRunFailed.kt new file mode 100644 index 0000000..0c64a98 --- /dev/null +++ b/src/main/kotlin/io/pivotal/trilogy/application/ApplicationRunFailed.kt @@ -0,0 +1,3 @@ +package io.pivotal.trilogy.application + +class ApplicationRunFailed : RuntimeException() \ No newline at end of file diff --git a/src/main/kotlin/io/pivotal/trilogy/application/TrilogyApplication.kt b/src/main/kotlin/io/pivotal/trilogy/application/TrilogyApplication.kt index ab9dc1b..43e23db 100644 --- a/src/main/kotlin/io/pivotal/trilogy/application/TrilogyApplication.kt +++ b/src/main/kotlin/io/pivotal/trilogy/application/TrilogyApplication.kt @@ -1,17 +1,37 @@ package io.pivotal.trilogy.application +import io.pivotal.trilogy.i18n.MessageCreator.createErrorMessage +import org.springframework.beans.factory.UnsatisfiedDependencyException import org.springframework.boot.SpringApplication import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.context.annotation.Import +import java.net.ConnectException @SpringBootApplication @Import(TrilogyApplicationConfiguration::class) open class TrilogyApplication { + companion object { @JvmStatic fun main(args: Array) { - SpringApplication.run(TrilogyApplication::class.java, *args) + try { + SpringApplication.run(TrilogyApplication::class.java, *args) + } catch(e: IllegalStateException) { + if (e.isCausedByConnectionFault) { + println(createErrorMessage("connectionFailure", listOf("${e.cause}"))) + } + throw e + } catch (e: UnsatisfiedDependencyException) { + println(createErrorMessage("applicationUsage")) + throw e + } + } + + private val Throwable.isCausedByConnectionFault: Boolean get() { + return (this.cause?.cause is ConnectException) or (this.cause?.cause?.cause is ConnectException) } } } + + diff --git a/src/main/kotlin/io/pivotal/trilogy/application/TrilogyApplicationRunner.kt b/src/main/kotlin/io/pivotal/trilogy/application/TrilogyApplicationRunner.kt index 133b574..f39ca95 100644 --- a/src/main/kotlin/io/pivotal/trilogy/application/TrilogyApplicationRunner.kt +++ b/src/main/kotlin/io/pivotal/trilogy/application/TrilogyApplicationRunner.kt @@ -1,5 +1,6 @@ package io.pivotal.trilogy.application +import io.pivotal.trilogy.i18n.MessageCreator.createErrorMessage import io.pivotal.trilogy.parsing.TrilogyApplicationOptionsParser import io.pivotal.trilogy.reporting.TestCaseReporter import org.springframework.beans.factory.annotation.Autowired @@ -23,20 +24,22 @@ open class TrilogyApplicationRunner : ApplicationRunner { System.out.println(output) if (testResults.didFail) { suppressStacktrace = true - throw RuntimeException() + throw ApplicationRunFailed() } } catch (e: RuntimeException) { - if (!suppressStacktrace) printFailure(e.stackTrace) - throw e + if (!suppressStacktrace) printFailure(e) + throw ApplicationRunFailed() } } else { printFailure(null) + throw ApplicationRunFailed() } } - private fun printFailure(stackTrace: Array?) { - stackTrace?.forEach { frame -> System.out.println(frame) } - System.out.println("Usage: trilogy [|--project=] --db-url=") + private fun printFailure(e: Throwable?) { + println("$e") + e?.stackTrace?.forEach(::println) + System.out.println(createErrorMessage("applicationUsage")) } } diff --git a/src/main/kotlin/io/pivotal/trilogy/i18n/MessageCreator.kt b/src/main/kotlin/io/pivotal/trilogy/i18n/MessageCreator.kt index c81b783..f623d24 100644 --- a/src/main/kotlin/io/pivotal/trilogy/i18n/MessageCreator.kt +++ b/src/main/kotlin/io/pivotal/trilogy/i18n/MessageCreator.kt @@ -5,6 +5,6 @@ import java.text.MessageFormat import java.util.ResourceBundle object MessageCreator { - fun createErrorMessage(messagePath: String, messageArguments: List): String = MessageFormat(getI18nMessage(messagePath)).format(messageArguments.toTypedArray()) + fun createErrorMessage(messagePath: String, messageArguments: List = emptyList()): String = MessageFormat(getI18nMessage(messagePath)).format(messageArguments.toTypedArray()) fun getI18nMessage(name: String): String = ResourceBundle.getBundle("messages", LocaleContextHolder.getLocale()).getString(name) } \ No newline at end of file diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index 3047d80..819c001 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -8,4 +8,6 @@ testSubjectCaller.errors.mismatch.input.unexpected = Unexpected parameter(s) ''{ output.errors.mismatch = Expected value ''{0}'' in the {1} out parameter, but received ''{2}'' output.errors.forRow = Row {0} of {1}: {2} testCaseRunner.errors.missingFixture = Unable to find fixture ''{0}'' -testProjectRunner.errors.scripts.invalid = Unable to load script ''{0}'':\n{1} \ No newline at end of file +testProjectRunner.errors.scripts.invalid = Unable to load script ''{0}'':\n{1} +connectionFailure = Unable to connect to the database:\n{0} +applicationUsage = Usage: trilogy [|--project=] --db_url= --db_user= --db_password=\nThe db_url, db_user and db_password can be replaced by setting environment variables with the same name in upper case \ No newline at end of file