Skip to content

Errors

Each Scraper declares the failures it can end with - Shared errors every caller understands, plus its own tagged errors.

A Scraper's failures are part of its contract. The Definition's error schema lists every way a Run can fail, and the compiler rejects an Implementation that can fail any other way.

There are two kinds of declared errors: Shared errors, which every Scraper may use, and a Scraper's own errors.

Shared errors ​

Shared errors describe what went wrong in terms a caller can act on.

ErrorMeansA caller can
BlockedThe source refused us: HTTP 401 or 403, a challenge page, an empty body.Stop. Retrying the same way will be refused again.
RateLimitedThe source asked to slow down: HTTP 429.Wait and retry.
UpstreamDownThe source failed or could not be reached: 5xx, a timeout, a transport error.Retry later.
QuotaExhaustedA paid quota ran out, for example HTTP 402.Top up or wait for the quota to reset.
SchemaDriftThe source's data, or the Scraper's output, no longer matches its schema.Repair the Scraper. Retrying will not help.
InvalidInputThe input does not match the Definition.Change the input. Retrying will not help.
RunTimeoutThe Run reached its Release's maximum duration on an instance (Run duration). The items before it are kept.Narrow the input, or raise the Release's maxRunDuration.
RequestLimitExceededThe Run's requests reached Cloudflare's limit on outgoing requests from one Run on an instance, 10,000 by default (Known gaps). The items before it are kept.Split the work into Runs of fewer requests. Retrying the same input will hit the limit again.

All of them are Schema.TaggedError classes with a reason string. SchemaDrift also has a sample: up to 500 characters of the data that did not match.

ts
new Blocked({ reason: `GET ${url}: HTTP 403` });

new SchemaDrift({ reason: issue.message, sample: body.slice(0, 500) });

Who raises them ​

  • Your Implementation raises Blocked, RateLimited, UpstreamDown, QuotaExhausted and SchemaDrift. The HTTP helpers do most of that mapping for you.
  • Scraper.run raises InvalidInput when the input fails the schema, and SchemaDrift when an item fails the output schema. You do not need to declare those two for that purpose: on an instance the result record's failure carries them either way.
  • An instance raises RunTimeout when a Run reaches its maximum duration, and RequestLimitExceeded when a Run fails after Cloudflare refused one of its requests for the subrequest limit, in place of the UpstreamDown the Scraper saw. Declare them so callers decode them with the rest of your errors; the result record carries them either way.

Declare InvalidInput in your Definition only when the Implementation itself can reject an input the schema allows, for example a date the source has no data for.

Your own errors ​

When a failure is specific to one source, declare it as a Schema.TaggedError in the Scraper's package and add it to the Definition's union.

Example (A Scraper's own error, declared next to the Shared ones)

ts
class SoldOut extends Schema.TaggedError<SoldOut>()("SoldOut", {
  remaining: Schema.FiniteFromString,
}) {}

const Tickets = Scraper.make("tickets", {
  error: Schema.Union([Blocked, RateLimited, SchemaDrift, UpstreamDown, SoldOut]),
  input: Schema.Struct({ event: Schema.NonEmptyString }),
  output: Schema.Struct({ price: Schema.FiniteFromString }),
});

SoldOut is a fact about this source, so it carries what a caller needs: how many tickets remain. Give an error the fields that let the caller decide what to do next.

Nothing reclassifies a Scraper's own errors into Shared errors. A caller that runs the Scraper in process sees these tags unchanged.

Choosing an error ​

The source answeredUse
401, 403, a login wall, a bot challengeBlocked
429RateLimited
402 or "limit reached" for a paid planQuotaExhausted
5xx, timeout, connection refusedUpstreamDown
200 with a body you cannot parseSchemaDrift
200 with a challenge pageBlocked, not an empty result
200 with a valid, empty listNo error. An empty Run is a success.
Something only this source doesYour own error

The last two rows matter most. A challenge page served with HTTP 200 must never look like "no items", and a legitimately empty answer must never look like a failure.

Failures you did not expect ​

A thrown exception or a bug is a defect. It is not in the error union and is not something a caller can react to. Defects end the Run as failed.

Put the context of any failure on the trace, not in a side channel. Name steps with Effect.fn, add facts with Effect.annotateCurrentSpan, and put a link or a storage key on the span for anything large.

Errors on an instance ​

An instance stores a failed Run's error as a string in its result record. The string names the error's tag but not its fields. The full context is on the Run's trace.

Run the Scraper in process with Scraper.run to see the typed error with its reason and sample.

Source ​