-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding validators and error responses.
- Loading branch information
1 parent
9c2e3bd
commit 8244f2a
Showing
9 changed files
with
141 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
PORT=8080 | ||
LOGGING_LEVEL=INFO | ||
MONGODB_URI=mongodb://mongo-node1:27017,mongo-node2:27018,mongo-node3:27019/electicity-prices | ||
SPRING_PROFILES_ACTIVE=price-sync | ||
MONGODB_URI=mongodb://localhost:27017,localhost:27018,localhost:27019/electicity-prices | ||
SPRING_PROFILES_ACTIVE=no-price-sync | ||
SYNC_START_DATE=2023-01-01 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.11.2 | ||
1.12.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
src/main/kotlin/ie/daithi/electricityprices/exceptions/DataNotAvailableYetException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
package ie.daithi.electricityprices.exceptions | ||
|
||
class DataNotAvailableYetException(s: String) : Throwable() | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.web.bind.annotation.ResponseStatus | ||
|
||
|
||
@ResponseStatus(value = HttpStatus.NOT_FOUND) | ||
class DataNotAvailableYetException(message: String) : RuntimeException(message) |
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/ie/daithi/electricityprices/exceptions/GlobalExceptionHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ie.daithi.electricityprices.exceptions | ||
|
||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.ExceptionHandler | ||
import org.springframework.web.bind.annotation.RestControllerAdvice | ||
import jakarta.validation.ConstraintViolationException | ||
import org.springframework.web.context.request.ServletWebRequest | ||
import org.springframework.web.context.request.WebRequest | ||
|
||
@RestControllerAdvice | ||
class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(ConstraintViolationException::class) | ||
fun handleConstraintViolationException( | ||
e: ConstraintViolationException, | ||
webRequest: WebRequest | ||
): ResponseEntity<Map<String, Any>> { | ||
val errorAttributes = mutableMapOf<String, Any>() | ||
errorAttributes["timestamp"] = System.currentTimeMillis() | ||
errorAttributes["status"] = HttpStatus.UNPROCESSABLE_ENTITY.value() | ||
errorAttributes["error"] = "Unprocessable Entity" | ||
errorAttributes["message"] = e.message ?: "Validation failed" | ||
errorAttributes["path"] = (webRequest as ServletWebRequest).request.requestURI | ||
return ResponseEntity(errorAttributes, HttpStatus.UNPROCESSABLE_ENTITY) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/ie/daithi/electricityprices/exceptions/UnprocessableEntityException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ie.daithi.electricityprices.exceptions | ||
|
||
import org.springframework.http.HttpStatus | ||
import org.springframework.web.bind.annotation.ResponseStatus | ||
|
||
|
||
@ResponseStatus(value = HttpStatus.UNPROCESSABLE_ENTITY) | ||
class UnprocessableEntityException(message: String) : RuntimeException(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/ie/daithi/electricityprices/web/validaton/ValidDateDay.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package ie.daithi.electricityprices.web.validaton | ||
|
||
import jakarta.validation.Constraint | ||
import jakarta.validation.ConstraintValidator | ||
import jakarta.validation.ConstraintValidatorContext | ||
import jakarta.validation.Payload | ||
import java.time.LocalDate | ||
import java.time.format.DateTimeFormatter | ||
import java.time.format.DateTimeParseException | ||
import kotlin.reflect.KClass | ||
|
||
@Target(AnnotationTarget.VALUE_PARAMETER) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
@Constraint(validatedBy = [DateDayValidator::class]) | ||
@MustBeDocumented | ||
annotation class ValidDateDay( | ||
val message: String = | ||
"The provided date is invalid. It must match yyyy-MM-dd", | ||
val groups: Array<KClass<*>> = [], | ||
val payload: Array<KClass<out Payload>> = [] | ||
) | ||
|
||
class DateDayValidator : ConstraintValidator<ValidDateDay, String> { | ||
override fun isValid( | ||
value: String, | ||
constraintValidatorContext: ConstraintValidatorContext | ||
): Boolean { | ||
return try { | ||
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") | ||
LocalDate.parse(value, formatter) | ||
true | ||
} catch (e: DateTimeParseException) { | ||
false | ||
} | ||
} | ||
} |