diff --git a/ktor-template-core/src/main/kotlin/mobi/waterdog/rest/template/exception/ErrorCode.kt b/ktor-template-core/src/main/kotlin/mobi/waterdog/rest/template/exception/ErrorCode.kt index c5ed06d..8f48601 100644 --- a/ktor-template-core/src/main/kotlin/mobi/waterdog/rest/template/exception/ErrorCode.kt +++ b/ktor-template-core/src/main/kotlin/mobi/waterdog/rest/template/exception/ErrorCode.kt @@ -2,13 +2,12 @@ package mobi.waterdog.rest.template.exception import io.ktor.http.HttpStatusCode -enum class ErrorCode(val httpStatusCode: HttpStatusCode, val messageCode: String) { - // client errors - InvalidUserInput(HttpStatusCode.BadRequest, "client_error.invalid_parameters"), - NotFound(HttpStatusCode.NotFound, "client_error.not_found"), +class ErrorCode(val httpStatusCode: HttpStatusCode, val messageCode: String) - // server errors - NotImplemented(HttpStatusCode.NotImplemented, "server_error.not_implemented") +object ErrorCodes { + val InvalidUserInput = ErrorCode(HttpStatusCode.BadRequest, "client_error.invalid_parameters") + val NotFound = ErrorCode(HttpStatusCode.NotFound, "client_error.not_found") - // TODO fill with all possible app errors + // server errors + val NotImplemented = ErrorCode(HttpStatusCode.NotImplemented, "server_error.not_implemented") } diff --git a/ktor-template-core/src/main/kotlin/mobi/waterdog/rest/template/validation/Validatable.kt b/ktor-template-core/src/main/kotlin/mobi/waterdog/rest/template/validation/Validatable.kt index 8f56b0f..a21496f 100644 --- a/ktor-template-core/src/main/kotlin/mobi/waterdog/rest/template/validation/Validatable.kt +++ b/ktor-template-core/src/main/kotlin/mobi/waterdog/rest/template/validation/Validatable.kt @@ -1,7 +1,7 @@ package mobi.waterdog.rest.template.validation import mobi.waterdog.rest.template.exception.AppException -import mobi.waterdog.rest.template.exception.ErrorCode +import mobi.waterdog.rest.template.exception.ErrorCodes import mobi.waterdog.rest.template.exception.ErrorDefinition import org.valiktor.Constraint import org.valiktor.ConstraintViolationException @@ -68,7 +68,7 @@ abstract class Validatable { private fun valiktorException2AppException(valiktorEx: ConstraintViolationException): AppException { val validationException = AppException( - code = ErrorCode.InvalidUserInput, + code = ErrorCodes.InvalidUserInput, title = "Invalid content for class ${this::class.qualifiedName}, check invalid fields in errors" ) diff --git a/ktor-template-example/src/main/kotlin/mobi/waterdog/rest/template/tests/core/persistance/sql/PersonRepositoryImpl.kt b/ktor-template-example/src/main/kotlin/mobi/waterdog/rest/template/tests/core/persistance/sql/PersonRepositoryImpl.kt index 49e5ab3..2fe0024 100644 --- a/ktor-template-example/src/main/kotlin/mobi/waterdog/rest/template/tests/core/persistance/sql/PersonRepositoryImpl.kt +++ b/ktor-template-example/src/main/kotlin/mobi/waterdog/rest/template/tests/core/persistance/sql/PersonRepositoryImpl.kt @@ -1,7 +1,7 @@ package mobi.waterdog.rest.template.tests.core.persistance.sql import mobi.waterdog.rest.template.exception.AppException -import mobi.waterdog.rest.template.exception.ErrorCode +import mobi.waterdog.rest.template.exception.ErrorCodes import mobi.waterdog.rest.template.pagination.PageRequest import mobi.waterdog.rest.template.pagination.SortField import mobi.waterdog.rest.template.tests.core.model.Person @@ -57,7 +57,7 @@ class PersonRepositoryImpl : PersonRepository { "name" -> Persons.name "birthday" -> Persons.birthday else -> throw AppException( - ErrorCode.NotImplemented, + ErrorCodes.NotImplemented, "Sort by column '${sort.field}' in Person table not " + "implemented." ) diff --git a/ktor-template-example/src/main/kotlin/mobi/waterdog/rest/template/tests/core/service/CarServiceImpl.kt b/ktor-template-example/src/main/kotlin/mobi/waterdog/rest/template/tests/core/service/CarServiceImpl.kt index a2e046b..3561321 100644 --- a/ktor-template-example/src/main/kotlin/mobi/waterdog/rest/template/tests/core/service/CarServiceImpl.kt +++ b/ktor-template-example/src/main/kotlin/mobi/waterdog/rest/template/tests/core/service/CarServiceImpl.kt @@ -2,7 +2,7 @@ package mobi.waterdog.rest.template.tests.core.service import mobi.waterdog.rest.template.database.DatabaseConnection import mobi.waterdog.rest.template.exception.AppException -import mobi.waterdog.rest.template.exception.ErrorCode +import mobi.waterdog.rest.template.exception.ErrorCodes import mobi.waterdog.rest.template.pagination.PageRequest import mobi.waterdog.rest.template.tests.core.model.Car import mobi.waterdog.rest.template.tests.core.model.CarSaveCommand @@ -43,7 +43,7 @@ class CarServiceImpl( override suspend fun updateCar(car: Car): Car { return dbc.suspendedQuery { if (!exists(car.id)) { - throw AppException(ErrorCode.NotFound, "Could not find car with id '${car.id}'.") + throw AppException(ErrorCodes.NotFound, "Could not find car with id '${car.id}'.") } carRepository.update(car) }