added a new exception for missing environment variable

This commit is contained in:
Faraphel 2024-06-24 10:50:57 +02:00
parent 1c2348320a
commit c25a6df13f
2 changed files with 13 additions and 2 deletions

View file

@ -1,5 +1,6 @@
package fr.faraphel.m1_pe_kafka 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.AdminUtils
import fr.faraphel.m1_pe_kafka.kafka.PrintConsumer import fr.faraphel.m1_pe_kafka.kafka.PrintConsumer
import fr.faraphel.m1_pe_kafka.kafka.Converter import fr.faraphel.m1_pe_kafka.kafka.Converter
@ -19,12 +20,13 @@ class Main : QuarkusApplication {
/** /**
* The entrypoint of the program * The entrypoint of the program
* @param args command line arguments * @param args command line arguments
* @throws MissingEnvironmentException an environment variable from the configuration is missing
* @return the result code of the program * @return the result code of the program
*/ */
override fun run(vararg args: String?): Int { override fun run(vararg args: String?): Int {
// get the kafka server address // get the kafka server address
val kafkaServer = System.getenv("KAFKA_BOOTSTRAP_SERVERS") val kafkaServer = System.getenv("KAFKA_BOOTSTRAP_SERVERS")
?: throw IllegalArgumentException("Missing environment variable: KAFKA_BOOTSTRAP_SERVERS") ?: throw MissingEnvironmentException("KAFKA_BOOTSTRAP_SERVERS")
// get the topics name // get the topics name
val topicTemperatureCelsius: String = System.getenv("TOPIC_TEMPERATURE_CELSIUS") val topicTemperatureCelsius: String = System.getenv("TOPIC_TEMPERATURE_CELSIUS")
@ -39,7 +41,7 @@ class Main : QuarkusApplication {
// get the location of the temperature to get // get the location of the temperature to get
val location = System.getenv("TEMPERATURE_LOCATION") 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 // parse the location to get the latitude and longitude
val (latitude, longitude) = location.split(",").map { coordinate -> coordinate.trim().toDouble() } val (latitude, longitude) = location.split(",").map { coordinate -> coordinate.trim().toDouble() }

View file

@ -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")