Skip to content

Commit

Permalink
Simplify error code handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Mygod committed Jul 24, 2024
1 parent f610127 commit 45e6cd3
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,14 @@ object MacLookup {
var response: String? = null
try {
response = connectCancellable("https://macaddress.io/macaddress/$mac") { conn ->
when (val responseCode = conn.responseCode) {
200 -> conn.inputStream.use {
Scanner(it).run {
findWithinHorizon(dataPattern, 0)
?: throw UnexpectedError(mac, findWithinHorizon(garbagePattern, 0))
}
val responseCode = conn.responseCode
if (responseCode == 200) conn.inputStream.use {
Scanner(it).run {
findWithinHorizon(dataPattern, 0)
?: throw UnexpectedError(mac, findWithinHorizon(garbagePattern, 0))
}
400, 401, 402, 404, 422, 429, 500 -> throw UnexpectedError(mac,
} else throw UnexpectedError(mac, "[$responseCode] " +
conn.findErrorStream.bufferedReader().readText())
else -> throw UnexpectedError(mac, "Unhandled response code $responseCode: " +
conn.findErrorStream.bufferedReader().readText())
}
}
val obj = JSONObject(Html.fromHtml(response, 0).toString())
val result = if (obj.getJSONObject("blockDetails").getBoolean("blockFound")) {
Expand Down

0 comments on commit 45e6cd3

Please sign in to comment.