Skip to main content

kiit-codes logokiit-codes

A Kotlin library for classifying and handling success and failure.

A small, dependency-free status and error taxonomy for application outcomes, with extensible codes, protocol mappings, validation, typed exceptions, and optional Result<T, E> integration.

Kiit Codes overview

Overview

Goals

Applications need to communicate a simple idea consistently: what happened? In practice, success and failure get modeled differently across domains, layers, and protocols, which causes recurring problems: no shared taxonomy for classifying outcomes, inconsistent handling across layers, validation/exceptions/statuses/results all using different approaches, similar error types rebuilt project to project, and generic errors that lose domain-specific meaning.

kiit-codes exists to provide a shared, application-level model for these concerns: a fixed taxonomy for consistent classification, extensible codes that preserve domain-specific meaning, and protocol mappings that keep application outcomes independent from how they're transported. The same model is then reused across statuses, validation, exceptions, and result types.

Inspiration

#SourceWhat was drawn from it
1HTTP status codesValidated against, not derived from — the most common HTTP codes map onto kiit-codes' eight groups without needing a ninth.
2gRPC status codesSame validation as HTTP — every gRPC code maps onto the existing eight groups.
3Scala's Either/TryBuilt on Either's two-branch shape, structured around Try's branches, with the Status taxonomy incorporated on top.

Activity

The core classification model has years of internal production use inside the original Kiit framework, powering both mobile and server Kotlin applications, prior to being extracted into this standalone repository. The public package version reflects the standalone repo's youth, not the underlying design's: the classification itself is settled, while newer additions (JS/TS export, iOS/Swift export via SKIE) have less track record and are still being exercised.

Resources

#ResourceDetails
1Repositorygithub.com/kiitdev/kiit-codes
2Maven coordinatedev.kiit:kiit-codes
3npm coordinate@kiit/codes (JS/TS export, not CI-gated yet)
4Related modulekiit-result builds a Result<T, E> type on top of this same taxonomy (docs page coming next)
5API referenceGenerated from source KDoc, linked here once published

Prefer to see it work first? Jump to the Tutorial. Prefer the reasoning first? Keep reading.

Setup

Install

dependencies {
implementation("dev.kiit:kiit-codes:1.0.1")
}

Source

#ItemLink
1Git Repogithub.com/kiitdev/kiit-codes
2Root folder of sources in repokiit-codes/src/commonMain/kotlin
3Sample appsamples/sample-kotlin
4Package Namekiit.codes
5Unit Testskiit-codes/src/commonTest

Licensed Apache 2.0.

Example

import kiit.codes.*

fun authorize(userId: String, requesterId: String): Status =
if (userId != requesterId) Restricted.UNAUTHORIZED
else Succeeded.SUCCESS

when (val status = authorize(userId, requesterId)) {
is Passed -> log.info("ok: ${status.name}")
is Failed -> log.warn("failed: ${status.name}${status.message}")
}

Concepts

Terms

#TermDefinition
1TaxonomyThe overall Status → Group → Code classification system.More
2StatusSealed interface for an operation's outcome: Passed or Failed.More
3GroupSecond tier: a fixed subtype of Passed/Failed (e.g. Restricted).More
4CodeThird tier: an open Status instance within a group (e.g. DENIED).More
5ErrError representation for use with Result/Outcome-style types.More
6CheckedNon-monadic validation result reporting every problem, not just the first.More
7StatusExceptionSealed exception hierarchy carrying a Checked, for exception-only boundaries.More

Status

Every Status belongs to exactly one Group, and every concrete status value is a Code within that Group. Passed.Succeeded.SUCCESS is the SUCCESS Code inside the Succeeded Group, under the Passed Status. Failed.Restricted.DENIED is the DENIED Code inside the Restricted Group, under the Failed Status.

Succeeded.CREATED is one built-in Code. Its fields, each read on its own line:

val status: Status = Succeeded.CREATED

