Implements Hunt basic create and get, and adds field validation to controllers

This commit is contained in:
2025-12-22 08:44:14 -06:00
parent 04b61485ea
commit 5905882763
16 changed files with 230 additions and 5 deletions

View File

@@ -2,12 +2,17 @@ package net.halfbinary.scavengerhuntapi.error
import net.halfbinary.scavengerhuntapi.error.exception.InvalidEmailException
import net.halfbinary.scavengerhuntapi.error.exception.LoginFailedException
import net.halfbinary.scavengerhuntapi.error.exception.NotFoundException
import net.halfbinary.scavengerhuntapi.error.exception.PreexistingAccountException
import org.springframework.http.HttpStatus
import org.springframework.http.converter.HttpMessageNotReadableException
import org.springframework.validation.FieldError
import org.springframework.web.bind.MethodArgumentNotValidException
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestControllerAdvice
@RestControllerAdvice
class ExceptionHandler {
@@ -28,4 +33,27 @@ class ExceptionHandler {
fun invalidEmailException(e: InvalidEmailException): String? {
return e.message
}
@ExceptionHandler(NotFoundException::class)
@ResponseStatus(HttpStatus.NOT_FOUND)
fun notFoundException(e: NotFoundException): String? {
return e.message
}
@ExceptionHandler(HttpMessageNotReadableException::class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
fun httpMessageNotReadableException(e: HttpMessageNotReadableException): String? {
return e.message
}
@ExceptionHandler(MethodArgumentNotValidException::class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
fun handleValidationExceptions(e: MethodArgumentNotValidException): Map<String, String?> {
return e.bindingResult.allErrors.associate { error ->
Pair(
(error as FieldError).field,
error.defaultMessage
)
}
}
}