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

Functions

error-detail

fn (body: Any): Any

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

Example

::brave::search::http/error-detail({error: {detail: "Bad query"}})  // "Bad query"
::brave::search::http/error-detail({message: "Rate limited"})  // "Rate limited"
::brave::search::http/error-detail("Forbidden")  // "Forbidden"
::brave::search::http/error-detail({})  // null

error-message

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

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

Example

::brave::search::http/error-message(422, {error: {detail: "Bad query"}})  // "Brave API error 422: Bad query"
::brave::search::http/error-message(500, null)  // "Brave 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: 422, headers: {}, body: {error: {detail: "Bad query"}}}
::brave::search::http/http-error(response).message  // "Brave API error 422: Bad query"

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

req ::brave::search/build-request("/web/search?q=rust", ::hot::ctx/get("brave.api.key"))
out ::brave::search::http/send(req)

// The decoded body, or the readable message when the call failed
if-err(out, (e) { e.message })

Types

HttpError

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

A non-2xx Brave response. message is readable text such as Brave API error 422: Unable to validate request parameter(s); body keeps the raw response.