Error Handling
Slumber's two-pass strategy gives you speed when things work and precision when they don't.
The two-pass strategy
When you call codec.awake(), two things happen:
- Fast pass — Deserializes with no path tracking. Minimal overhead. If it succeeds, you're done.
- Tracking pass — Only runs if the fast pass throws. Re-deserializes with full path tracking, collecting diagnostic information at every level.
This means you pay zero cost for path tracking in the success case — which is the common case.
Nullable vs non-nullable errors
The key design decision: nullable fields absorb errors, non-nullable fields report them.
data class Profile(val name: String, val bio: String?)
// Non-nullable field with bad data → AwakerException
codec.awake<Profile>(mapOf("name" to listOf(1, 2, 3), "bio" to "hello"))
// → AwakerException: "Value at path 'root.name' must not be null"
// Nullable field with bad data → graceful null
codec.awake<Profile>(mapOf("name" to "Alice", "bio" to listOf(1, 2, 3)))
// → Profile(name="Alice", bio=null) ← no crash Path tracking in nested structures
Error messages include the full path to the failing field:
data class Address(val zip: String)
data class Company(val address: Address)
data class Employee(val company: Company)
codec.awake<Employee>(mapOf(
"company" to mapOf(
"address" to mapOf(
"zip" to null // null for non-nullable String
)
)
))
// → AwakerException: "Value at path 'root.company.address.zip' must not be null" Collection index in error paths
When an error occurs inside a collection, the index is included in the path:
data class Container(val items: List<Int>)
codec.awake<Container>(mapOf(
"items" to listOf(1, null, 3)
))
// → AwakerException: "Value at path 'root.items.1' must not be null"
// ^ index 1 Missing required parameters
When a non-nullable, non-default parameter is missing from the input:
data class User(val name: String, val age: Int)
codec.awake<User>(mapOf("name" to "Alice"))
// → AwakerException with message containing:
// "Value at path 'root' must not be null"
// "'root': User misses parameters 'age'" Exception types
Slumber defines three exception classes:
// Base class for all Slumber errors
open class SlumberException(message: String) : Throwable(message)
// Thrown when deserialization fails
class AwakerException(
message: String,
val logs: List<String>, // Diagnostic messages from nested levels
val rootType: KType?, // The type being deserialized
val input: Any?, // The input data that caused the error
) : SlumberException(message)
// Thrown when serialization fails (e.g. null for non-nullable type)
class SlumbererException(
message: String,
val input: Any?, // The data that caused the error
) : SlumberException(message) Catching and handling errors
try {
val user = codec.awake<User>(untrustedData)
} catch (e: AwakerException) {
println("Deserialization failed at: extracted from message")
println("Root type: ${e.rootType}")
println("Input: ${e.input}")
println("Diagnostics: ${e.logs.joinToString("\n")}")
} Default parameter fallback
When a field has a default value and the input provides invalid data, the default is used:
data class Config(
val host: String = "localhost",
val port: Int = 8080,
)
// Invalid data for "port" → falls back to default
codec.awake<Config>(mapOf("host" to "myserver", "port" to "not_a_number"))
// → Config(host="myserver", port=8080)