diff options
Diffstat (limited to 'lib/src/errors.dart')
| -rw-r--r-- | lib/src/errors.dart | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/lib/src/errors.dart b/lib/src/errors.dart index 24b35c4..be32b25 100644 --- a/lib/src/errors.dart +++ b/lib/src/errors.dart @@ -18,6 +18,8 @@ /// all, when the CEP is malformed before it ever leaves the machine. library; +import 'models.dart'; + /// Base of every error this library throws. Catch this to handle any failure. /// /// Sealed: the subtypes below are the complete set, so a `switch` on one is @@ -59,12 +61,40 @@ final class InvalidCepException extends CepisException } /// The CEP is well-formed but does not exist in the DNE database. +/// +/// [faixa] may name the municipality the CEP would have belonged to. It does +/// not soften the error: this is still a 404, and there is still no address. final class CepNotFoundException extends CepisException { - /// Creates the error for [message], recording the normalised [cep]. - const CepNotFoundException(super.message, {this.cep}); + /// Creates the error for [message], recording the normalised [cep] and the + /// [faixa] the API named, when it named one. + const CepNotFoundException(super.message, {this.cep, this.faixa}); /// The CEP that was looked up, normalised to 8 digits. final String? cep; + + /// The municipality that owns the numeric band this CEP fell in, or `null`. + /// + /// `null` when the CEP falls in a band no municipality owns — the API + /// **omits** the block there rather than sending null — and also whenever the + /// block cannot be read. See [Faixa], and read [Faixa.cepIni]/[Faixa.cepFim] + /// as the extremes that exist, not as a range of valid CEPs. + final Faixa? faixa; + + /// Mentions the municipality when the API named one. + /// + /// The count comes along deliberately: `118 CEPs` across nearly five + /// thousand numbers is what stops the two bounds from reading like a range + /// where anything in between would work. + @override + String toString() { + final band = faixa; + if (band == null) { + return super.toString(); + } + return '$runtimeType: $message — that band belongs to ' + '${band.localidade}/${band.uf}, which has ${band.ceps} CEPs scattered ' + 'between ${band.cepIni} and ${band.cepFim}'; + } } /// The request never completed: DNS, refused connection, timeout, TLS. |