status.name // "CREATED"
status.origin // "kiit"
status.message // "A new resource was created."
status.success // true
status.group // "Succeeded"
FieldDefinition
nameStable SCREAMING_SNAKE_CASE label, e.g. "TOKEN_EXPIRED", for logs.
originWhere a status came from: "kiit" for built-ins, "custom" by default.
messageHuman-readable constant description. Never built from runtime data.
successtrue for Passed, false for Failed.
groupThe fixed Group this status belongs to, e.g. "Succeeded", "Pending", "Excluded", or "Restricted".

Taxonomy

The full Status → Group → Code taxonomy: every built-in Passed and Failed group, and every built-in code within each.

Kiit Codes taxonomy

TierParentFixed/OpenChildrenDescription
1Status FixedPassed
Failed
2Group FixedSucceededThe operation completed successfully.
PendingThe operation was accepted but has not yet fully resolved.
ExcludedThe item was intentionally excluded from the operation.
InformationThe response provides information; no operation was performed.
RestrictedThe caller is not allowed.
InvalidThe request itself is wrong.
RejectedThe caller was allowed, but the business refuses it.
UnservedThe system can't serve it right now, though nothing was wrong with the request.
3Code Open + DefaultsShips with common built-in codes (e.g. SUCCESS, DENIED); extensible with custom, domain-specific codes within the same group.

Passed

Passed.success == true.

GroupCodeDescription
SucceededSUCCESSThe operation completed successfully.
CREATEDA new resource was created.
UPDATEDThe resource was fully updated.
PATCHEDThe resource was partially updated.
FETCHEDThe resource was retrieved.
DELETEDThe resource was deleted.
HANDLEDThe request was handled; nothing to return.
REFERREDThe result is at another location.
EXITEDThe application exited cleanly.
PendingACCEPTEDThe request was accepted.
QUEUEDThe request is waiting to be processed.
PROCESSINGThe request is being processed.
CONFIRMThe request is awaiting confirmation.
REDIRECTEDThis request is being handled elsewhere.
SCHEDULEDThe operation is scheduled for later.
ExcludedOMITTEDThe item was excluded from the result.
SKIPPEDThe item was not processed.
DISCARDEDThe item was processed, then excluded for unrelated reasons.
CANCELLEDThe operation was cancelled by the caller before completion.
DEDUPLICATEDThe duplicate item was not processed.
DISQUALIFIEDThe item was disqualified.
InformationNOTICEAn informational notice.
ADVISORYA notice that may need attention.
METADATAInformation about the application itself was returned.
HEALTHThe service is healthy and operational.
DIAGNOSTICSDiagnostic or operational information was returned.
MOVEDThe resource has permanently moved to a new location.

Failed

Failed.success == false.

GroupCodeDescription
RestrictedDENIEDThe request was denied.
UNAUTHENTICATEDAuthentication is required.
UNAUTHORIZEDThe caller lacks permission.
FORBIDDENAccess to this resource is forbidden.
LOCKEDAccess is locked; resolve the condition to restore access.
SUSPENDEDAccess has been administratively suspended.
InvalidINVALID_VALUEThe request had an invalid value.
BAD_REQUESTThe request was malformed.
NOT_FOUNDThe requested route or endpoint does not exist.
OUT_OF_RANGEA value was outside the acceptable range.
PAYLOAD_TOO_LARGEThe payload is too large.
MISSING_FIELDA required field was not provided.
RejectedRULE_VIOLATIONA business rule rejected the request.
CONFLICTThe request conflicts with the current state.
NOT_EXISTSThe referenced item does not exist.
PRECONDITION_FAILEDA required precondition was not met.
EXPIREDThe item has expired.
GONEThe resource was removed and is no longer available.
UnservedUNEXPECTEDAn unexpected, unclassified error occurred.
UNSUPPORTEDThis capability is not currently available.
TIMEOUTThe operation timed out.
RATE_LIMITEDToo many requests; try again later.
RESOURCE_LIMITEDA resource limit has been reached.
UNREACHABLEA required dependency could not be reached.
UNDER_MAINTENANCEThe service is temporarily under maintenance.
INTERNALAn internal invariant was violated.
DATA_LOSSUnrecoverable data loss or corruption occurred.
DEGRADEDThis dependency is degraded; some calls may be refused.
LEGAL_BLOCKAccess is blocked for legal reasons.
ABORTEDThe operation was aborted; retrying may help.

