التقاط أخطاء الشبكة والتحليل
تحويل استثناءات Ktor وserializer إلى نتائج
التقاط أخطاء الشبكة والتحليل درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Where Errors Begin
Most failures appear at two spots: the network call and the JSON parse. Catching them here keeps the rest of your shared code clean and calm.
Wrap the Risky Call
Put the Ktor request and the decode inside a single try block. One guarded spot is easier to reason about than scattered checks.
try {
val user = client.get(url).body<User>()
} catch (e: Exception) { }Catch Network Failures
A dropped connection throws an IOException. Catch it and turn it into a clear Failure your UI can present as a connectivity problem.
catch (e: IOException) {
Failure("No connection")
}Catch Parsing Failures
When JSON does not match your model, kotlinx.serialization throws a SerializationException. Catch it separately so you know data, not the network, was bad.
catch (e: SerializationException) {
Failure("Bad data")
}Order Your catch Blocks
List the specific exceptions first and a broad Exception last. Kotlin matches top to bottom, so narrow types must come before the catch-all.
Return, Do Not Rethrow
Inside each catch, build a Failure and return it. Rethrowing would just push the crash up to the apps you are trying to protect.
Keep It in commonMain
This guarding belongs in shared code, right around the call. Both Android and iOS then receive the same safe Result with no extra work.
Inspect Ktor Status Codes
A reachable server can still answer 404 or 500. Check the response status and map non-2xx codes to a Failure too.
if (!resp.status.isSuccess())
return Failure("Server error")Avoid Swallowing Silently
Never leave an empty catch. Log the cause before returning Failure so you can still debug what really happened on a device.
runCatching as a Shortcut
Kotlin offers runCatching to wrap a block into a Result-like value, a tidy alternative when you do not need custom catch logic.
val r = runCatching { fetch() }One Funnel, Two Sources
Network and parsing errors now flow through one funnel into your Failure type, so callers handle both with the same simple branch.
Quick Check
Think about how kotlinx.serialization signals trouble.
Recap
You wrapped the call in try, caught IOException and SerializationException separately, mapped bad status codes, and returned tidy Failures from shared code. 🛡️
الأسئلة الشائعة
هل درس «التقاط أخطاء الشبكة والتحليل» مجاني؟
نعم — نص درس «التقاط أخطاء الشبكة والتحليل» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «التقاط أخطاء الشبكة والتحليل»؟
تحويل استثناءات Ktor وserializer إلى نتائج تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «التقاط أخطاء الشبكة والتحليل»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- نوع Sealed للنتيجة
- التقاط أخطاء الشبكة والتحليل
- تحويل الأخطاء إلى رسائل الواجهة
- إعادة المحاولة والبدائل غير المتصلة