From 47c822d0d5e690092617b6170972917058a6d45d Mon Sep 17 00:00:00 2001 From: Murilo Ijanc Date: Mon, 27 Jul 2026 20:15:36 -0300 Subject: Carry the municipality of a CEP that does not exist A 404 that only says "not found" is correct and useless. The CEP that fails is almost never random: it is someone who knows the city and guessed the number, and the classic guess is the -000 that 957 municipalities never received. The API now names the municipality on the 404, so surface it as Error.Faixa instead of throwing the body away. --- client.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'client.go') diff --git a/client.go b/client.go index 53ada45..f1e3d90 100644 --- a/client.go +++ b/client.go @@ -225,11 +225,21 @@ func (c *Client) Lookup(ctx context.Context, cep string) (*Address, error) { switch resp.status { case http.StatusNotFound: + // The body may name the municipality the CEP would fall in; when it + // does, say so in the message too, because most callers only ever + // print the error. + faixa := decodeFaixa(resp.body) + message := "CEP " + number + " does not exist in the DNE database" + if faixa != nil { + message += " (range of " + faixa.Localidade + "/" + faixa.UF + + ", which goes from " + faixa.CEPIni + " to " + faixa.CEPFim + ")" + } return nil, &Error{ Kind: ErrCEPNotFound, CEP: number, Status: resp.status, - Message: "CEP " + number + " does not exist in the DNE database", + Faixa: faixa, + Message: message, } case http.StatusBadRequest: return nil, &Error{ -- cgit v1.3