Skip to content
This repository has been archived by the owner on Mar 24, 2022. It is now read-only.

Commit

Permalink
Connection error handling
Browse files Browse the repository at this point in the history
[#135632495]
  • Loading branch information
Konstantin Semenov committed Jan 20, 2017
1 parent e212b68 commit 12bac76
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.pivotal.trilogy.application

class ApplicationRunFailed : RuntimeException()
Original file line number Diff line number Diff line change
@@ -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<String>) {
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)
}
}

}


Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<StackTraceElement>?) {
stackTrace?.forEach { frame -> System.out.println(frame) }
System.out.println("Usage: trilogy [<filePath>|--project=<path to trilogy test project>] --db-url=<jdbc url>")
private fun printFailure(e: Throwable?) {
println("$e")
e?.stackTrace?.forEach(::println)
System.out.println(createErrorMessage("applicationUsage"))
}
}

2 changes: 1 addition & 1 deletion src/main/kotlin/io/pivotal/trilogy/i18n/MessageCreator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import java.text.MessageFormat
import java.util.ResourceBundle

object MessageCreator {
fun createErrorMessage(messagePath: String, messageArguments: List<Any>): String = MessageFormat(getI18nMessage(messagePath)).format(messageArguments.toTypedArray())
fun createErrorMessage(messagePath: String, messageArguments: List<Any> = emptyList()): String = MessageFormat(getI18nMessage(messagePath)).format(messageArguments.toTypedArray())
fun getI18nMessage(name: String): String = ResourceBundle.getBundle("messages", LocaleContextHolder.getLocale()).getString(name)
}
4 changes: 3 additions & 1 deletion src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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}
testProjectRunner.errors.scripts.invalid = Unable to load script ''{0}'':\n{1}
connectionFailure = Unable to connect to the database:\n{0}
applicationUsage = Usage: trilogy [<filePath>|--project=<path to trilogy test project>] --db_url=<jdbc url> --db_user=<db user name> --db_password=<db user password>\nThe db_url, db_user and db_password can be replaced by setting environment variables with the same name in upper case

0 comments on commit 12bac76

Please sign in to comment.