Codes – typed application outcomes for Kotlin and Java

Hacker News Top Tools

Summary

Codes is a library for JVM applications that provides stable outcome identities and explicit mappings for HTTP and gRPC, with zero runtime dependencies.

No content available
Original Article
View Cached Full Text

Cached at: 09/01/26, 12:33 PM

aalsanie/codes

Source: https://github.com/aalsanie/codes

Codes

Java 17+. Zero runtime dependencies.

Maven Central CI License

Codes provides stable application outcome identities and explicit boundary mappings for JVM applications. Applications keep their own domain result or error model and use Codes where multiple parts of a system need to agree on outcome meaning without coupling that meaning to HTTP, gRPC, serialization, or a framework.

A domain outcome can keep the same identity across boundaries:

com.example.payments:PAYMENT_DECLINED
                    |
                    +-- HTTP 422
                    +-- gRPC FAILED_PRECONDITION
                    +-- logs/metrics keep PAYMENT_DECLINED

Install

Gradle:

dependencies {
    implementation("io.github.aalsanie:codes:0.3.1")
}

Maven:

<dependency>
    <groupId>io.github.aalsanie</groupId>
    <artifactId>codes</artifactId>
    <version>0.3.1</version>
</dependency>

Java 17+. Zero runtime dependencies. Kotlin applications consume the same Java API with JSpecify nullability metadata.

Custom outcomes

OutcomeDefinition paymentDeclined = OutcomeDefinition.custom(
    "com.example.payments",
    "PAYMENT_DECLINED",
    OutcomeState.FAILED,
    "The payment was declined."
);

Outcome outcome = Outcome.of(paymentDeclined);

HttpOutcomeMapper http = HttpOutcomeMapper.standard()
    .withMapping(paymentDeclined, HttpStatusCode.of(422));

GrpcOutcomeMapper grpc = GrpcOutcomeMapper.standard()
    .withMapping(paymentDeclined, GrpcStatusCode.FAILED_PRECONDITION);

OutcomeCode is the stable machine identity. Protocol mappings do not change that identity.

Standard outcomes

OK

INVALID_ARGUMENT
UNAUTHENTICATED
PERMISSION_DENIED
NOT_FOUND
ALREADY_EXISTS
FAILED_PRECONDITION
OUT_OF_RANGE
RATE_LIMITED
CANCELLED
DEADLINE_EXCEEDED
ABORTED
UNIMPLEMENTED
UNAVAILABLE
INTERNAL
DATA_LOSS
RESOURCE_EXHAUSTED

OK is the standard successful outcome. Applications define domain-specific success, pending, and failure outcomes when the standard catalog does not match the operation.

Runtime occurrences

Outcome outcome = Outcome.of(
    StandardOutcomes.NOT_FOUND,
    "customerId=123"
);

System.out.println(outcome.getCode());
System.out.println(outcome.getMessage());
System.out.println(outcome.getDetail());

message comes from the reusable definition. detail belongs to one occurrence.

Structured issues

ValidationResult validation = ValidationResult.invalid(
    Issue.at("email", "Invalid email address.")
);

Outcome outcome = validation.toOutcome(StandardOutcomes.INVALID_ARGUMENT);

ValidationResult is a small convenience for aggregating issues. It is not intended to replace an application’s result, validation, or functional programming model.

HTTP

HttpStatusCode status = HttpOutcomeMapper.standard()
    .map(StandardOutcomes.NOT_FOUND)
    .orNull();

assert status == HttpStatusCode.NOT_FOUND;

Some standard outcomes are intentionally left unmapped for HTTP when the correct status depends on the application.

gRPC

GrpcStatusCode status = GrpcOutcomeMapper.standard()
    .map(StandardOutcomes.NOT_FOUND)
    .orNull();

assert status == GrpcStatusCode.NOT_FOUND;

The standard gRPC mapper covers all standard outcomes.

Kotlin

Java getters and static factories are directly usable as Kotlin properties and calls:

val outcome = Outcome.of(StandardOutcomes.NOT_FOUND, "customerId=123")
val status = HttpOutcomeMapper.standard().map(outcome).orNull()

check(outcome.code == StandardOutcomes.NOT_FOUND.code)
check(status?.value == 404)

Reference

License

Apache License 2.0.

Similar Articles

Kilo Code for JetBrains

Product Hunt

Kilo Code for JetBrains is a native, open-source coding agent that integrates with JetBrains IDEs, supporting local and remote development with parallel agents, GitHub PRs, and access to over 500 models.

rustc_codegen_jvm: Rust compiler backend to emit JVM bytecode

Lobsters Hottest

rustc_codegen_jvm is a custom Rust compiler backend that emits JVM bytecode, allowing Rust code to be compiled into runnable JAR files compatible with JVM 8+. It supports a wide range of Rust features including control flow, data structures, traits, and closures.

Kilo-Org/kilocode

GitHub Trending (daily)

Kilo Code is an open-source AI coding agent that integrates with VS Code, JetBrains, and the CLI, offering access to 500+ models at provider pricing with no markup.