HTTP plumbing for Tavily: one HttpError shape with a readable message, and send.
Functions
error-detail
fn (body: Any): Any
Tavily's error text from a response body, or null. Understands {detail: {error}}, {detail}, and plain {message} or string bodies. Exposed
for testing.
Example
::tavily::http/error-detail({detail: {error: "Unauthorized"}}) // "Unauthorized"
::tavily::http/error-detail({message: "Bad request"}) // "Bad request"
::tavily::http/error-detail(500) // null
error-message
fn (status: Int, body: Any): Str
Tavily API error <status>: <detail>, or without the detail when the
body has none. Exposed for testing.
Example
::tavily::http/error-message(401, {detail: {error: "Unauthorized"}}) // "Tavily API error 401: Unauthorized"
::tavily::http/error-message(500, "") // "Tavily API error 500"
http-error
fn (response: ::hot::http/HttpResponse): HttpError
Build the HttpError for a non-2xx response. Exposed for testing.
Example
response ::hot::http/HttpResponse({status: 429, headers: {}, body: {detail: "Slow down"}})
e ::tavily::http/http-error(response)
e.status // 429
e.message // "Tavily API error 429: Slow down"
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("tavily.api.key")
out ::tavily::http/send(::tavily/build-request("/search", {query: "hot language"}, key))
if(is-err(out), out, map(out.results, (r) { r.url }))
Types
HttpError
HttpError type {
status: Int,
message: Str,
headers: Map,
body: Any
}
A non-2xx Tavily response. message is readable text such as Tavily API error 401: Unauthorized: missing or invalid API key.; body keeps the raw response.