エラーをUIメッセージにマッピングする
失敗をローカライズされた親しみやすいテキストに変換します
「エラーをUIメッセージにマッピングする」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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): 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. 💬
よくある質問
「エラーをUIメッセージにマッピングする」レッスンは無料ですか?
はい。「エラーをUIメッセージにマッピングする」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「エラーをUIメッセージにマッピングする」で何を学びますか?
失敗をローカライズされた親しみやすいテキストに変換します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「エラーをUIメッセージにマッピングする」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Sealed Result Type
- ネットワークとパースのエラーを捕捉する
- エラーをUIメッセージにマッピングする
- RetryとOffline Fallbacks