Commit 14a2df03 authored by ryoii's avatar ryoii

Handle NoSuchBot Exception

parent 66a015af
...@@ -21,22 +21,30 @@ fun Application.authModule() { ...@@ -21,22 +21,30 @@ fun Application.authModule() {
} }
miraiVerify<BindDTO>("/verify", verifiedSessionKey = false) { miraiVerify<BindDTO>("/verify", verifiedSessionKey = false) {
try { val bot = getBotOrThrow(it.qq)
val bot = Bot.instanceWhose(it.qq)
with(SessionManager) { with(SessionManager) {
closeSession(it.sessionKey) closeSession(it.sessionKey)
allSession[it.sessionKey] = AuthedSession(bot, EmptyCoroutineContext) allSession[it.sessionKey] = AuthedSession(bot, EmptyCoroutineContext)
} }
call.respondStateCode(StateCode.Success) call.respondStateCode(StateCode.Success)
} catch (e: NoSuchElementException) {
call.respondStateCode(StateCode.NoBot)
}
} }
miraiVerify<BindDTO>("/release") { miraiVerify<BindDTO>("/release") {
val bot = getBotOrThrow(it.qq)
val session = SessionManager[it.sessionKey] as AuthedSession
if (bot.uin == session.bot.uin) {
SessionManager.closeSession(it.sessionKey) SessionManager.closeSession(it.sessionKey)
call.respondStateCode(StateCode.Success) call.respondStateCode(StateCode.Success)
} else {
throw NoSuchElementException()
}
} }
} }
} }
private fun getBotOrThrow(qq: Long) = try {
Bot.instanceWhose(qq)
} catch (e: NoSuchElementException) {
throw NoSuchBotException
}
\ No newline at end of file
...@@ -108,7 +108,9 @@ internal inline fun Route.intercept(crossinline blk: suspend PipelineContext<Uni ...@@ -108,7 +108,9 @@ internal inline fun Route.intercept(crossinline blk: suspend PipelineContext<Uni
call.respondStateCode(StateCode.IllegalSession) call.respondStateCode(StateCode.IllegalSession)
} catch (e: NotVerifiedSessionException) { } catch (e: NotVerifiedSessionException) {
call.respondStateCode(StateCode.NotVerifySession) call.respondStateCode(StateCode.NotVerifySession)
} catch (e: NoSuchElementException) { } catch (e: NoSuchBotException) {
call.respondStateCode(StateCode.NoBot)
}catch (e: NoSuchElementException) {
call.respondStateCode(StateCode.NoElement) call.respondStateCode(StateCode.NoElement)
} catch (e: IllegalAccessException) { } catch (e: IllegalAccessException) {
call.respondStateCode(StateCode(400, e.message), HttpStatusCode.BadRequest) call.respondStateCode(StateCode(400, e.message), HttpStatusCode.BadRequest)
...@@ -189,6 +191,11 @@ object IllegalSessionException : IllegalAccessException("Session失效或不存 ...@@ -189,6 +191,11 @@ object IllegalSessionException : IllegalAccessException("Session失效或不存
*/ */
object NotVerifiedSessionException : IllegalAccessException("Session未激活") object NotVerifiedSessionException : IllegalAccessException("Session未激活")
/**
* 指定Bot不存在
*/
object NoSuchBotException: IllegalAccessException("指定Bot不存在")
/** /**
* 错误参数 * 错误参数
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment