diff options
| author | Murilo Ijanc <[email protected]> | 2026-07-27 20:27:46 -0300 |
|---|---|---|
| committer | Murilo Ijanc <[email protected]> | 2026-07-27 20:27:46 -0300 |
| commit | 2795e86ccf5a15a7601611aaa12be1e0758c963a (patch) | |
| tree | 6a348960633e3e62eaeb102e74e8d1ceab68d65a /README.md | |
| parent | 329f679cd359bcab92adf3366cf17711836d0c79 (diff) | |
| download | java-aabedb3032fa1ea42e364c4d18a4c142da08ddeb.tar.gz | |
A CEP that fails is almost never random: it is someone who knows the city
and guessed the number, usually the -000 that 957 municipalities never
received. The API now answers those 404s with a faixa block naming the
municipality that owns the band, so CepNotFoundException exposes it and
says it in its message.
It stays an error. The band is context, not an address, and its cep_ini
and cep_fim are the smallest and largest CEP that actually exist there —
not the bounds the Correios publish, and not a dense interval — so the
type documents both traps rather than inviting callers to walk the range.
Reading it is best effort: a missing, malformed or oddly shaped faixa is
dropped, because turning a definitive answer about the request into a
parse failure would hide the answer.
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 45 |
1 files changed, 42 insertions, 3 deletions
@@ -26,7 +26,7 @@ cd cepis-java make build ``` -That writes `build/cepis-0.1.0.jar`; put it on the classpath. +That writes `build/cepis-0.2.0.jar`; put it on the classpath. ## Usage @@ -162,7 +162,7 @@ Client client = Client.builder().baseUrl("http://localhost:8080").build(); From the command line, to confirm quickly that it answers: ```sh -java -jar build/cepis-0.1.0.jar --base-url http://localhost:8080 01001000 +java -jar build/cepis-0.2.0.jar --base-url http://localhost:8080 01001000 ``` ## Errors @@ -174,7 +174,7 @@ caused it: | error | when | does retrying help? | | --- | --- | --- | | `InvalidCepException` | not 8 digits | no | -| `CepNotFoundException` | well-formed, not in the database | no | +| `CepNotFoundException` | well-formed, not in the database; may carry `faixa()` | no | | `RateLimitedException` | 429; carries `retryAfter()` | yes, later | | `ServerErrorException` | 5xx; carries `status()` | yes, later | | `NetworkException` | DNS, connection, timeout, TLS | yes, later | @@ -198,6 +198,45 @@ more. API is answering instead of it — a captive Wi-Fi portal, a corporate proxy, an HTML error page. Useful as a diagnosis: the problem is not in your code. +### The municipality behind a CEP that does not exist + +A CEP that fails is rarely random: it is usually someone who knows the city and +guessed the number — the classic guess being the `-000`, which 957 +municipalities never received. So a `CepNotFoundException` may name the +municipality whose band the CEP fell in: + +```java +try { + Address address = Cepis.lookup("25845-000"); +} catch (CepNotFoundException e) { + e.faixa().ifPresent(faixa -> { + faixa.describe(); // "Areal/RJ" + faixa.localidade(); // "Areal" + faixa.ibge(); // "3300225" + faixa.cepIni(); // "25845001" + faixa.cepFim(); // "25849899" + faixa.ceps(); // 118 + }); +} +``` + +Three things to keep straight, because getting any of them wrong is worse than +not having the field at all: + +- **It is still a 404.** This is context for the error, not an address. Do not + let it turn a not-found into a success — the CEP asked for does not exist. +- **`cepIni` and `cepFim` are the smallest and the largest CEP that *actually + exist* there**, not the bounds the Correios publish. The DNE's own start for + Areal is `25845000`, which is exactly the CEP that just failed. +- **The interval is sparse.** Areal's 118 CEPs are spread over some 9000 + numbers, so almost nothing between the two exists. That is what `ceps()` is + for. Never present the pair as a range, or a list, of valid CEPs. + +`faixa()` is empty whenever the CEP fell in a band no municipality owns; the API +omits the field in that case rather than sending null. A `faixa` that is +malformed, empty or of the wrong shape is dropped the same way: the not-found +itself is never at risk of becoming a parse failure. + ## Development Everything runs through `make` and the JDK. There is nothing to install first |