Skip to content

Commit 4aeeada

Browse files
committed
Error handling
1 parent 892011c commit 4aeeada

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

kotlin-ktor/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ dependencies {
2020
implementation("io.ktor", "ktor-server-core", ktorVersion)
2121
implementation("io.ktor", "ktor-server-netty", ktorVersion)
2222
implementation("io.ktor", "ktor-server-websockets", ktorVersion)
23+
implementation("io.ktor","ktor-server-status-pages", ktorVersion)
2324

2425
implementation("io.ktor", "ktor-server-thymeleaf-jvm", ktorVersion)
2526
implementation("ch.qos.logback","logback-classic", logbackVersion)

kotlin-ktor/src/main/java/com/baeldung/thymeleaf/server/ThymeleafKtorApplication.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.baeldung.thymeleaf.server
22

33
import com.baeldung.thymeleaf.server.plugins.configureRouting
4+
import com.baeldung.thymeleaf.server.plugins.configureStatusPages
45
import com.baeldung.thymeleaf.server.plugins.configureTemplating
56
import io.ktor.server.engine.*
67
import io.ktor.server.netty.*
@@ -9,5 +10,6 @@ fun main() {
910
embeddedServer(Netty, port = 8080, host = "0.0.0.0") {
1011
configureTemplating()
1112
configureRouting()
13+
configureStatusPages()
1214
}.start(wait = true)
1315
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.thymeleaf.server.plugins
2+
3+
import io.ktor.http.*
4+
import io.ktor.server.application.*
5+
import io.ktor.server.plugins.statuspages.*
6+
import io.ktor.server.response.*
7+
import io.ktor.server.thymeleaf.*
8+
9+
fun Application.configureStatusPages() {
10+
install(StatusPages) {
11+
status(HttpStatusCode.NotFound) { call, _ ->
12+
call.respond(ThymeleafContent("error404", mapOf("message" to "Sorry! Page was not found.")))
13+
}
14+
}
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html >
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Error</title>
6+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
7+
rel="stylesheet"
8+
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
9+
crossorigin="anonymous">
10+
</head>
11+
<body>
12+
<div class="container">
13+
<h2>Error</h2>
14+
<p th:text="${message}" />
15+
</div>
16+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
17+
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
18+
crossorigin="anonymous"/>
19+
</body>
20+
</html>

kotlin-ktor/src/test/java/com/baeldung/thymeleaf/server/ThymeleafServerIntegrationTest.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.baeldung.thymeleaf.server
22

3-
import com.baeldung.graphql.client.GraphQLClient
43
import com.baeldung.thymeleaf.server.plugins.configureRouting
4+
import com.baeldung.thymeleaf.server.plugins.configureStatusPages
55
import com.baeldung.thymeleaf.server.plugins.configureTemplating
66
import io.ktor.server.engine.*
77
import io.ktor.server.netty.*
@@ -27,6 +27,7 @@ class ThymeleafServerIntegrationTest {
2727
module {
2828
configureRouting()
2929
configureTemplating()
30+
configureStatusPages()
3031
}
3132
connector {
3233
host = "0.0.0.0"
@@ -76,4 +77,15 @@ class ThymeleafServerIntegrationTest {
7677
driver.close()
7778
}
7879

80+
@Test
81+
fun `when get an invalid route then should return a default error page`() {
82+
val options = ChromeOptions()
83+
options.addArguments("--headless=new");
84+
val driver = ChromeDriver(options)
85+
driver.get("http://0.0.0.0:8080/other-page")
86+
val header2 = driver.findElements(By.tagName("h2"))
87+
assertEquals("Error", header2.first().text)
88+
driver.close()
89+
}
90+
7991
}

0 commit comments

Comments
 (0)