Handle NoSuchBot Exception

This commit is contained in:
ryoii 2020-02-05 19:46:43 +08:00
parent 66a015af52
commit 14a2df03d6
2 changed files with 27 additions and 12 deletions

View File

@ -21,22 +21,30 @@ fun Application.authModule() {
}
miraiVerify<BindDTO>("/verify", verifiedSessionKey = false) {
try {
val bot = Bot.instanceWhose(it.qq)
with(SessionManager) {
closeSession(it.sessionKey)
allSession[it.sessionKey] = AuthedSession(bot, EmptyCoroutineContext)
}
call.respondStateCode(StateCode.Success)
} catch (e: NoSuchElementException) {
call.respondStateCode(StateCode.NoBot)
val bot = getBotOrThrow(it.qq)
with(SessionManager) {
closeSession(it.sessionKey)
allSession[it.sessionKey] = AuthedSession(bot, EmptyCoroutineContext)
}
call.respondStateCode(StateCode.Success)
}
miraiVerify<BindDTO>("/release") {
SessionManager.closeSession(it.sessionKey)
call.respondStateCode(StateCode.Success)
val bot = getBotOrThrow(it.qq)
val session = SessionManager[it.sessionKey] as AuthedSession
if (bot.uin == session.bot.uin) {
SessionManager.closeSession(it.sessionKey)
call.respondStateCode(StateCode.Success)
} else {
throw NoSuchElementException()
}
}
}
}
private fun getBotOrThrow(qq: Long) = try {
Bot.instanceWhose(qq)
} catch (e: NoSuchElementException) {
throw NoSuchBotException
}

View File

@ -108,7 +108,9 @@ internal inline fun Route.intercept(crossinline blk: suspend PipelineContext<Uni
call.respondStateCode(StateCode.IllegalSession)
} catch (e: NotVerifiedSessionException) {
call.respondStateCode(StateCode.NotVerifySession)
} catch (e: NoSuchElementException) {
} catch (e: NoSuchBotException) {
call.respondStateCode(StateCode.NoBot)
}catch (e: NoSuchElementException) {
call.respondStateCode(StateCode.NoElement)
} catch (e: IllegalAccessException) {
call.respondStateCode(StateCode(400, e.message), HttpStatusCode.BadRequest)
@ -189,6 +191,11 @@ object IllegalSessionException : IllegalAccessException("Session失效或不存
*/
object NotVerifiedSessionException : IllegalAccessException("Session未激活")
/**
* 指定Bot不存在
*/
object NoSuchBotException: IllegalAccessException("指定Bot不存在")
/**
* 错误参数
*/