mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-01 03:50:18 +08:00
Handle NoSuchBot Exception
This commit is contained in:
parent
66a015af52
commit
14a2df03d6
@ -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
|
||||
}
|
@ -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不存在")
|
||||
|
||||
/**
|
||||
* 错误参数
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user