kunibert
All repositories: gitoria
2.1 KB
# hl:fetch`fetch(url, options)` makes an HTTP or HTTPS request and waits for the answer.## When the connection failsA refused connection, an unknown host, a connect timeout, a reset by the peer and a TLSverification failure are all raised as an ordinary located error. The message names the method,the URL and the reason, for example`hl:fetch POST http://127.0.0.1:9/x: connection refused`,`hl:fetch GET http://nonexistent.invalid/x: could not resolve host` or `hl:fetch GET http://10.255.255.1/x: timed out`.The error can be caught with `on Error(e)`, in a plain program and inside a request handler alike.When the handler has absorbed the error, `fetch` gives back `null`, so the code can answer the failure itself:```hybrielimport { NativeHttpServer, Response } from 'hl:http1'import { fetch } from 'hl:fetch'inherit NativeHttpServerport = 8080on Error(e) { console.log('fetch failed: ' + e.message) }on request(req) {let res = fetch('http://127.0.0.1:9/x', { method = 'POST' json = { a = 1 } })if (res == null) { return new Response('upstream is down', { status = 502 }) }return new Response(res.text())}listen()```Without an `on Error` handler the error is not caught. That is the language rule: the request then endswith the framework's `500` answer carrying the located message, and the server keeps serving.## A failure as a valueImport `tryFetch` from `hl:fetch` to handle a failed exchange without `on Error`.It accepts fetch's options and waits for completion. Every HTTP reply, including404 or 500, has the same response and text()/json() readers as fetch. Connection,DNS, TLS and timeout failures return exactly `{ ok = false, status = 0, error }`.The error is fetch's message without its call site, for example`hl:fetch GET http://127.0.0.1:9/x: connection refused`. Native fetch is unchanged.The JS target and browser use the host Fetch API with the same response shapeand whole-exchange `timeoutMs`. Browsers enforce CORS and expose only a genericnetwork reason for many failures. `caFile` is supported only by the native engine.`tryFetch` waits even with `stream = true`; use fetchStart for native chunk events.
Branches
Latest commits
- 8205b05eRecords: #17 livemre
- c3391204Answer fresh reviews under open threadsKunibert
- a0f3d147Records: #16 livemre
- 04322148Run reviews in parallel across repositoriesKunibert
- a6577aaeRecords: #14 live, #16 startedmre
- 0382238bStart fresh on Review again and name vendored pathsKunibert
- 5608c6f1Records: kunibert#13 vendored paths livemre
- 8840771dExclude vendored findings from app verdictsKunibert
- 6aa65353Records: jukebox connected, first reviewmre
- ad8011cfRecords: neutral ticket rule, jukeboxmre
- ca5f31dfPrompt: neutral reviewer identitymre
- b241d9c8Records: Anton tryParse noteKunibert
- 42cc1bc0Records: K12 doneKunibert
- 9e6039eeRecords: gates K11, K12Kunibert
- 59b8fe83Use Hybriel day39 helpersKunibert
- c2e393a2Records: K12 startedKunibert
- 00696678Records: Anton day39 notemre
- 070e01a4Records: parkmre
- bf8af0dcRecords: v1.4 checkedmre
- 44af321bRecords: contract v1.4mre