External servicesConnecting and calling
A provider's failures
A provider package declares the failures its provider refuses a call
with in failures/, and each operation lists the ones it can answer with
in failures:. The runtime reads a refusal, a 4xx the driver doesn't
read as something else, as one of them. By default it reads the text at
the provider's error_code: as the failure's code: slot_taken is
SlotTaken.
Many providers send no such code. postcodes.io answers a postcode it
doesn't hold with 404 and a sentence, {"status": 404, "error": "Postcode not found"}. A failure then says how its provider answers it:
| Written | Means |
|---|---|
wire_status: 404 |
The HTTP status the provider refuses with: a 4xx but 401, 403, 408 and 429, which the driver reads first |
wire_name: "Postcode not found" |
The provider's own text at error_code:, exactly as it writes it, where that isn't the failure's code |
/// postcodes.io holds no such postcode.public type PostcodeNotFound = Failure { kind: FailureKind.not_found wire_status: 404 wire_name: "Postcode not found"}
/// The text isn't a postcode postcodes.io can read.public type InvalidPostcode = Failure { kind: FailureKind.invalid_value wire_name: "Invalid postcode"}
/// postcodes.io found nothing at that address.public type NothingFound = Failure { kind: FailureKind.not_found wire_status: 404}
public resource postcodes { get: { method: HttpMethod.get path: "/postcodes/{postcode}" input: PostcodeLookup output: PostcodeAnswer changes: false failures: [PostcodeNotFound, InvalidPostcode, NothingFound] source: PackageSource.written }}
A failure's code is still its Jadpo code, postcode_not_found from its
name. An app maps it by that code and a test scripts it by that code;
wire_name: is only how the provider writes it, as a field's
wire_name: is its spelling on the wire.
A refusal is the most specific failure of the operation that matches it:
- one whose
wire_status:andwire_name:both match; - one whose text matches, at any status: its
wire_name:, or its code when it gives neither key; - one whose
wire_status:matches and gives nowire_name:, whatever the text, or with none.
So 404 {"error": "Postcode not found"} is PostcodeNotFound, an
answer of "Invalid postcode" with any status is InvalidPostcode, and
any other 404, such as "Outcode not found", is NothingFound. A refusal
no failure matches is a fault naming the operation and the provider's
code. Two failures of one operation read the same way are
package.operation_invalid, since nothing would tell them apart.
The app maps them at the call as any package failure:
use app { postcodes }use addresses/failures { AddressUnknown, AddressUnreadable }use shared/providers/postcodes_io/// Where a postcode someone typed is.action place_postcode( postcode: PostcodesIo.Postcode) fails AddressUnknown, AddressUnreadable -> PostcodesIo.PostcodeAnswer { return attempt postcodes.postcodes.get( PostcodesIo.PostcodeLookup { postcode: postcode } ) { postcode_not_found: AddressUnknown invalid_postcode: AddressUnreadable nothing_found: AddressUnknown }}
use app { postcodes }use addresses/failures { AddressUnknown, AddressUnreadable }test "a postcode postcodes.io doesn't hold is unknown" { fakes: { postcodes.postcodes.get: [postcode_not_found] } assert place_postcode("ZZ1 1ZZ") fails AddressUnknown}test "a postcode it can't read is unreadable" { fakes: { postcodes.postcodes.get: [invalid_postcode] } assert place_postcode("SW1A") fails AddressUnreadable}
Markdown for agents: /docs/a-providers-failures.md.Built from Jadpo's main branch at commit 9080dc3.