From c25a6df13f28225e600273d5b2a67301aeef90a9 Mon Sep 17 00:00:00 2001 From: Faraphel Date: Mon, 24 Jun 2024 10:50:57 +0200 Subject: [PATCH] added a new exception for missing environment variable --- src/main/kotlin/fr/faraphel/m1_pe_kafka/Main.kt | 6 ++++-- .../m1_pe_kafka/error/MissingEnvironmentException.kt | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/fr/faraphel/m1_pe_kafka/error/MissingEnvironmentException.kt diff --git a/src/main/kotlin/fr/faraphel/m1_pe_kafka/Main.kt b/src/main/kotlin/fr/faraphel/m1_pe_kafka/Main.kt index 8bed7f0..c027250 100644 --- a/src/main/kotlin/fr/faraphel/m1_pe_kafka/Main.kt +++ b/src/main/kotlin/fr/faraphel/m1_pe_kafka/Main.kt @@ -1,5 +1,6 @@ package fr.faraphel.m1_pe_kafka +import fr.faraphel.m1_pe_kafka.error.MissingEnvironmentException import fr.faraphel.m1_pe_kafka.kafka.AdminUtils import fr.faraphel.m1_pe_kafka.kafka.PrintConsumer import fr.faraphel.m1_pe_kafka.kafka.Converter @@ -19,12 +20,13 @@ class Main : QuarkusApplication { /** * The entrypoint of the program * @param args command line arguments + * @throws MissingEnvironmentException an environment variable from the configuration is missing * @return the result code of the program */ override fun run(vararg args: String?): Int { // get the kafka server address val kafkaServer = System.getenv("KAFKA_BOOTSTRAP_SERVERS") - ?: throw IllegalArgumentException("Missing environment variable: KAFKA_BOOTSTRAP_SERVERS") + ?: throw MissingEnvironmentException("KAFKA_BOOTSTRAP_SERVERS") // get the topics name val topicTemperatureCelsius: String = System.getenv("TOPIC_TEMPERATURE_CELSIUS") @@ -39,7 +41,7 @@ class Main : QuarkusApplication { // get the location of the temperature to get val location = System.getenv("TEMPERATURE_LOCATION") - ?: throw IllegalArgumentException("Missing environment variable: TEMPERATURE_LOCATION") + ?: throw MissingEnvironmentException("TEMPERATURE_LOCATION") // parse the location to get the latitude and longitude val (latitude, longitude) = location.split(",").map { coordinate -> coordinate.trim().toDouble() } diff --git a/src/main/kotlin/fr/faraphel/m1_pe_kafka/error/MissingEnvironmentException.kt b/src/main/kotlin/fr/faraphel/m1_pe_kafka/error/MissingEnvironmentException.kt new file mode 100644 index 0000000..5b034da --- /dev/null +++ b/src/main/kotlin/fr/faraphel/m1_pe_kafka/error/MissingEnvironmentException.kt @@ -0,0 +1,9 @@ +package fr.faraphel.m1_pe_kafka.error + + +/** + * An HTTP exception. + * Might be raised if a required environment variable is missing. + */ +class MissingEnvironmentException(name: String) + : Exception("Missing environment variable: $name")