Skip to content

Commit 68e0996

Browse files
committed
Polish common exception structure
1 parent 65eb1db commit 68e0996

File tree

4 files changed

+13
-15
lines changed

4 files changed

+13
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package com.github.mduesterhoeft.router
22

3-
import java.lang.RuntimeException
4-
5-
class ApiException(
6-
val statusCode: Int = 400,
3+
open class ApiException(
74
message: String,
85
val code: String,
9-
val details: Map<String, Any> = emptyMap()
10-
) : RuntimeException(message) {
6+
val httpResponseStatus: Int,
7+
val details: Map<String, Any> = emptyMap(),
8+
cause: Throwable? = null
9+
) : RuntimeException("[$code] $message", cause) {
1110

1211
override fun toString(): String {
13-
return "ApiException(statusCode=$statusCode, code=$code, details=$details, message=$message)"
12+
return "ApiException(message='$message', code='$code', httpResponseStatus=$httpResponseStatus, details=$details, cause=$cause)"
1413
}
1514
}

src/main/kotlin/com/github/mduesterhoeft/router/RequestHandler.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ abstract class RequestHandler : RequestHandler<APIGatewayProxyRequestEvent, APIG
5555
if (matchResults.any { it.matchPath && it.matchMethod && !it.matchContentType }) {
5656
return createErrorResponse(
5757
input, ApiException(
58-
statusCode = 415,
58+
httpResponseStatus = 415,
5959
message = "Unsupported Media Type",
6060
code = "UNSUPPORTED_MEDIA_TYPE"
6161
)
@@ -64,7 +64,7 @@ abstract class RequestHandler : RequestHandler<APIGatewayProxyRequestEvent, APIG
6464
if (matchResults.any { it.matchPath && it.matchMethod && !it.matchAcceptType }) {
6565
return createErrorResponse(
6666
input, ApiException(
67-
statusCode = 406,
67+
httpResponseStatus = 406,
6868
message = "Not Acceptable",
6969
code = "NOT_ACCEPTABLE"
7070
)
@@ -73,15 +73,15 @@ abstract class RequestHandler : RequestHandler<APIGatewayProxyRequestEvent, APIG
7373
if (matchResults.any { it.matchPath && !it.matchMethod }) {
7474
return createErrorResponse(
7575
input, ApiException(
76-
statusCode = 405,
76+
httpResponseStatus = 405,
7777
message = "Method Not Allowed",
7878
code = "METHOD_NOT_ALLOWED"
7979
)
8080
)
8181
}
8282
return createErrorResponse(
8383
input, ApiException(
84-
statusCode = 404,
84+
httpResponseStatus = 404,
8585
message = "Not found",
8686
code = "NOT_FOUND"
8787
)
@@ -97,7 +97,7 @@ abstract class RequestHandler : RequestHandler<APIGatewayProxyRequestEvent, APIG
9797
"code" to ex.code,
9898
"details" to ex.details
9999
)))
100-
.withStatusCode(ex.statusCode)
100+
.withStatusCode(ex.httpResponseStatus)
101101
.withHeaders(mapOf("Content-Type" to "application/json"))
102102

103103
open fun <T> createResponse(input: APIGatewayProxyRequestEvent, response: ResponseEntity<T>): APIGatewayProxyResponseEvent {

src/main/kotlin/com/github/mduesterhoeft/router/Router.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class Router {
6464
}
6565
}
6666

67-
interface Filter : (HandlerFunction<*, *>) -> HandlerFunction<*,*> {
67+
interface Filter : (HandlerFunction<*, *>) -> HandlerFunction<*, *> {
6868
companion object {
6969
operator fun invoke(fn: (HandlerFunction<*, *>) -> HandlerFunction<*, *>): Filter = object : Filter {
7070
override operator fun invoke(next: HandlerFunction<*, *>): HandlerFunction<*, *> = fn(next)
@@ -85,7 +85,7 @@ data class RouterFunction<I, T>(
8585
val handler: HandlerFunction<I, T>
8686
)
8787

88-
data class Request<I>(val apiRequest: APIGatewayProxyRequestEvent, val body: I, val pathPattern: String) {
88+
data class Request<I>(val apiRequest: APIGatewayProxyRequestEvent, val body: I, val pathPattern: String = apiRequest.path) {
8989

9090
val pathParameters by lazy { UriTemplate.from(pathPattern).extract(apiRequest.path) }
9191
fun getPathParameter(name: String) = pathParameters[name]

src/test/kotlin/com/github/mduesterhoeft/router/UriTemplateTest.kt

-1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,4 @@ class UriTemplateTest {
3434
.isEqualTo(mapOf("first" to "first-value", "second" to "second-value"))
3535
then(UriTemplate.from("/some").extract("/some")).isEmpty()
3636
}
37-
3837
}

0 commit comments

Comments
 (0)