Err

Error representation for use with Validation, Exceptions, and Result types. This stores instance level error details and the building block for Checked's error list.

sealed class Err {
abstract val message: String

data class ErrorInfo(override val message: String, val cause: Throwable? = null) : Err()
data class ErrorField(val field: String, val value: String, override val message: String) : Err()
data class ErrorList(val errors: List<Err>, override val message: String) : Err()
}
VariantFieldsUse
Err.ErrorInfomessage, cause?, ref?Default implementation: a message with an optional cause.
Err.ErrorFieldfield, value, message, cause?, ref?An error on a specific field.
Err.ErrorListerrors, message, cause?, ref?Wraps a list of other errors.
BuilderUse
Err.of(message)Plain message, no field or cause.
Err.of(status)Build directly from a Status.
Err.on(field, value, message)Error on a specific field, including its value.
Err.on(field, message)Same, but omits the value — for sensitive fields.
Err.ex(throwable)Wrap a caught exception or throwable.
Err.obj(any)Wrap an arbitrary object as the cause.
Err.list(strings, message)Build an Err.ErrorList from a list of plain strings.
Err.build(any?)Generic builder that dispatches based on the input's type.

Checked

Non-monadic validation result that reports every problem at once, instead of stopping at the first. Checked(status: Status, errors: List<Err>), reachable only through Checked.success(status) or Checked.failure(status, errors).

class Checked private constructor(
val status: Status,
val errors: List<Err>,
) : HasErrors {
val isValid: Boolean get() = errors.isEmpty()

companion object {
fun success(status: Passed = Succeeded.SUCCESS): Checked
fun failure(status: Failed, errors: List<Err>): Checked
}
}
#TraitDetails
1Invariantstatus and errors can never disagree: a passing Checked always has an empty errors list, a failing one always has at least one entry.
2isValidBoolean, reflects errors.isEmpty().
3InterfaceImplements HasErrors.
4collect(...)collect(vararg checks) / collect(checks: List<Checked>) combine multiple Checked into one, failing with Invalid.INVALID_VALUE and every pooled error if any input failed.

Exceptions

Sealed exception hierarchy carrying a Checked, for boundaries that only understand exceptions — one subclass per Failed group:

sealed class StatusException(val checked: Checked) : Exception() {
val status: Status get() = checked.status
val errors: List<Err> get() = checked.errors

class RestrictedException(status: Failed.Restricted, errors: List<Err> = emptyList()) : StatusException(...)
class InvalidException(status: Failed.Invalid, errors: List<Err> = emptyList()) : StatusException(...)
class RejectedException(status: Failed.Rejected, errors: List<Err> = emptyList()) : StatusException(...)
class UnservedException(status: Failed.Unserved, errors: List<Err> = emptyList()) : StatusException(...)
}
ExceptionMatches
RestrictedExceptionFailed.Restricted
InvalidExceptionFailed.Invalid
RejectedExceptionFailed.Rejected
UnservedExceptionFailed.Unserved
#TraitDetails
1CarriesA Checked, exposed as status: Status and errors: List<Err>.
2ConversionFailed.toException(errors) converts a bare Failed status into the matching subclass.
3Platform equivalentsiOS via @ObjCName in iosMain; JS/TS via jsMain.

Protocols

Maps Status to and from external protocol representations — HTTP and gRPC out of the box, or a custom protocol of your own via CodeLookup.

