Skip to content

Commit 69508d3

Browse files
authored
fix(#4): detail exceptions handling
1 parent 77f89b4 commit 69508d3

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
}
88

99
group = "com.github.samarium150"
10-
version = "1.4"
10+
version = "1.4.1"
1111

1212
repositories {
1313
mavenLocal()

src/main/kotlin/APIError.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.github.samarium150.mirai.plugin
2+
3+
/**
4+
* Class representation for handling Lolicon API error
5+
*
6+
* @property code [Int] Error code
7+
* @property message [String] Error message
8+
* @constructor
9+
*/
10+
class APIError internal constructor(
11+
private val code: Int,
12+
override val message: String
13+
): Exception(message) {
14+
15+
/**
16+
* Override toString() for logging
17+
*
18+
* @return [String]
19+
*/
20+
override fun toString(): String {
21+
return "[${code}: ${message}]"
22+
}
23+
24+
/**
25+
* Return the readable information
26+
*
27+
* @return [String]
28+
*/
29+
fun toReadable(): String {
30+
return when (code) {
31+
-1, 401 -> message
32+
403 -> "调用不规范, 用户两行泪. 快去找开发者的麻烦."
33+
404 -> "没有找到相关图片, 换个关键词试试吧"
34+
else -> throw Exception(this.toString())
35+
}
36+
}
37+
}

src/main/kotlin/Lolicon.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.samarium150.mirai.plugin
22

3+
import com.github.kittinunf.fuel.core.FuelError
34
import kotlinx.coroutines.*
45
import kotlinx.io.errors.IOException
56
import net.mamoe.mirai.Mirai
@@ -125,6 +126,12 @@ object Lolicon: CompositeCommand(
125126
@Suppress("BlockingMethodInNonBlockingContext")
126127
withContext(Dispatchers.IO) { try { stream.close() } catch (e: IOException) {} }
127128
}
129+
} catch (fe: FuelError) {
130+
Main.logger.warning(fe.toString())
131+
sendMessage("网络连接失败或图片已被删除,之后再试试吧")
132+
} catch (ae: APIError) {
133+
Main.logger.warning(ae.toString())
134+
sendMessage(ae.toReadable())
128135
} catch (e: Exception) {
129136
Main.logger.error(e)
130137
}

src/main/kotlin/Main.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import net.mamoe.mirai.utils.info
2626
object Main: KotlinPlugin(
2727
JvmPluginDescription(
2828
id = "com.github.samarium150.mirai-console-lolicon",
29-
version = "1.4",
29+
version = "1.4.1",
3030
name = "mirai-console-lolicon"
3131
)
3232
) {

src/main/kotlin/RequestHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ object RequestHandler {
3434
val (_, response, result) = getResponse(url)
3535
if (result is Result.Failure) throw result.getException()
3636
val feedback: Response = gson.fromJson(String(response.data), Response::class.java)
37-
if (feedback.code != 0) throw Exception(feedback.msg)
37+
if (feedback.code != 0) throw APIError(feedback.code, feedback.msg)
3838
return feedback
3939
}
4040

0 commit comments

Comments
 (0)