1 /* 2 * Copyright 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.compose.runtime.tooling 18 19 import androidx.compose.runtime.ComposerImpl 20 import androidx.compose.runtime.CompositionLocal 21 import androidx.compose.runtime.changelist.OperationErrorContext 22 import androidx.compose.runtime.staticCompositionLocalOf 23 import kotlin.coroutines.CoroutineContext 24 25 /** 26 * Used to attach a compose stack trace to a throwable based on a location of compose node in 27 * composition. This context is expected to be used by custom node implementations to attach 28 * diagnostic compose stack traces to exceptions in passes that are not handled by the Compose 29 * runtime (example: measure / layout / draw in Compose UI). 30 * 31 * Compose runtime automatically appends information about exceptions that happen in composition and 32 * effects. 33 */ 34 val LocalCompositionErrorContext: CompositionLocal<CompositionErrorContext?> = <lambda>null35 staticCompositionLocalOf { 36 null 37 } 38 39 /** 40 * Provides a way to attach a compose stack trace to a throwable based on a location of compose node 41 * in composition. This context is expected to be used by custom node implementations to attach 42 * diagnostic compose stack traces to exceptions in passes that are not handled by the Compose 43 * runtime (example: measure / layout / draw in Compose UI). 44 * 45 * Compose runtime automatically appends information about exceptions that happen in composition and 46 * effects. 47 */ 48 sealed interface CompositionErrorContext { 49 /** 50 * Attaches a Compose stack trace to a throwable as a suppressed [DiagnosticComposeException]. 51 * Has no effect if: 52 * - Throwable already contains a suppressed [DiagnosticComposeException] 53 * - [composeNode] is not found in composition 54 * - composition contains no source information (e.g. in minified builds) 55 * 56 * @param composeNode closest node to where exception was originally thrown 57 * @return true if the exception was attached, false otherwise 58 * @receiver throwable to attach a compose stack trace to 59 */ Throwablenull60 fun Throwable.attachComposeStackTrace(composeNode: Any): Boolean 61 } 62 63 internal class CompositionErrorContextImpl(private val composer: ComposerImpl) : 64 CompositionErrorContext, OperationErrorContext, CoroutineContext.Element { 65 override fun Throwable.attachComposeStackTrace(composeNode: Any): Boolean = 66 tryAttachComposeStackTrace { 67 composer.stackTraceForValue(composeNode) 68 } 69 70 override fun buildStackTrace(currentOffset: Int?): List<ComposeStackTraceFrame> = 71 composer.parentStackTrace() 72 73 companion object Key : CoroutineContext.Key<CompositionErrorContextImpl> { 74 override fun toString(): String = "CompositionErrorContext" 75 } 76 77 override val key: CoroutineContext.Key<*> 78 get() = Key 79 } 80