TypePurpose
CodesToHttpMaps Status to/from HTTP status codes.
CodesToGrpcMaps Status to/from gRPC status codes.
CodeLookupInterface for defining a mapping to any other protocol.
CompositeLookupCombines a base CodeLookup with per-code extensions/overrides.

Kiit Codes protocol mappings

Design

Philosophy

A closed taxonomy keeps generic handling, exhaustive matching, logging, and protocol mappings consistent everywhere a status is used. Codes stay open underneath so each domain can extend the taxonomy freely without forking it. This doesn't replace domain modeling: domain errors explain what happened in one domain, the taxonomy explains what kind of outcome it was, consistently, across every domain in an application. Status is a sealed interface rather than an enum specifically so consumers can add their own codes while still participating in the same taxonomy — an enum can't be extended this way.

Features

#FeatureDescription
1Status classificationThe core Passed/Failed split, with a fixed Group and an open Code beneath it for finer-grained classification.
2ExtensibilityAdd domain-specific codes within the same fixed groups, without forking the taxonomy or losing shared meaning.
3Protocol mappingsMap statuses to and from HTTP, gRPC, or any custom protocol via CodeLookup/CompositeLookup.
4ValidationChecked, Err, and collect report every problem found at once, instead of stopping at the first.
5Typed exceptionsStatusException and Failed.toException() for boundaries that only understand exceptions.
6Result integrationThe separate kiit-result module builds a Result<T, E> type on top of this same taxonomy.

Limitations

#LimitationDetails
1AI framing is unprovenStable names and explicit classification are expected to reduce ambiguity for AI tooling, but that's a hypothesis, not a benchmarked result.
2JS/TS not CI-gatedExists but isn't CI-gated or published to npm yet; lacks the compiler-enforced exhaustiveness that Kotlin, Java, and Swift (via SKIE) get.

Exclusions

#ExcludedReasoning
1Retry logic or severity levelsRetryability cuts across groups rather than aligning with them — Unserved alone has both retryable and non-retryable codes. A dedicated Retry category was considered and rejected for the same reason.
2A numeric status code fieldAn earlier version had one; it invited the wrong inference (looking like an HTTP code while meaning something else). Real protocol numbers are available on demand via CodesToHttp/CodesToGrpc, never implied by the taxonomy itself.
3A ninth groupEvery gRPC code and the most common HTTP codes map onto the existing eight without needing one, tested directly against both.

Tutorial

Status Codes

This walks through building a tiny service that returns Status for expected outcomes, then crosses a boundary that can only communicate via exceptions.

Define a service that returns a Status instead of throwing for expected failures:

import kiit.codes.*

data class User(val id: String, val email: String)

class UserService {
private val users = mutableMapOf<String, User>()

fun create(id: String, email: String): Status {
if (email.isBlank()) return Invalid.BAD_REQUEST
if (users.containsKey(id)) return Rejected.CONFLICT
users[id] = User(id, email)
return Succeeded.CREATED
}

fun authorize(id: String, requesterId: String): Status =
when {
!users.containsKey(id) -> Rejected.NOT_EXISTS
id != requesterId -> Restricted.UNAUTHORIZED
else -> Succeeded.SUCCESS
}
}

Callers

Call it and branch on the result:

val service = UserService()

val created = service.create("alice", "alice@example.com")
println("${created.name} (success=${created.success})") // CREATED (success=true)

val denied = service.authorize("alice", "bob")
println("${denied.name} (success=${denied.success})") // UNAUTHORIZED (success=false)

Validation

Now add a method that reports every problem at once instead of stopping at the first:

fun UserService.validateSignup(id: String, email: String): Checked {
val errors = mutableListOf<Err>()
if (id.isBlank()) errors.add(Err.on("id", id, "Id is required"))
if (!email.contains("@")) errors.add(Err.on("email", email, "Email must contain @"))
return if (errors.isEmpty()) Checked.success(Succeeded.SUCCESS)
else Checked.failure(Invalid.INVALID_VALUE, errors)
}

