HTTP plumbing for Exa: one HttpError shape with a readable message, and send.

Functions

error-detail

fn (body: Any): Any

Exa's error text from a response body, or null. Understands {error}, and plain {message} or string bodies. Exposed for testing.

Example

::exa::http/error-detail({error: "Invalid request"})  // "Invalid request"
::exa::http/error-detail("Bad gateway")  // "Bad gateway"
::exa::http/error-detail([])  // null

error-message

fn (status: Int, body: Any): Str

Exa API error <status>: <detail>, or without the detail when the body has none. Exposed for testing.

Example

::exa::http/error-message(400, {error: "Invalid request"})  // "Exa API error 400: Invalid request"
::exa::http/error-message(500, null)  // "Exa API error 500"

http-error

fn (response: ::hot::http/HttpResponse): HttpError

Build the HttpError for a non-2xx response. Exposed for testing.

Example

response {status: 401, headers: {}, body: {message: "Bad key"}}
e ::exa::http/http-error(response)
e.status  // 401
e.message  // "Exa API error 401: Bad key"

send

fn (request: ::hot::http/HttpRequest): Any

Send request and unwrap: the decoded JSON body on 2xx, an Err carrying HttpError otherwise, or the transport Err.

Example

key ::hot::ctx/get("exa.api.key")
body ::exa::http/send(::exa/build-request("/search", {query: "hot language", numResults: 3}, key))
length(body.results)

Types

HttpError

HttpError type {
    status: Int,
    message: Str,
    headers: Map,
    body: Any
}

A non-2xx Exa response. message is readable text such as Exa API error 400: Invalid request; body keeps the raw response.