Skip to content

Commit

Permalink
エラーページ実装
Browse files Browse the repository at this point in the history
  • Loading branch information
nexryai committed May 24, 2024
1 parent ad94c92 commit 83ab2be
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/main/kotlin/me/nexryai/transit/plugins/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import io.ktor.server.routing.*
import me.nexryai.transit.entities.TransitParams
import me.nexryai.transit.services.TransitInfoService
import me.nexryai.transit.templates.ResultPageTemplate
import me.nexryai.transit.templates.RouteNotFoundPageTemplate
import me.nexryai.transit.templates.ServerErrorPageTemplate
import me.nexryai.transit.templates.WelcomePageTemplate

fun Application.configureRouting() {
Expand Down Expand Up @@ -63,10 +65,10 @@ fun Application.configureRouting() {
val res = try {
transitInfoService.getTransit()
} catch (e: IllegalArgumentException) {
call.respondText(text = "Invalid params or route not found", status = HttpStatusCode.BadRequest)
call.respondHtmlTemplate(RouteNotFoundPageTemplate()) {}
return@get
} catch (e: Exception) {
call.respondText(text = "500: Internal server error", status = HttpStatusCode.InternalServerError)
call.respondHtmlTemplate(ServerErrorPageTemplate(e.toString().split(":")[0])) {}
return@get
}

Expand Down
65 changes: 65 additions & 0 deletions src/main/kotlin/me/nexryai/transit/templates/ErrorPageTemplate.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package me.nexryai.transit.templates

import io.ktor.server.html.*
import kotlinx.html.*

class RouteNotFoundPageTemplate(): Template<HTML> {
private val head = TemplatePlaceholder<HeadTemplate>()
override fun HTML.apply() {
insert(HeadTemplate(), head)
body {
div {
id = "app"
div {
id = "error-page"
h2(classes = "error-page-title") {
i(classes = "ti ti-mood-puzzled")
+" Route not found"
}
p {
+"経路が見つかりませんでした。"
}
ul {
li {
+"出発地または到着地が正しいか確認してください。"
}
li {
+"出発地と到着地が離れすぎている可能性があります。"
}
li {
+"一時的なシステムエラーによりこの表示が出る場合もあります。"
}
}
}
}
}
}
}

class ServerErrorPageTemplate(private val message: String): Template<HTML> {
private val head = TemplatePlaceholder<HeadTemplate>()
override fun HTML.apply() {
insert(HeadTemplate(), head)
body {
div {
id = "app"
div {
id = "error-page"
h2(classes = "error-page-title") {
i(classes = "ti ti-zoom-exclamation")
+" Something went wrong"
}
p {
+"An unrecoverable error occurred. Unexpected exception was thrown."
}
p {
+"Technical details: "
span(classes = "error-page-detail") {
+message
}
}
}
}
}
}
}
15 changes: 15 additions & 0 deletions src/main/kotlin/me/nexryai/transit/templates/Styles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ var styles = CssBuilder().apply {
fontStyle = FontStyle.normal
}


rule("#error-page") {
marginTop = 100.px
}

rule(".error-page-title") {
fontFamily = "'Ubuntu', sans-serif"
fontWeight = FontWeight.w300
}

rule(".error-page-detail") {
fontFamily = "monospace"
color = Color("#666")
}

rule(".form") {
display = Display.flex
flexDirection = FlexDirection.column
Expand Down

0 comments on commit 83ab2be

Please sign in to comment.