Map Errors to UI Messages
Turn failures into localized, user-friendly text.
Map Errors to UI Messages is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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): StringLocalize 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. 💬
Frequently asked questions
Is the “Map Errors to UI Messages” lesson free?
Yes — the full text of “Map Errors to UI Messages” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.
What will I learn in “Map Errors to UI Messages”?
Turn failures into localized, user-friendly text. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Multiplatform Academy?
No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Map Errors to UI Messages” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Multiplatform Academy lesson?
Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- A Sealed Result Type
- Catch Network & Parsing Errors
- Map Errors to UI Messages
- Retry & Offline Fallbacks