Skip to content

Commit

Permalink
fixing the way missing parameter error goes
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanfallet committed Feb 22, 2025
1 parent 0cd44be commit b1f93b7
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ open class APIChildModelRouter<Model : IChildModel<Id, CreatePayload, UpdatePayl
call.respond(mapOf("error" to exception.key))
}

is MissingParameterException -> handleExceptionAPI(
ControllerException(
if (exception.type.isBadRequest) HttpStatusCode.BadRequest else HttpStatusCode.NotFound,
"error_parameter_${exception.key}"
), call
)

is PropertyValidatorException -> handleExceptionAPI(
ControllerException(HttpStatusCode.BadRequest, "${route}_${exception.key}_${exception.reason}"), call
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package dev.kaccelero.routers

import dev.kaccelero.annotations.ModelAnnotations
import dev.kaccelero.annotations.PathParameter
import dev.kaccelero.annotations.Payload
import dev.kaccelero.annotations.QueryParameter
import dev.kaccelero.commons.exceptions.ControllerException
import dev.kaccelero.annotations.*
import dev.kaccelero.controllers.IChildModelController
import dev.kaccelero.models.IChildModel
import io.ktor.http.*
Expand Down Expand Up @@ -116,9 +112,8 @@ abstract class AbstractChildModelRouter<Model : IChildModel<Id, CreatePayload, U
modelTypeInfo.type as KClass<Model>,
call.parameters[id]!!
).let {
if (it == null && !parameter.type.isMarkedNullable) throw ControllerException(
HttpStatusCode.NotFound,
"Missing parameter: ${parameter.name}"
if (it == null && !parameter.type.isMarkedNullable) throw MissingParameterException(
ParameterType.ID, parameter.name
)
it
}
Expand All @@ -128,9 +123,8 @@ abstract class AbstractChildModelRouter<Model : IChildModel<Id, CreatePayload, U
parameter.type,
parameter.name?.let { call.parameters[it] }
).let {
if (it == null && !parameter.type.isMarkedNullable) throw ControllerException(
HttpStatusCode.NotFound,
"Missing parameter: ${parameter.name}"
if (it == null && !parameter.type.isMarkedNullable) throw MissingParameterException(
ParameterType.PATH, parameter.name
)
it
}
Expand All @@ -140,9 +134,8 @@ abstract class AbstractChildModelRouter<Model : IChildModel<Id, CreatePayload, U
parameter.type,
parameter.name?.let { call.request.queryParameters[it] }
).let {
if (it == null && !parameter.type.isMarkedNullable) throw ControllerException(
HttpStatusCode.NotFound,
"Missing parameter: ${parameter.name}"
if (it == null && !parameter.type.isMarkedNullable) throw MissingParameterException(
ParameterType.QUERY, parameter.name
)
it
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package dev.kaccelero.routers

import dev.kaccelero.annotations.ModelAnnotations
import dev.kaccelero.annotations.Payload
import dev.kaccelero.annotations.PropertyValidatorException
import dev.kaccelero.annotations.TemplateMapping
import dev.kaccelero.annotations.*
import dev.kaccelero.commons.exceptions.ControllerException
import dev.kaccelero.commons.responses.ControllerResponse
import dev.kaccelero.commons.responses.RedirectResponse
Expand Down Expand Up @@ -72,6 +69,13 @@ open class TemplateChildModelRouter<Model : IChildModel<Id, CreatePayload, Updat
)
}

is MissingParameterException -> handleExceptionTemplate(
ControllerException(
if (exception.type.isBadRequest) HttpStatusCode.BadRequest else HttpStatusCode.NotFound,
"error_parameter_${exception.key}"
), call, fromTemplate
)

is PropertyValidatorException -> handleExceptionTemplate(
ControllerException(
HttpStatusCode.BadRequest, "${route}_${exception.key}_${exception.reason}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class APIUnitRouterTest {
router.createRoutes(this)
}
val response = client.get("/api/hello/query")
assertEquals(HttpStatusCode.NotFound, response.status)
assertEquals(HttpStatusCode.BadRequest, response.status)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.kaccelero.annotations

data class MissingParameterException(
val type: ParameterType,
val key: String?,
) : Exception()
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.kaccelero.annotations

enum class ParameterType(
val isBadRequest: Boolean = false,
) {

ID, PATH, QUERY(true)

}

0 comments on commit b1f93b7

Please sign in to comment.