diff options
| author | Murilo Ijanc <[email protected]> | 2026-07-27 20:15:36 -0300 |
|---|---|---|
| committer | Murilo Ijanc <[email protected]> | 2026-07-27 20:23:14 -0300 |
| commit | 47c822d0d5e690092617b6170972917058a6d45d (patch) | |
| tree | 583ce076573d29a60dc8453df7089022458b7303 /client.go | |
| parent | 323d9a8422f06676d25cdf890c06962b260cb8aa (diff) | |
| download | go-0.2.0.tar.gz | |
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.
Diffstat (limited to 'client.go')
| -rw-r--r-- | client.go | 12 |
1 files changed, 11 insertions, 1 deletions
@@ -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{ |