val checked = service.validateSignup("", "not-an-email")
println("valid=${checked.isValid}, errors=${checked.errors.size}")
// valid=false, errors=2

Checked can only be constructed through Checked.success(status)/Checked.failure(status, errors), so status and errors can never disagree. See Concepts for the full type, or Guide for collect(...) combining multiple Checked results into one.

Try/Catch

Now add a method that throws instead, for a caller that only understands exceptions:

fun UserService.requireAuthorized(id: String, requesterId: String) {
val status = authorize(id, requesterId)
if (status is Failed) throw status.toException()
}

try {
service.requireAuthorized("alice", "bob")
} catch (e: StatusException) {
println("caught: ${e.status.name}${e.message}")
// caught: UNAUTHORIZED — Not authorized to perform this action
}

status.toException() picked StatusException.RestrictedException automatically, since Restricted.UNAUTHORIZED belongs to the Restricted group. See Concepts for the full exception hierarchy, or Design for why the taxonomy is shaped this way.

Result

kiit-codes classifies an outcome, but doesn't hand back a value alongside it. For that, pair it with kiit-result — a separate Kiit library that builds a Result<T, E> type on this same taxonomy:

fun UserService.find(id: String): Result<User, Status> =
users[id]?.let { Result.success(it) } ?: Result.failure(Rejected.NOT_EXISTS)

See the kiit-result docs for the full API.

Guide

Usage

Status only, when the outcome itself is enough:

when (val status = authorize(userId, requesterId)) {
is Passed -> log.info("ok: ${status.name}")
is Failed -> log.warn("failed: ${status.name}${status.message}")
}

Extensibility — custom codes stay inside a built-in group:

val PAYMENT_DECLINED = Failed.Rejected(
name = "PAYMENT_DECLINED",
message = "Payment declined",
origin = "payments",
)

PAYMENT_DECLINED remains a Rejected outcome everywhere in the system while retaining its own domain-specific identity. origin keeps custom namespaces distinct from "kiit" and from other teams' codes.

Kiit Codes custom codes

Validation, reporting every problem instead of stopping at the first:

fun validateUser(name: String, email: String): Checked {
val errors = mutableListOf<Err>()
if (name.isBlank()) errors.add(Err.on("name", name, "Name is required"))
if (!email.contains("@")) errors.add(Err.on("email", email, "Email must contain @"))
return if (errors.isEmpty()) Checked.success() else Checked.failure(Invalid.INVALID_VALUE, errors)
}

Exceptions, converting a Failed status at a boundary that needs one:

fun requireAuthorized(id: String, requesterId: String) {
val status = authorize(id, requesterId)
if (status is Failed) throw status.toException()
}

Kiit Codes usage

Protocols

Working code for the types introduced in Concepts — mapping statuses to and from HTTP, gRPC, and a custom protocol of your own.

HTTP, via CodesToHttp:

val http = CodesToHttp()

http.toCode(Succeeded.CREATED) // 201
http.toCode(Invalid.INVALID_VALUE) // 400
http.toStatus(404)?.name // "NOT_FOUND"

gRPC, via CodesToGrpc:

val grpc = CodesToGrpc()

grpc.toCode(Restricted.DENIED) // 7, PERMISSION_DENIED
grpc.toStatus(6)?.name // "CONFLICT", ALREADY_EXISTS reversed

Custom protocols, via CodeLookup/CompositeLookup:

val lookup = CompositeLookup(
base = CodesToHttp(),
extensions = mapOf(PAYMENT_DECLINED to 402),
)

lookup.toCode(PAYMENT_DECLINED) // 402

FAQ

Common questions about the taxonomy, design choices, alternatives, adoption, and project maturity.

Why

