Skip to content

Commit 35500c1

Browse files
committed
feat(model-server): report errors as application/problem+json
This commit modifies the v2 REST API to always report errors as application/problem+json instances following RFC 9475. We do not declare this as a breaking change as we never guaranteed a certain error format. The OpenAPI specification has been extended with a default error response for all routes to avoid having to repeat the definition many times. The installation of the StatusPages extension had to be moved to the model-server Main file as this extension is now used to map instances of the new HttpException class to suitable responses. HttpException has been introduced due to the lack of a standard HTTP exception hierarchy in ktor. Throwing exceptions instead of call.respondXXX usually makes route implementations shorter and easier to read.
1 parent 9c5f1f0 commit 35500c1

File tree

8 files changed

+392
-119
lines changed

8 files changed

+392
-119
lines changed

authorization/src/main/kotlin/org/modelix/authorization/KtorAuthUtils.kt

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import io.ktor.server.auth.jwt.jwt
3434
import io.ktor.server.auth.parseAuthorizationHeader
3535
import io.ktor.server.auth.principal
3636
import io.ktor.server.plugins.forwardedheaders.XForwardedHeaders
37-
import io.ktor.server.plugins.statuspages.StatusPages
3837
import io.ktor.server.request.header
3938
import io.ktor.server.response.respond
4039
import io.ktor.server.response.respondText
@@ -85,28 +84,6 @@ fun Application.installAuthentication(unitTestMode: Boolean = false) {
8584
}
8685
}
8786
}
88-
install(StatusPages) {
89-
exception<Throwable> { call, cause ->
90-
when (cause) {
91-
is NoPermissionException -> call.respondText(
92-
text = cause.message ?: "",
93-
status = HttpStatusCode.Forbidden,
94-
)
95-
is NotLoggedInException -> call.respondText(
96-
text = cause.message ?: "",
97-
status = HttpStatusCode.Unauthorized,
98-
)
99-
else -> {
100-
val text = """
101-
|500: $cause
102-
|
103-
|${cause.stackTraceToString()}
104-
""".trimMargin()
105-
call.respondText(text = text, status = HttpStatusCode.InternalServerError)
106-
}
107-
}
108-
}
109-
}
11087
routing {
11188
authenticate(jwtAuth) {
11289
get("/user") {

0 commit comments

Comments
 (0)