Signatures, be true: domain errors and functional handling in Kotlin - The JetBrains Blog
Blog post from JetBrains
The article argues that Kotlin backend functions should expose expected business failures in their return types rather than hiding them through exceptions or `Unit` returns, using `Either<DomainError, Success>` and sealed error interfaces to make outcomes explicit and compiler-enforced. It distinguishes API client errors, which can be handled as broad HTTP 4xx responses; unexpected infrastructure failures, which should remain exceptions and yield operational signals such as 500 responses and alerts; and domain errors, such as invalid signing codes or closed signing windows, which healthy clients must handle differently and therefore belong in the contract. Narrow sealed error unions per public method enable exhaustive `when` handling and avoid anti-patterns such as `Either<Throwable, Unit>` or overly broad shared error types that force callers to consider impossible cases. The approach can be composed with Kotlin early returns or Arrow utilities, while HTTP mapping should occur only at route boundaries so lower layers remain focused on business outcomes. Although explicit error types add verbosity, the article maintains that they improve code review, client-server consistency, AI-generated code verification, testing, and maintainability by making a function’s real behavior visible in its signature.
No tracked trend matches for this post yet.
Use this post, company, and trend context to find content marketing opportunities, perform competitive analysis, or address product feature gaps via the Plushcap MCP server or the Plushcap API.