Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
dc36d072
Commit
dc36d072
authored
Feb 07, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix exception handling
parent
d2b6f119
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
6 deletions
+17
-6
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/internal/InternalEventListeners.kt
.../net.mamoe.mirai/event/internal/InternalEventListeners.kt
+17
-6
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/internal/InternalEventListeners.kt
View file @
dc36d072
package
net.mamoe.mirai.event.internal
package
net.mamoe.mirai.event.internal
import
kotlinx.coroutines.CompletableJob
import
kotlinx.coroutines.*
import
kotlinx.coroutines.CoroutineScope
import
kotlinx.coroutines.Job
import
kotlinx.coroutines.withContext
import
net.mamoe.mirai.event.Listener
import
net.mamoe.mirai.event.Listener
import
net.mamoe.mirai.event.ListeningStatus
import
net.mamoe.mirai.event.ListeningStatus
import
net.mamoe.mirai.event.Subscribable
import
net.mamoe.mirai.event.Subscribable
import
net.mamoe.mirai.utils.LockFreeLinkedList
import
net.mamoe.mirai.utils.LockFreeLinkedList
import
net.mamoe.mirai.utils.MiraiDebugAPI
import
net.mamoe.mirai.utils.MiraiLogger
import
net.mamoe.mirai.utils.io.logStacktrace
import
net.mamoe.mirai.utils.io.logStacktrace
import
kotlin.coroutines.CoroutineContext
import
kotlin.coroutines.CoroutineContext
import
kotlin.coroutines.coroutineContext
import
kotlin.jvm.JvmField
import
kotlin.jvm.JvmField
import
kotlin.reflect.KClass
import
kotlin.reflect.KClass
...
@@ -31,6 +31,7 @@ internal fun <E : Subscribable> CoroutineScope.Handler(handler: suspend (E) -> L
...
@@ -31,6 +31,7 @@ internal fun <E : Subscribable> CoroutineScope.Handler(handler: suspend (E) -> L
return
Handler
(
coroutineContext
[
Job
],
coroutineContext
,
handler
)
return
Handler
(
coroutineContext
[
Job
],
coroutineContext
,
handler
)
}
}
private
inline
fun
inline
(
block
:
()
->
Unit
)
=
block
()
/**
/**
* 事件处理器.
* 事件处理器.
*/
*/
...
@@ -39,6 +40,7 @@ internal class Handler<in E : Subscribable>
...
@@ -39,6 +40,7 @@ internal class Handler<in E : Subscribable>
@PublishedApi
internal
constructor
(
parentJob
:
Job
?,
private
val
subscriberContext
:
CoroutineContext
,
@JvmField
val
handler
:
suspend
(
E
)
->
ListeningStatus
)
:
@PublishedApi
internal
constructor
(
parentJob
:
Job
?,
private
val
subscriberContext
:
CoroutineContext
,
@JvmField
val
handler
:
suspend
(
E
)
->
ListeningStatus
)
:
Listener
<
E
>,
CompletableJob
by
Job
(
parentJob
)
{
Listener
<
E
>,
CompletableJob
by
Job
(
parentJob
)
{
@UseExperimental
(
MiraiDebugAPI
::
class
)
override
suspend
fun
onEvent
(
event
:
E
):
ListeningStatus
{
override
suspend
fun
onEvent
(
event
:
E
):
ListeningStatus
{
if
(
isCompleted
||
isCancelled
)
return
ListeningStatus
.
STOPPED
if
(
isCompleted
||
isCancelled
)
return
ListeningStatus
.
STOPPED
if
(!
isActive
)
return
ListeningStatus
.
LISTENING
if
(!
isActive
)
return
ListeningStatus
.
LISTENING
...
@@ -46,8 +48,17 @@ internal class Handler<in E : Subscribable>
...
@@ -46,8 +48,17 @@ internal class Handler<in E : Subscribable>
// Inherit context.
// Inherit context.
withContext
(
subscriberContext
)
{
handler
.
invoke
(
event
)
}.
also
{
if
(
it
==
ListeningStatus
.
STOPPED
)
this
.
complete
()
}
withContext
(
subscriberContext
)
{
handler
.
invoke
(
event
)
}.
also
{
if
(
it
==
ListeningStatus
.
STOPPED
)
this
.
complete
()
}
}
catch
(
e
:
Throwable
)
{
}
catch
(
e
:
Throwable
)
{
e
.
logStacktrace
()
subscriberContext
[
CoroutineExceptionHandler
]
?.
handleException
(
subscriberContext
,
e
)
// this.complete() // do not `completeExceptionally`, otherwise parentJob will fail.
?:
coroutineContext
[
CoroutineExceptionHandler
]
?.
handleException
(
subscriberContext
,
e
)
?:
inline
{
@Suppress
(
"DEPRECATION"
)
MiraiLogger
.
warning
(
"""Event processing: An exception occurred but no CoroutineExceptionHandler found,
either in coroutineContext from Handler job, or in subscriberContext"""
.
trimIndent
()
)
e
.
logStacktrace
(
"Event processing(No CoroutineExceptionHandler found)"
)
}
// this.complete() // do not `completeExceptionally`, otherwise parentJob will fai`l.
// ListeningStatus.STOPPED
// ListeningStatus.STOPPED
// not stopping listening.
// not stopping listening.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment