diff options
Diffstat (limited to 'src/client.c')
| -rw-r--r-- | src/client.c | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/src/client.c b/src/client.c index 58c55b3..27812f2 100644 --- a/src/client.c +++ b/src/client.c @@ -513,6 +513,20 @@ static void api_message(const cepis_buf_t *body, char *out, size_t out_size, json_free(root); } +/* + * A 404 body may carry a `faixa` block naming the municipality the CEP would + * have belonged to. Nothing here can fail: an unparseable body just means the + * API named no municipality, which is also what an absent block means. + */ +static bool faixa_from_body(const cepis_buf_t *body, cepis_faixa_t *out) +{ + json_value_t *root = json_parse(body->data, body->len); + bool named = cepis_faixa_from_json(root, out); + + json_free(root); + return named; +} + static cepis_status_t build_url(const cepis_client_t *client, const char *path, cepis_buf_t *out) { @@ -558,10 +572,23 @@ cepis_status_t cepis_lookup(cepis_client_t *client, const char *cep, return status; if (response.status == 404) { + cepis_faixa_t faixa; + bool named = faixa_from_body(&response.body, &faixa); + cepis_buf_free(&response.body); - return cepis_set_error( + status = cepis_set_error( err, CEPIS_ERR_NOT_FOUND, 404, -1.0, "CEP %s does not exist in the DNE database", number); + /* + * After cepis_set_error, which clears has_faixa: the block is + * extra detail about the failure, never a reason to report a + * different one. + */ + if (named && err != NULL) { + err->faixa = faixa; + err->has_faixa = true; + } + return status; } if (response.status == 400) { snprintf(fallback, sizeof(fallback), "CEP %s rejected", number); |