From 42e586658b4bc39542a5311842f0040f7d29cb2b Mon Sep 17 00:00:00 2001 From: Murilo Ijanc Date: Mon, 27 Jul 2026 20:31:13 -0300 Subject: Name the municipality behind a CEP that does not exist MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The API now hangs an optional faixa block off the 404 of GET /{cep}, and this exposes it as CepNotFoundException.Faixa. A CEP that fails is rarely random: it is usually someone who knows the city and guessed the number, most often the -000 that 957 municipalities never received. Knowing where the guess landed turns a dead-end "CEP not found" into a question worth asking the user. It stays an error. The block names the municipality the number would belong to, never an address, and the lookup still throws. CepIni and CepFim are the smallest and largest CEP that actually exist there, not the range the Correios publish — the published start for Areal is 25845000, exactly the CEP that fails — and the interval between them is sparse, which is what Ceps is for. Parsing is deliberately toothless: absent, misshapen or nameless, the block becomes null and the caller gets the same clean not-found it got before the field existed. A courtesy on top of a 404 must never be able to turn it into a parse failure. --- src/Cepis/CepNotFoundException.cs | 44 +++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) (limited to 'src/Cepis/CepNotFoundException.cs') diff --git a/src/Cepis/CepNotFoundException.cs b/src/Cepis/CepNotFoundException.cs index ced7b16..19a4576 100644 --- a/src/Cepis/CepNotFoundException.cs +++ b/src/Cepis/CepNotFoundException.cs @@ -4,24 +4,60 @@ namespace Cepis; /// The CEP is well-formed but does not exist in the DNE database. /// /// +/// /// Retrying does not help: the answer is definitive until the next fortnightly /// database. A batch never throws this — a CEP with no address is /// at its position instead. +/// +/// +/// carries the municipality the CEP would have belonged to, +/// when the API can name one. It is context for the failure and not a fallback +/// address: this is still a 404. +/// /// public sealed class CepNotFoundException : CepisException { - /// Create the exception with a message. + /// Create the exception with a message and the range, if any. /// what went wrong - public CepNotFoundException(string message) + /// + /// the municipality the CEP would belong to, or when + /// the API named none + /// + public CepNotFoundException(string message, Faixa? faixa = null) : base(message) { + Faixa = faixa; } - /// Create the exception with a message and an underlying cause. + /// Create the exception with a message, the range and a cause. /// what went wrong + /// the municipality the CEP would belong to, if any /// the failure underneath - public CepNotFoundException(string message, Exception? innerException) + public CepNotFoundException(string message, Faixa? faixa, Exception? innerException) : base(message, innerException) { + Faixa = faixa; } + + /// + /// The municipality the CEP would have belonged to, or + /// when there is none. + /// + /// + /// + /// is the ordinary case for a CEP falling in a band + /// no municipality owns: the API omits the block rather than sending + /// a null, and an older API does not send it at all. Anything unreadable in + /// it lands here as too — a courtesy field is not + /// worth turning a clean not-found into a parse error over. + /// + /// + /// It says which municipality the number would be in, not that any + /// address exists. Read before using + /// and : + /// they bound the CEPs that exist there, and the range between them is + /// mostly empty. + /// + /// + public Faixa? Faixa { get; } } -- cgit v1.3