0Pricing
Kotlin Multiplatform Academy · درس

تحويل الأخطاء إلى رسائل الواجهة

تحويل حالات الفشل إلى نصوص محلية سهلة الاستخدام

تحويل الأخطاء إلى رسائل الواجهة درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Raw Errors Are Cryptic

A user should never see SerializationException on screen. Your job is to translate technical failures into calm, human sentences.

Define Friendly Categories

Group failures into a few error kinds like network, server and unknown. Each kind maps to one clear message the apps can show.

enum class ErrorKind { NETWORK, SERVER, UNKNOWN }

Carry the Kind in Failure

Extend your Failure to hold an ErrorKind, not just a raw string. The UI then decides wording instead of trusting backend text.

data class Failure(val kind: ErrorKind) : Result<Nothing>()

Map at the Boundary

Convert exceptions to an ErrorKind right where you catch them. After this point your code speaks only in friendly, stable categories.

catch (e: IOException) {
  Failure(ErrorKind.NETWORK)
}

A Message Lookup

Write one function that turns an ErrorKind into display text. Keep it in shared code so both apps phrase errors identically.

fun message(k: ErrorKind): String

Localize the Text

Real apps translate messages. Let each platform localize the kind, so the shared layer passes the category and the UI picks the language.

Keep Strings Out of Logic

Your rules return an ErrorKind, never a hardcoded sentence. Wording can change anytime without touching business logic.

Different Kinds, Different UI

A network error might show a retry button while a server error shows a support link. The kind drives both the words and the action.

Fallback for the Unknown

Always include an UNKNOWN kind. Unexpected failures still get a polite, generic message instead of a blank or scary screen.

Test the Mapping

Because the mapper is pure, you can test it in commonTest: feed an exception, assert the kind, with no UI or device involved.

One Source of Truth

Now errors travel as typed kinds from catch to screen, giving Android and iOS one consistent, friendly story for every failure.

Quick Check

Consider what the shared layer should expose to the UI.

Recap

You categorized failures into ErrorKinds, mapped exceptions at the boundary, and let each UI localize and act on the kind for a friendly experience. 💬

الأسئلة الشائعة

هل درس «تحويل الأخطاء إلى رسائل الواجهة» مجاني؟

نعم — نص درس «تحويل الأخطاء إلى رسائل الواجهة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

ماذا ستتعلم في «تحويل الأخطاء إلى رسائل الواجهة»؟

تحويل حالات الفشل إلى نصوص محلية سهلة الاستخدام تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟

لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «تحويل الأخطاء إلى رسائل الواجهة»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟

نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. نوع Sealed للنتيجة
  2. التقاط أخطاء الشبكة والتحليل
  3. تحويل الأخطاء إلى رسائل الواجهة
  4. إعادة المحاولة والبدائل غير المتصلة
← العودة إلى Kotlin Multiplatform Academy