QuestionAnswer
Why not just use exceptions or booleans?Exceptions are thrown inconsistently across a codebase, and a boolean can't say why. This gives every outcome a shared shape, closed categories, open codes underneath.
Why a closed taxonomy but open codes?Closed categories keep generic handling, exhaustive matching, logging, and protocol mappings consistent everywhere. Codes stay open so each domain can extend it freely.
Why classify outcomes if my domain errors already explain what happened?Domain errors explain what happened in one domain. The taxonomy explains what kind of outcome it was, consistently, across every domain in the app.
Does this replace domain modeling?No. It classifies outcomes; it doesn't replace aggregates, value objects, or domain events. An infrastructure-level vocabulary, not a competing one.

Alternatives

QuestionAnswer
How is this different from Arrow's Either/Validated or kotlin-result?Those give you a Result type with no taxonomy underneath, you supply the meaning yourself. This provides the taxonomy those types can build on, plus a working exception path.
Why not just use raw HTTP status codes everywhere?A background job or CLI command doesn't have an HTTP status. HTTP was the closest precedent, and the taxonomy is validated against it, but it isn't scoped to HTTP.
Doesn't this lock me into Kiit's taxonomy?The eight categories are closed and cross-validated against HTTP and gRPC. Every code inside them is yours to extend, and you're free to ignore the built-in ones entirely.

API

QuestionAnswer
Why not just use strings for status names?Strings don't give you compiler-checked exhaustiveness, discoverability, or protocol mappings. The goal is consistent classification, not just naming.
Why isn't Status just an enum?Enums can't be extended by consumers. This lets every application define its own statuses while still participating in the same taxonomy.
Why exactly eight categories?Every gRPC code and the most common HTTP codes map onto these eight without needing a ninth, tested directly against both.
Why was the numeric status code field removed?An earlier version had one, and it invited the wrong inference, a number resembling an HTTP code but meaning something else. Real protocol numbers are available on demand, never implied.
Isn't 50+ codes a steep learning curve?Most of the real cost is the eight categories, not the codes. Each category's default is a safe fallback; the rest is opt-in precision you reach for as needed.
Why is Unserved so much bigger than the others?Independent evidence, not an oversight, both HTTP and gRPC show the same clustering on their own for capacity and infrastructure failures.
Why isn't retry logic or severity built in?Retryability cuts across categories rather than aligning with them; Unserved alone has both retryable and non-retryable codes. A dedicated Retry category was considered and rejected.
Doesn't a generic category lose domain-specific detail?No, the category is deliberately coarse while the code stays domain-specific. PAYMENT_DECLINED and ORDER_CONFLICT can both be Rejected and still keep distinct identities.

Adoption

QuestionAnswer
What if I classify something incorrectly?Nothing catastrophic, a status can be moved to a more appropriate category later. The taxonomy improves consistency, it doesn't enforce absolute correctness upfront.
What if my company already has its own status system?You don't have to replace it overnight. Existing statuses can map into the taxonomy incrementally while keeping their original names and meanings.
How does this work across microservices?Services don't need identical codes, only the shared categories. Each service keeps its own domain-specific statuses while exposing consistent high-level semantics.

AI

QuestionAnswer
Is the "built for AI" angle just marketing?The design decisions are justified on ordinary engineering grounds first, consistency, exhaustive matching, explicit semantics. AI benefits from the same properties, but the library stands on its own without them.
What evidence supports the AI-related claims?Intentionally modest. Stable names and explicit classification are expected to reduce ambiguity for AI tooling, but that's a hypothesis to validate with real benchmarks, not an assumed result.

Maturity

QuestionAnswer
Is this production-ready at 1.0.1?The version reflects the public package's youth, not the underlying design's. The core classification has years of internal production use prior to extraction; newer pieces (JS/TS, iOS) have less track record.
What about single-maintainer risk?Real risk, worth being upfront about. Apache 2.0 licensed and source available, but there's currently no second maintainer or organizational backing.