How do I make a custom CoroutineExceptionHandler t...
# coroutines
m
How do I make a custom CoroutineExceptionHandler that re-throws the exception that caused a job to cancel? e.g.
Copy code
coroutineScope {
    launch {
        throw NotImplementedError()  // Currently somehow deadlocks
    }

    launch {
        // ...
    }
}  
// Ideally coroutineScope exits early and also (re-)throw the NotImplementedError
s
In principle this should already work the way you described
If it's not, something else is going on
m
Is there any way to debug? I tried running it in the IntelliJ debugger but that doesn't help much; Most if not all of the RUNNING threads are in
wait
I added this catch clause, which does log the error, but after rethrowing it, there doesn't seem to be anything that happens.
Copy code
coroutineScope {
            for (field in operation.fields) {
                launch {
                    wrapper {
                        try {
                            map[field.alias ?: field.name] = execute(operation, defMap[field.name] ?: throw GraphQLException("Cannot query field \"${field.name}\" on type \"${operation.type}\".", field.loc), field)
                        } catch (e: GraphQLException) {
                            errors += e.errors.map { it.withParent(field.alias ?: field.name) }
                        } catch (e: Exception) {  // Added catch clause
                            e.printStackTrace()
                            throw e
                        }
                    }
                }
            }
        }
I figured out the issue, turns out there's some strange behavior when catching exceptions in Ktor monitoring. Will investigate more over there.
👍 1
d
if you're using
supervisorScope
it swallows everything except
CancellationException
I think; so, you need to wrap your error in a
throw CancellationException(e)
. I'm not clear on the scoping of
CoroutineExceptionHandler
, I know it can't "ignore" the error to allow progression bc the stack's already unwound.