मुख्य सामग्री पर जाएं
स्निपेट·मुफ़्त

runCatching → Result<T, AppError>

Tiny extension that lifts thrown exceptions into a typed Result<T, AppError>, so callers can pattern-match instead of try/catch.

मुफ़्त में इंस्टॉल करें।

अगर आपके Android डिवाइस पर PocketCode इंस्टॉल है, तो यह पेज ऐप में खुलेगा।

प्रीव्यू

Result.kt · 71 लाइनें
/**
 * Tiny error-handling helpers. Turns thrown exceptions into a typed
 * Result<T, AppError> so callers can branch on the failure shape
 * rather than wrap every call in try/catch.
 *
 * Usage:
 *
 *     val user: Result<User, AppError> = catching {
 *         repo.fetchUser(id)
 *     } onError {
 *         when (it) {
 *             is IOException        -> AppError.Network(it)
 *             is HttpException      -> AppError.Server(it.code(), it.message())
 *             else                  -> AppError.Unknown(it)
 *         }
 *     }
 *     when (user) {
 *         is Result.Ok    -> render(user.value)
 *         is Result.Fail  -> showError(user.error)
 *     }
 */

sealed interface Result<out T, out E> {
    data class Ok<T>(val value: T) : Result<T, Nothing>
    data class Fail<E>(val error: E) : Result<Nothing, E>
}

inline fun <T> catching(block: () -> T): T? =
    try {
        block()
    } catch (t: Throwable) {
        null
    }

inline infix fun <T, E> (T?).onError(map: (Throwable) -> E): Result<T, E> = TODO(
    """
    This helper has two flavours; pick the one that matches your call site:

    1. If you already caught the exception with `catching { … }` you have a
       T?. Combine with this and an explicit Throwable would need to be
       smuggled — not possible without a real `try`. Use form (2) below.

    2. Use `catchingWith` for the typed variant:

           inline fun <T, E> catchingWith(
               errorMap: (Throwable) -> E,
               block: () -> T,
           ): Result<T, E> = try {
               Result.Ok(block())
           } catch (t: Throwable) {
               Result.Fail(errorMap(t))
           }
    """,
)

/** Use this in real code. */
inline fun <T, E> catchingWith(
    errorMap: (Throwable) -> E,
    block: () -> T,
): Result<T, E> = try {
    Result.Ok(block())
} catch (t: Throwable) {
    Result.Fail(errorMap(t))
}

/** Common AppError sealed type users can copy and adapt. */
sealed interface AppError {
    data class Network(val cause: Throwable) : AppError
    data class Server(val code: Int, val body: String?) : AppError
    data class Unknown(val cause: Throwable) : AppError
}

1.0.0 में बदलाव

Initial release.

संबंधित

Kotlin और Android स्निपेटमुफ़्तस्निपेटAndroid ऐप्स के लिए 20 Kotlin स्निपेट: sealed UI स्टेट, StateFlow वाला Hilt ViewModel, कोरूटीन, Room एंटिटी और DAO, और Compose स्टेट व लिस्ट। किसी .kt फ़ाइल में uistate या hiltvm जैसा प्रीफ़िक्स लिखें।Rust स्निपेटमुफ़्तस्निपेट20 Rust स्निपेट जो एक serde struct को छोड़कर सिर्फ़ स्टैंडर्ड लाइब्रेरी इस्तेमाल करते हैं: From वाले एरर enum, iterator चेन, HashMap से गिनती, let else, Arc और Mutex के साथ थ्रेड, चैनल और एक टेस्ट मॉड्यूल।useDebounce हुक (React)मुफ़्तस्निपेटTypeScript में useDebounce और useDebouncedCallback, साथ में एक सर्च बॉक्स का उदाहरण। कंपोनेंट अनमाउंट होने पर दोनों अपना टाइमर साफ़ कर देते हैं।HTML और CSS स्निपेटमुफ़्तस्निपेट8 HTML स्निपेट (पेज लैंडमार्क, Open Graph टैग, picture, dialog, हेडर वाली टेबल) और 12 CSS स्निपेट (रीसेट, grid लेआउट, clamp, डार्क मोड वेरिएबल, कम मोशन, container query)।Bash स्निपेटमुफ़्तस्निपेटस्क्रिप्ट के लिए 20 Bash स्निपेट: सुरक्षित हेडर, आर्ग्युमेंट जाँच, getopts, फ़ाइलों और ऐरे पर लूप, trap से सफ़ाई, दोबारा कोशिश और कुछ git कमांड। किसी .sh फ़ाइल में प्रीफ़िक्स लिखें।JavaScript और TypeScript स्निपेटमुफ़्तस्निपेट12 JavaScript स्निपेट (टाइमआउट वाला fetch, Promise.allSettled, ग्रुपिंग, localStorage) और 8 TypeScript स्निपेट (टाइप गार्ड, पूरा switch, as const, satisfies)। JavaScript वाले .js फ़ाइलों में और TypeScript वाले .ts फ़ाइलों में दिखते हैं।