Skip to content

Commit ea0f21d

Browse files
committedApr 3, 2025
[localserver] improve errors handling
1 parent ae51153 commit ea0f21d

File tree

1 file changed

+12
-2
lines changed
  • app/src/main/java/me/capcom/smsgateway/modules/localserver

1 file changed

+12
-2
lines changed
 

‎app/src/main/java/me/capcom/smsgateway/modules/localserver/WebService.kt

+12-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import io.ktor.server.auth.authenticate
2121
import io.ktor.server.auth.basic
2222
import io.ktor.server.engine.embeddedServer
2323
import io.ktor.server.netty.Netty
24+
import io.ktor.server.plugins.BadRequestException
25+
import io.ktor.server.plugins.NotFoundException
2426
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
2527
import io.ktor.server.plugins.statuspages.StatusPages
2628
import io.ktor.server.response.header
@@ -96,9 +98,11 @@ class WebService : Service() {
9698
call.respond(
9799
when (cause) {
98100
is IllegalArgumentException -> HttpStatusCode.BadRequest
101+
is BadRequestException -> HttpStatusCode.BadRequest
102+
is NotFoundException -> HttpStatusCode.NotFound
99103
else -> HttpStatusCode.InternalServerError
100104
},
101-
mapOf("message" to cause.message)
105+
mapOf("message" to cause.description)
102106
)
103107
}
104108
}
@@ -236,4 +240,10 @@ class WebService : Service() {
236240
context.stopService(Intent(context, WebService::class.java))
237241
}
238242
}
239-
}
243+
}
244+
245+
private val Throwable.description: String
246+
get() {
247+
return (localizedMessage ?: message ?: toString()) +
248+
(cause?.let { ": " + it.description } ?: "")
249+
}

0 commit comments

Comments
 (0)
Please sign in to comment.